Well, the ExecuteNonQuery method is there for statements for changing data, ie. DELETE / UPDATE /INSERT, and the returned value are the number of rows affected by that statement. When checking the documentation we can see that there are some conditions that return -1. ...
Although theExecuteNonQueryreturns no rows, any output parameters or return values mapped to parameters are populated with data. For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. For all other types of statements, the return value is -1...
前面一篇文章,我们介绍了如何在c#中对数据库进行更新操作。主要是利用SqlCommand 对象的ExecuteNonQuery方法。 这篇文章介绍,如何进行查询操作。本文给出的例子仍然是针对sql server数据库的。对于其它数据库(源),区别只是引入的部门api的不同,但流程和方法是一样的。
cmd.Parameters.Add("@Id", SqlDbType.Int); //添加参数,说明类型 cmd.Parameters["@Id"].Value = 1; //设置参数值 conn.Open(); //打开连接 int i = cmd.ExecuteNonQuery(); //执行命令,ExecuteNonQuery由名称看出,只能用于非查询语句 conn.Close(); //关闭连接 Console.WriteLine(i); //输出影响行...
();returncmd.ExecuteNonQuery(); } } }// Set the connection, command, and then execute the command and only return one value.publicstaticObjectExecuteScalar(String connectionString, String commandText, CommandType commandType,paramsSqlParameter[] parameters){using(SqlConnection conn =newSqlConnection(...
cmd.CommandType = commandType; cmd.Parameters.AddRange(parameters); conn.Open(); return cmd.ExecuteNonQuery(); } } } // Set the connection, command, and then execute the command and only return one value. public static Object ExecuteScalar(String connectionString, String commandText, CommandType ...
carriage return values for C#.net Case insensitive Replace cast from double to decimal Cast Interface to class Cast to Enum issue when value is null Casting an Int16 varible to Int in C# produces a runtime "Specified cast is not valid" exception casting from object to System.Reflection.Proper...
cmd.CommandType = commandType; cmd.Parameters.AddRange(parameters); conn.Open(); return cmd.ExecuteNonQuery(); } } } // Set the connection, command, and then execute the command and only return one value. public static Object ExecuteScalar(String connectionString, String commandText, CommandTy...
();returncmd.ExecuteNonQuery(); } } }// Set the connection, command, and then execute the command and only return one value.publicstaticObjectExecuteScalar(String connectionString, String commandText, CommandType commandType,paramsSqlParameter[] parameters){using(SqlConnection conn =newSqlConnection(...
SqlCommand cmd = new SqlCommand("insert into mynews value ('插入一条新数据')", con); Command对象的构造函数的参数有两个,一个是需要执行的SQL语句,另一个是数据库连接对象。创建Command对象后,就可以执行SQL命令,执行后完成并关闭数据连接,示例代码如下所示。 cmd.ExecuteNonQuery(); //执行SQL命令 con....