Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.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# 将dataGridView连接到SQL查询_C#_Sql_Winforms_Datagridview - Fatal编程技术网

C# 将dataGridView连接到SQL查询

C# 将dataGridView连接到SQL查询,c#,sql,winforms,datagridview,C#,Sql,Winforms,Datagridview,我正在用C Windows窗体和SQL数据库构建一个简单的预订系统。作为程序的一部分,用户可以在文本框中添加一些文本,然后单击“搜索”按钮搜索符合搜索条件的预订。当用户单击该按钮时,执行SQL查询并将查询结果更新到dataGridView中 下面的示例是我代码中的简单示例。booking_id的格式为int,firstname为字符串。程序不断抛出System.InvalidCastException和error与试图将booking_id插入SQL字符串的行有某种关联。我已经搜索了一些有关此错

我正在用C Windows窗体和SQL数据库构建一个简单的预订系统。作为程序的一部分,用户可以在文本框中添加一些文本,然后单击“搜索”按钮搜索符合搜索条件的预订。当用户单击该按钮时,执行SQL查询并将查询结果更新到dataGridView中

下面的示例是我代码中的简单示例。booking_id的格式为int,firstname为字符串。程序不断抛出System.InvalidCastException和error与试图将booking_id插入SQL字符串的行有某种关联。我已经搜索了一些有关此错误的信息,但仍然无法修复它。这一定是我在这里遗漏的一些非常简单的东西——有人能提供一些帮助吗?提前谢谢你

// User clicks 'Search' butto.
private void button_Search1_Click(object sender, EventArgs e)
{
    string commandText = "SELECT * FROM Booking " +
        "INNER JOIN Customer ON Booking.customer_id = Customer.customer_id" +
        "WHERE Booking.booking_date IS NOT NULL AND " +
        "Booking.booking_id_id LIKE '%' + @booking_id + '%' " +
        "Customer.customer.firstname LIKE '%' + @firstname + '%'";

    using (SqlConnection connection = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;" + @"AttachDbFilename=|DataDirectory|\Booking.mdf; Integrated Security=True"))
    {
        //Create a SqlCommand instance
        SqlCommand command = new SqlCommand(commandText, connection);

        //Add the parameter
        //THESE LINES CAUSE PROBLEM!?
        command.Parameters.Add("@booking_id", SqlDbType.Int).Value = Convert.ToInt32(textBox_BookindId);
        command.Parameters.Add("@firstname", SqlDbType.VarChar, 20).Value = textBox_Firstname;

        // Open SQL connection and execute query.
        connection.Open();
        command.ExecuteNonQuery();

        SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
        DataTable dt = new DataTable();
        dataAdapter.Fill(dt);
        dataGridView1.DataSource = dt;

如果目标是强制Datatable dt中的booking_id_id列成为字符串,以便它很好地进入文本框,那么在选择booking id时将其转换为字符串。请注意,我已使用“as”将列命名为Booking ID


string commandText=选择Customer.Customer\u id、Booking.Booking\u date、CASTBooking.Booking\u id\u id作为NVARCHAR10作为“Booking id”,Booking中的Customer.Customer.firstname+在Booking中内部加入Customer.Customer\u id=Customer.Customer\u id+其中Booking.Booking\u日期不为空,并且+Booking.Booking\u id\u id类似于“%”++@Booking\u id+“%”+Customer.Customer.firstname类似于“%++@firstname+”

阅读整个异常消息。我敢打赌它的状态类似于:附加信息:无法将“System.Windows.Forms.TextBox”类型的对象强制转换为“System.IConvertible”。