在Datagridview中显示数据库搜索结果

时间:2022-06-24 05:22:45

I am using Visual Studio 2013 with SQL Server 2012 for database in my Windows forms application c#.

我正在使用Visual Studio 2013和SQL Server 2012作为我的Windows窗体应用程序c#中的数据库。

I want to show my query search results in a DataGridView:

我想在DataGridView中显示我的查询搜索结果:

public void customerSearch(int custID, DataGridView dataGridView)
{
        try
        {
            SqlConnection connection = new SqlConnection(@"Connection String");
            connection.Open();

            SqlCommand searchQuery = new SqlCommand("select * from [Customer] where custId = @custID", connection);
            searchQuery.Parameters.AddWithValue("@custId", custID);
            //searchQuery.ExecuteNonQuery();

            using (SqlDataReader reader = searchQuery.ExecuteReader())
            {
                while (reader.Read())
                {
                    dataGridView.DataBindings.ToString();
                }
            }
        }
        catch (SqlException Exception)
        {
            MessageBox.Show(Exception.ToString());
        }
        finally
        {
            connection.Close();
        }
    }
}

I got NullReferenceException at connection.Close(); Any help will be appreciated.

我在connect得到NullReferenceException。close ();如有任何帮助,我们将不胜感激。

1 个解决方案

#1


4  

When you put your connection in a USING it will automatically close and dispose your connection when the block exits.

当你把你的连接放在一个使用它将自动关闭和处理你的连接当块退出。

public void customerSearch(int custID, DataGridView dataGridView)
        {
            using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString))
            {
                try
                {

                    connection.Open();

                    SqlCommand searchQuery = new SqlCommand("select * from [Customer] where custId = @custID", connection);
                    searchQuery.Parameters.Add("@custId", SqlDbType.Int).Value = custID;
                    //searchQuery.ExecuteNonQuery();

                    using (SqlDataReader reader = searchQuery.ExecuteReader())
                    {
                        DataTable dt = new DataTable();
                        dt.Load(reader);
                        dataGridView.AutoGenerateColumns = true;
                        dataGridView.DataSource = dt;
                        dataGridView.Refresh();
                    }
                }
                catch (SqlException Exception)
                {
                    MessageBox.Show(Exception.ToString());
                }
            }
        }

--EDIT--

——编辑

You can't bind a DataGridView to a DataReader. I edited the code above to demonstrate one way to do this. I just create a DataTable from your reader and bind the grid to the DataTable.

不能将DataGridView绑定到DataReader。我编辑了上面的代码以演示一种方法。我只是从您的阅读器中创建一个DataTable,并将网格绑定到DataTable。

#1


4  

When you put your connection in a USING it will automatically close and dispose your connection when the block exits.

当你把你的连接放在一个使用它将自动关闭和处理你的连接当块退出。

public void customerSearch(int custID, DataGridView dataGridView)
        {
            using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString))
            {
                try
                {

                    connection.Open();

                    SqlCommand searchQuery = new SqlCommand("select * from [Customer] where custId = @custID", connection);
                    searchQuery.Parameters.Add("@custId", SqlDbType.Int).Value = custID;
                    //searchQuery.ExecuteNonQuery();

                    using (SqlDataReader reader = searchQuery.ExecuteReader())
                    {
                        DataTable dt = new DataTable();
                        dt.Load(reader);
                        dataGridView.AutoGenerateColumns = true;
                        dataGridView.DataSource = dt;
                        dataGridView.Refresh();
                    }
                }
                catch (SqlException Exception)
                {
                    MessageBox.Show(Exception.ToString());
                }
            }
        }

--EDIT--

——编辑

You can't bind a DataGridView to a DataReader. I edited the code above to demonstrate one way to do this. I just create a DataTable from your reader and bind the grid to the DataTable.

不能将DataGridView绑定到DataReader。我编辑了上面的代码以演示一种方法。我只是从您的阅读器中创建一个DataTable,并将网格绑定到DataTable。