我正在尝试连接到sql server但是我有这个错误:
System.InvalidOperationException:ExecuteNonQuery需要打开和 可用连接。连接的当前状态已关闭。在 System.Data.SqlClient.SqlCommand.ValidateCommand(String方法, 布尔异步) System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion,String methodName,Boolean sendToPipe,Int32 timeout, 布尔asyncWrite)at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()at FattureServer.Form1.button4_Click(Object sender,EventArgs e)
string dbserver2 = textBox4.Text;
string dbname2 = textBox1.Text;
string dbusername2 = textBox2.Text;
string dbpassword2 = textBox3.Text;
SqlConnection conn2 = new SqlConnection("Data Source=" + dbserver + ";Initial Catalog=" + dbname + ";User ID=" + dbusername + ";Password=" + dbpassword + "");
// SqlCommand cmd2 = new SqlCommand("INSERT INTO Cliente (IdCliente,IdUtente,RagioneSociale,Titolo,Indirizzo,Stato,Provincia,Citta,Comune,Cap,Telefono,Email) VALUES(@idcliente,@username,@password, @email)", conn2);
string query2 = "INSERT INTO Cliente (Titolo,RagioneSociale) VALUES(@Titolo,@RagioneSociale)";
SqlCommand myCommand = new SqlCommand(query2, conn2);
myCommand.Parameters.AddWithValue("@Titolo", titolo);
myCommand.Parameters.AddWithValue("@RagioneSociale", ragionesociale);
myCommand.ExecuteNonQuery();
conn2.Close();
如何解决这个错误?
修改
如果我插入conn2.open()我有这个错误:
System.Data.SqlClient.SqlException(0x80131904):无效的对象名称“Cliente”。在System.Data.SqlClient.SqlConnection.OnError(SqlException异常,Boolean breakConnection,Action1 wrapCloseInAction) System.Data.SqlClient.TdsParser.TryRun上System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj,Boolean callerHasConnectionLock,Boolean asyncClose)中的System.Data.SqlClient.SqlInternalConnection.OnError(SqlException异常,Boolean breakConnection,Action1 wrapCloseInAction) (在System.Data.SqlClient.SqlCommand.RunExecuteReaderTds中的System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds,RunBehavior runBehavior,String resetOptionsString)中的RunBehavior runBehavior,SqlCommand cmdHandler,SqlDataReader dataStream,BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj,Boolean& dataReady) (CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,Boolean async,Int32 timeout,Task& task,Boolean asyncWrite,SqlDataReader ds,Boolean describeParameterEncryptionRequest)System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream) ,String方法,TaskCompletionSource1完成,Int32超时,任务& task,Boolean asyncWrite) 在System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource1完成,String methodName,布尔sendToPipe,Int32超时,布尔asyncWrite)在FattureServer.Form1.button4_Click的System.Data.SqlClient.SqlCommand.ExecuteNonQuery()处(Object sender,EventArgs e) )在C:\ Users \ riccardo \ Desktop \ FattureServer \ FattureServer \ Form1.cs:第189行ClientConnectionId:02db8bd4-e91a-4b43-9ba9-e1717c9e96de错误号码:208,状态:1,类:16
答案 0 :(得分:3)
你忘了做conn2.Open();在执行命令之前。
编辑。大卫布拉班特是正确的......你应该把一切都包装在一个使用陈述中。
编辑2.看起来你的表“Cliente”是在DB中创建的。你确定你指向正确的数据库并且该表已经存在吗?
答案 1 :(得分:2)
无效的对象名称'Cliente'
这是另一个问题。 也许,一个错字。客户?检查数据对象以避免拼写错误。