C#错误:"填充:SelectCommand.Connection属性尚未初始化."

use*_*123 7 c# sql-server datagridview

我正在使用此视频尝试填充a的结果DataGridView,并收到上述错误.下面的代码与此错误有关 - 我将值传递给存储过程,然后最终SELECT返回表中的值DataGridView.

        SqlConnection con = new SqlConnection();
        con.ConnectionString = "integrated security=SSPI;data source=SERV;" + "persist security info=False;initial catalog=DB";

        con.Open();
        SqlCommand select = new SqlCommand("SELECT * FROM Table");
        SqlCommand enter = new SqlCommand("sp_Proc", con);

        // Stored Procedure
        enter.CommandType = CommandType.StoredProcedure;
        enter.Parameters.Add(new SqlParameter("@vvalue", SqlDbType.VarChar)).Value = Convert.ToString(txt1.Text);
        enter.Parameters.Add(new SqlParameter("@dvalue", SqlDbType.Decimal)).Value = Convert.ToDecimal(txt2.Text);
        enter.ExecuteNonQuery();

        // DataGrid returns the SELECT

        SqlDataAdapter sadapt = new SqlDataAdapter(select);
        sadapt.SelectCommand = select;
        DataTable dtab = new DataTable();
        sadapt.Fill(dtab); // generates the error
        BindingSource b = new BindingSource();
        b.DataSource = dtab;
        dGrid.DataSource = b;
        sadapt.Update(dtab);

        con.Close();
Run Code Online (Sandbox Code Playgroud)

Joh*_*Woo 14

你没有传递connection对象command.试试这个,

SqlCommand select = new SqlCommand("SELECT * FROM Table", con);
Run Code Online (Sandbox Code Playgroud)