'ExecuteNonQuery: Connection property has not been initialized.'
'ExecuteNonQuery: Connection property has not been initialized.'
我搜索了这个论坛,并尝试了很多我找到的可能的解决方案,但没有任何效果。任何人都可以阐明这种情况吗?谢谢!
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();
看起来您在创建 SqlCommand 时,将连接作为 Insert 语句的一部分。具体来说,“, con”仍然包含在您的文本字符串中。如果您将最后一个双引号移到括号之后,它应该可以工作。
但是,我建议您像这样重写您的代码:
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();
}
}
这是我最终使用的代码。谢谢大家的帮助。
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();
我搜索了这个论坛,并尝试了很多我找到的可能的解决方案,但没有任何效果。任何人都可以阐明这种情况吗?谢谢!
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();
看起来您在创建 SqlCommand 时,将连接作为 Insert 语句的一部分。具体来说,“, con”仍然包含在您的文本字符串中。如果您将最后一个双引号移到括号之后,它应该可以工作。
但是,我建议您像这样重写您的代码:
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();
}
}
这是我最终使用的代码。谢谢大家的帮助。
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();