Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.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# Excute非查询无效操作异常_C#_Visual Studio_Error Handling - Fatal编程技术网

C# Excute非查询无效操作异常

C# Excute非查询无效操作异常,c#,visual-studio,error-handling,C#,Visual Studio,Error Handling,我正在尝试连接到sql server,但出现以下错误: System.InvalidOperationException:ExecuteOnQuery需要打开和关闭的 可用连接。连接的当前状态为关闭。在 System.Data.SqlClient.SqlCommand.ValidateCommand(字符串方法, 布尔异步)at System.Data.SqlClient.SqlCommand.InternalExecuteOnQuery(TaskCompletionSource`1 完成,字符

我正在尝试连接到sql server,但出现以下错误:

System.InvalidOperationException:ExecuteOnQuery需要打开和关闭的 可用连接。连接的当前状态为关闭。在 System.Data.SqlClient.SqlCommand.ValidateCommand(字符串方法, 布尔异步)at System.Data.SqlClient.SqlCommand.InternalExecuteOnQuery(TaskCompletionSource`1 完成,字符串方法名,布尔sendToPipe,Int32超时, 布尔异步(写)在 System.Data.SqlClient.SqlCommand.ExecuteNonQuery()位于 FattureServer.Form1.button4\u单击(对象发送者,事件参数e)

如何解决这个错误

编辑:

如果插入conn2.open(),则会出现以下错误:

System.Data.SqlClient.SqlException(0x80131904):对象名称“Cliente”无效。位于System.Data.SqlClient.SqlConnection.OneError(SqlException异常、布尔断开连接、Action1 wrapCloseInAction) System.Data.SqlClient.TdsParser.ThroweException和System.Data.SqlClient.TdsParser.ThroweException的System.Data.SqlClient.SqlInternalConnection.OneError(SqlException异常、布尔断开连接、Action1 wrapCloseInAction)和System.Data.SqlClient.TdsParser.TryRun的警告(TdsParserStateObject stateObj、布尔调用连接锁、布尔异步关闭)在System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds,RunBehavior RunBehavior,SqlDataReader dataStream,BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject StateObject,Boolean和dataReady)中的System.Data.SqlClient.SqlCommand.RunExecuteReader(SqlDataReader ds,RunBehavior RunBehavior,String ResetOptionString)(CommandBehavior cmdBehavior、RunBehavior RunBehavior、Boolean returnStream、Boolean async、Int32超时、任务和任务、Boolean asyncWrite、SqlDataReader ds、Boolean describeParameterEncryptionRequest)位于System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior、RunBehavior RunBehavior、布尔返回流、字符串方法、TaskCompletionSource1完成、Int32超时、任务和任务、布尔异步写入) 位于System.Data.SqlClient.SqlCommand.InternalExecuteOnQuery(TaskCompletionSource1 completion,String methodName,Boolean sendToPipe,Int32超时,Boolean asyncWrite)的System.Data.SqlClient.SqlCommand.ExecuteOnQuery()位于FattureServer.Form1.button4_单击(对象发送者,事件参数e)在C:\Users\riccardo\Desktop\FattureServer\FattureServer\Form1.cs中:第189行客户端连接ID:02db8bd4-e91a-4b43-9ba9-e1717c9e96de错误号:208,状态:1,类别:16


在执行命令之前,您忘记执行conn2.Open();命令

David Brabant是正确的…您应该将所有内容包装在using语句中

编辑2。看起来您的表“Cliente”是在数据库中创建的。您确定您指向的是正确的数据库并且该表已经存在吗

无效的对象名称“客户端”

这是另一个问题。
可能是键入错误的.Client?检查数据对象以避免键入错误。

在执行命令之前,您忘记执行conn2.Open();是否询问如何解决“ExecuteOnQuery需要打开且可用的连接。连接的当前状态为关闭”真的吗?你在哪里打开你的连接?连接是一次性的。你应该用“使用”来封装你的呼叫.桌子的其余部分看起来是意大利人。因此,
Cliente
应该是正确的。
IdCliente,idtente,RagioneSociale,Titolo,Indirizzo,Stato,Provincia,Citta,Comune,Cap,Telefono,Email
是的,他已经看过了。但他可以确定地检查一下。
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();