简体   繁体   English

'ExecuteNonQuery:连接属性尚未初始化。'

[英]'ExecuteNonQuery: Connection property has not been initialized.'

I have searched this forum, and have tried a multitude of possible solutions I found, but nothing is working.我已经搜索了这个论坛,并尝试了我找到的多种可能的解决方案,但没有任何效果。 can anyone shed some light on this situation?任何人都可以对这种情况有所了解吗? Thanks!谢谢!

SqlConnection con = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=TESTdatabase;Integrated Security=True");
con.Open();

SqlCommand cmd = new SqlCommand(
  "INSERT into tblGenerator (GeneratorName, GeneratorAddress, GeneratorCity, GeneratorState, GeneratorZip, GeneratorPhone, GeneratorContact, GeneratorEPAID) " +
  "VALUES ('" + GenName.Text + "' , '" + GenAdd.Text + "' , '" + GenCity.Text + "' , '" + GenState.Text + "' , '" + GenZip.Text + "' , '" + GenPhone.Text + "' ," +
 " '" + GenContact.Text + "' , '" + GenEPAID.Text + "' ), con");

cmd.ExecuteNonQuery();
con.Close();

It looks like when you are creating your SqlCommand, you have the connection as part of the Insert statement.看起来,当您创建 SqlCommand 时,您将连接作为 Insert 语句的一部分。 Specifically, ", con" is still wrapped inside your text string.具体来说,", con" 仍然包含在您的文本字符串中。 If you move your last double quote to after the parenthesis, it should work.如果您将最后一个双引号移到括号之后,它应该可以工作。

However, I would suggest rewriting your code like this:但是,我建议像这样重写您的代码:

using (var con = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=TESTdatabase;Integrated Security=True"))
{
    if(ConnectionState.Closed == con.State) con.Open();
    using (var cmd = con.CreateCommand())
    {
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = $@"INSERT INTO tblGenerator (GeneratorName, GeneratorAddress, GeneratorCity, GeneratorState, GeneratorZip, GeneratorPhone, GeneratorContact, GeneratorEPAID)
                            VALUES ('{GenName.Text}', '{GenAdd.Text}', '{GenCity.Text}', '{GenState.Text}', '{GenZip.Text}', '{GenPhone.Text}', '{GenContact.Text}', '{GenEPAID.Text}')";
        cmd.ExecuteNonQuery();
    }
}

This is the code that I ended up using.这是我最终使用的代码。 Thanks everyone for your help.感谢大家的帮助。

        SqlConnection myConnection =
            new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=TESTdatabase;Integrated Security=True");

        SqlCommand myCommand = new SqlCommand(
            "INSERT into tblGenerator (GeneratorName, GeneratorAddress, GeneratorCity, GeneratorState, GeneratorZip, GeneratorPhone, GeneratorContact, GeneratorEPAID)" +
            "VALUES (@GenName, @GenAdd, @GenCity, @GenState, @GenZip, @GenPhone, @GenContact, @GenEPAID)");
        myCommand.Parameters.AddWithValue("@GenName", GenName.Text);
        myCommand.Parameters.AddWithValue("@GenAdd", GenAdd.Text);
        myCommand.Parameters.AddWithValue("@GenCity", GenCity.Text);
        myCommand.Parameters.AddWithValue("@GenState", GenState.Text);
        myCommand.Parameters.AddWithValue("@GenZip", GenZip.Text);
        myCommand.Parameters.AddWithValue("@GenPhone", GenPhone.Text);
        myCommand.Parameters.AddWithValue("@GenContact", GenContact.Text);
        myCommand.Parameters.AddWithValue("@GenEPAID", GenEPAID.Text);



        myConnection.Open();
        myCommand.Connection = myConnection;
        MessageBox.Show("You Have Successfully Added a New Generator To SQL");
        myCommand.ExecuteNonQuery();
        myConnection.Close();

暂无
暂无

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

相关问题 “ ExecuteNonQuery:连接属性尚未初始化。” - “ExecuteNonQuery: Connection property has not been initialized.” ExecuteNonQuery:连接属性尚未初始化。 - ExecuteNonQuery: Connection property has not been initialized. ExecuteNonQuery:连接属性尚未初始化。 我被困住了 - ExecuteNonQuery: Connection property has not been initialized. I am stuck ExecuteNonQuery:连接属性尚未初始化。 SqlParameter数组 - ExecuteNonQuery: Connection property has not been initialized. Sqlparameter array System.InvalidOperationException:“ExecuteNonQuery:连接属性尚未初始化。” - System.InvalidOperationException: 'ExecuteNonQuery: Connection property has not been initialized.' 错误:ExecuteNonQuery:连接属性尚未初始化。 (尝试使用 SSIS 的脚本任务连接 Snowflake DB) - Error: ExecuteNonQuery: Connection property has not been initialized. (Trying to connect with Snowflake DB using script task of SSIS) 获取“System.InvalidOperationException:'ExecuteNonQuery:连接属性尚未初始化。'” - Getting “System.InvalidOperationException: 'ExecuteNonQuery: Connection property has not been initialized.'” 异常错误“ ExecuteNonQuery:连接属性尚未初始化。”这是我的C#编码 - exception error “ExecuteNonQuery: Connection property has not been initialized.” this is my C# Coding 出现错误“ ExecuteNonQuery:连接属性尚未初始化。”我运行程序 - getting error “ ExecuteNonQuery: Connection property has not been initialized.” wen i run my program 获取以下“ System.InvalidOperationException:'ExecuteNonQuery:连接属性尚未初始化。” - Getting the following “System.InvalidOperationException: 'ExecuteNonQuery: Connection property has not been initialized.'”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM