【发布时间】:2013-11-25 04:19:20
【问题描述】:
我想使用 Database.ExecuteNonQuery 或类似的东西来执行一个 sql 脚本,然后捕获输出。
例如:创建了 xxx 表
我的脚本:
strArray = Regex.Split(streamReader.ReadToEnd(), "\r\nGO");
if (connection.State == ConnectionState.Open)
{
System.Data.SqlClient.SqlCommand cmd;
foreach (string sql in strArray)
{
cmd = new System.Data.SqlClient.SqlCommand(sql);
cmd.Connection = connection;
Console.WriteLine(sql);
Console.WriteLine(cmd.ExecuteNonQuery().ToString());
}
}
【问题讨论】:
-
使用执行而不是执行非查询
-
executenonquery 返回一个整数
The number of rows affected. -
ExecuteNonQuery返回受影响记录的数量,而不是返回某些内容的查询的结果。因此使用ExecuteScalar。
标签: c# command output capture executenonquery