Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.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#_.net_Ado.net - Fatal编程技术网

C# 为什么我会得到;连接属性尚未初始化";在这里

C# 为什么我会得到;连接属性尚未初始化";在这里,c#,.net,ado.net,C#,.net,Ado.net,1.ExecuteOnQuery:连接属性尚未初始化”。 2.这是执行以下代码时出现的错误 protected void Button1_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection(); conn.ConnectionString = @"Data Source=Sadiq;Initial Catalog=rafi;Integr

1.ExecuteOnQuery:连接属性尚未初始化”。 2.这是执行以下代码时出现的错误

protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = @"Data Source=Sadiq;Initial Catalog=rafi;Integrated Security=True";
            conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "student";
            int studid = Convert.ToInt32(TextBox1.Text);
            string name = TextBox2.Text;
            int age = Convert.ToInt32(TextBox3.Text);
            string school = TextBox4.Text;
            string clas = TextBox5.Text;
            int marks = Convert.ToInt32(TextBox6.Text);
            string grade = TextBox7.Text;
            cmd.Parameters.Add(new SqlParameter("@stud_id", studid));
            cmd.Parameters.Add(new SqlParameter("@name", name));
            cmd.Parameters.Add(new SqlParameter("@age", age));
            cmd.Parameters.Add(new SqlParameter("@school", school));
            cmd.Parameters.Add(new SqlParameter("@class", clas));
            cmd.Parameters.Add(new SqlParameter("@marks", marks));
            cmd.Parameters.Add(new SqlParameter("@grade", grade));
            cmd.ExecuteNonQuery();
            conn.Close();
}
您需要执行以下操作:

cmd.Connection = conn;
在执行查询之前。它需要一个连接来执行

您还可以将其重构为:

SqlCommand cmd = new SqlCommand("student", conn);
cmd.CommandType = CommandType.StoredProcedure;
而不是:

SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "student";
cmd.Connection = conn;
该重载通过构造函数设置
CommandText
Connection
属性

您还需要使用语句将所有这些ADO.NET对象包装在
中。我将在一篇文章中详细解释。

您需要执行以下操作:

cmd.Connection = conn;
在执行查询之前。它需要一个连接来执行

您还可以将其重构为:

SqlCommand cmd = new SqlCommand("student", conn);
cmd.CommandType = CommandType.StoredProcedure;
而不是:

SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "student";
cmd.Connection = conn;
该重载通过构造函数设置
CommandText
Connection
属性


您还需要在
using
语句中包装所有这些ADO.NET对象。我在一篇文章中详细解释了它。

您没有告诉
cmd
要使用哪个连接。将它作为参数传递给
new SqlCommand()
您没有告诉
cmd
要使用哪个连接。将它作为参数传递给
new SqlCommand())