简体   繁体   English

ExecuteNonQuery:连接属性尚未初始化

[英]ExecuteNonQuery: Connection property has not been initialized

I have this code and when I run it gives this error ExecuteNonQuery: Connection property has not been initialized. 我有这段代码,运行时它给出此错误ExecuteNonQuery:Connection属性尚未初始化。 And I have sql database. 而且我有sql数据库。 its name is Cost. 它的名字叫Cost。 I have this code and when I run it gives this error ExecuteNonQuery: Connection property has not been initialized. 我有这段代码,运行时它给出此错误ExecuteNonQuery:Connection属性尚未初始化。 And I have sql database. 而且我有sql数据库。 its name is Cost. 它的名字叫Cost。 My code is: 我的代码是:

    namespace Accountingss
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        public SqlConnection conn;

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void Connect(string cmdtxt, Hashtable parameters)
        {
            conn = new SqlConnection();
            string connString = @"Data      Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Cost.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
            conn.ConnectionString = connString;
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = cmdtxt;
            cmd.Parameters.Clear();
            var ieParams = parameters.GetEnumerator();
            while (ieParams.MoveNext())
            {
                cmd.Parameters.AddWithValue(ieParams.Key.ToString(), ieParams.Value.ToString());
                //cmd.Parameters.Add(new SqlParameter(ieParams.Key.ToString(), ieParams.Value.ToString()));
            }

            conn.Open();
            cmd.ExecuteNonQuery();

            //SqlDataAdapter costdataAdpater = new SqlDataAdapter();
            //DataTable costdataTable = new DataTable(); 
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string insert = "INSERT INTO Cost (Type, Amount) VALUES (@type,         @amount)";// +type.Text + ',' + a.Text + ")";
            var addpTA = new Hashtable();
            addpTA.Add("@type", txtType.Text);
            addpTA.Add("@amount", txtAmount.Text);
            Connect(insert, addpTA);
        }
    }
}

You have to assign connection to sql command like below. 您必须将连接分配给sql命令,如下所示。 It seems that you have forgot to do so. 看来您已经忘记这样做了。

cmd.Connection = conn;

You didn't connected the command to the connection. 您没有将命令连接到连接。

cmd.Connection = conn;

And after executing the command you should close it. 在执行命令后,您应该关闭它。

conn.Close();

Simply connect the SqlConnection to your SqlCommand before executing 只需在执行之前将SqlConnection连接到SqlCommand即可

cmd.Connection = conn;

The Connection object is the tool that delivers our commands to the underlying database engine. Connection对象是将我们的命令传递到基础数据库引擎的工具。
We need to plumb it to our commands if we want to reach the database. 如果要访问数据库,我们需要将其放入命令中。

A good shortcut is to create the command directly from the connection using this method 一个好的捷径是使用此方法直接从连接创建命令

   SqlCommand cmd = conn.CreateCommand();

You should pass connection to command 您应该将连接传递给命令

SqlCommand cmd = new SqlCommand(conn);

or 要么

cmd.Connection = conn;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM