Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 对象类型、系统窗口窗体中不存在映射_C#_Ado.net - Fatal编程技术网

C# 对象类型、系统窗口窗体中不存在映射

C# 对象类型、系统窗口窗体中不存在映射,c#,ado.net,C#,Ado.net,我尝试使用ADO.NET连接到SQL Server数据库。我尝试构建一个windows窗体,并放置一些文本框和按钮,以便输入值并检查数据库中的记录。但结果表明,不存在从对象类型System.Windows.Forms Textbox到已知托管提供的本机类型的映射 那么,你能给我一些建议吗 我实现如下代码: using System; using System.Data.SqlClient; using System.Collections.Generic; using System.Compon

我尝试使用ADO.NET连接到SQL Server数据库。我尝试构建一个windows窗体,并放置一些文本框和按钮,以便输入值并检查数据库中的记录。但结果表明,不存在从对象类型System.Windows.Forms Textbox到已知托管提供的本机类型的映射

那么,你能给我一些建议吗

我实现如下代码:

using System;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace casestudy
{
    public partial class Form1 : Form
    {
        SqlConnection vcon1 = new SqlConnection(@"Data Source=SOPHIA-PC\SQLEXPRESS;Initial Catalog=casestudy;Integrated Security=True");

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
            vcon1.Open();

        }

        catch (Exception ex)
        {
            MessageBox.Show("error.occured" + ex.Message);
            this.Dispose();
        }

    }
    private void Find_Click(object sender, EventArgs e)
    {
        string querystring = "SELECT * FROM AssignedSolution WHERE CASEID = @caseid";
        SqlCommand Vcom = new SqlCommand(querystring, vcon1);
        Vcom.Parameters.AddWithValue("@caseid", txtCASEID);
        SqlDataReader rdr = null;

        try
        {
            Vcom.Connection = vcon1;

            Vcom.ExecuteNonQuery();


            rdr = Vcom.ExecuteReader();
                            while (rdr.Read())
            {
                Console.WriteLine(rdr[0]);

            }
            Vcom.Dispose();

        }

        catch (Exception ex)
        {
            MessageBox.Show("error.occured" + ex.Message);

        }
           finally
           {
              vcon1.Close();
              vcon1.Dispose();
           }
        }
    }
}

我相信你的问题就在这一行:

Vcom.Parameters.AddWithValue("@caseid", txtCASEID);
请尝试以下方法:

Vcom.Parameters.AddWithValue("@caseid", txtCASEID.Text);

您正试图提供从
文本框
SqlParameter
的值。
AddWithValue
方法将参数名及其值作为参数,但value参数的类型为
对象
,因此您不会因为尝试分配它而收到编译器的任何投诉。但是,
txtCASEID
是一个
文本框
而不是
字符串
;您需要使用它的
Text
属性来获取
string
值,因此
txtCASEID.Text

在这个方法中
Form1\u Load
您不需要在那里打开连接,因为。。将它放在click事件中,并查看如何使用
using(){}
构造
Sql数据对象
,这样您就不必显式调用
Dispose()
方法。嗨,Roryap,非常感谢!我添加了一个文本,所以这次不会返回任何错误。但是,单击“查找”时无法查看记录。我不知道是否需要添加DataGridview或Datareader?我不知道怎样才能在表格上看到结果。