选择查询以从SQL Server获取数据

use*_*440 14 c# sql sql-server asp.net

我试图在我的C#代码中运行SQL Select查询.但我总是得到-1输出

int result = command.ExecuteNonQuery();
Run Code Online (Sandbox Code Playgroud)

但是,如果我使用deleteinsert工作相同的表...

ConnectString 也没关系.

请检查以下代码

SqlConnection conn = new SqlConnection("Data Source=;Initial Catalog=;Persist Security Info=True;User ID=;Password=");
conn.Open();

SqlCommand command = new SqlCommand("Select id from [table1] where name=@zip", conn);

//command.Parameters.AddWithValue("@zip","india");
int result = command.ExecuteNonQuery();

// result gives the -1 output.. but on insert its 1
using (SqlDataReader reader = command.ExecuteReader())
{
    // iterate your results here
    Console.WriteLine(String.Format("{0}",reader["id"]));
}

conn.Close();
Run Code Online (Sandbox Code Playgroud)

查询在SQL Server上运行正常,但我不知道为什么只有select查询不起作用.

所有其他查询都有效.

Dhw*_*ani 35

SqlCommand.ExecuteNonQuery方法

您可以使用ExecuteNonQuery执行目录操作(例如,查询数据库的结构或创建数据库对象,例如表),或者通过执行UPDATE,INSERT或DELETE语句来更改数据库中的数据而不使用DataSet.虽然ExecuteNonQuery不返回任何行,但是映射到参数的任何输出参数或返回值都会填充数据.对于UPDATE,INSERT和DELETE语句,返回值是受命令影响的行数.当插入或更新的表上存在触发器时,返回值包括插入或更新操作影响的行数以及受触发器或触发器影响的行数.对于所有其他类型的语句,返回值为-1.如果发生回滚,则返回值也为-1.

SqlCommand.ExecuteScalar方法 对连接执行Transact-SQL语句并返回受影响的行数.

所以没有.SELECT语句返回的语句必须使用ExecuteScalar方法.

参考:http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery(v=vs.110).aspx

所以尝试下面的代码:

SqlConnection conn = new SqlConnection("Data Source=;Initial Catalog=;Persist Security Info=True;User ID=;Password=");
conn.Open();

SqlCommand command = new SqlCommand("Select id from [table1] where name=@zip", conn);
command.Parameters.AddWithValue("@zip","india");
 // int result = command.ExecuteNonQuery();
using (SqlDataReader reader = command.ExecuteReader())
{
  if (reader.Read())
  {
     Console.WriteLine(String.Format("{0}",reader["id"]));
   }
}

conn.Close();
Run Code Online (Sandbox Code Playgroud)

  • 不需要关闭 `reader`,并且 `reader.Close();` 行是多余的,因为 `reader` 将通过 `using (SqlDataReader reader = ...` 关闭) (2认同)

Dmi*_*nko 9

根据MSDN

http://msdn.microsoft.com/ru-ru/library/system.data.sqlclient.sqlcommand.executenonquery(v=vs.110).aspx

结果是受影响的行数,并且因为您的查询select 无论如何都不会影响(即插入,删除或更新)行.

如果要返回查询的单行,请使用ExecuteScalar()而不是ExecuteNonQuery():

  int result = (int) (command.ExecuteScalar());
Run Code Online (Sandbox Code Playgroud)

但是,如果您希望返回许多行,ExecuteReader()则唯一的选择是:

  using (SqlDataReader reader = command.ExecuteReader()) {
    while (reader.Read()) {
      int result = reader.GetInt32(0);

      ...
    }
  }
Run Code Online (Sandbox Code Playgroud)


Vik*_*ana 5

you can use ExecuteScalar() in place of ExecuteNonQuery() to get a single result use it like this

Int32 result= (Int32) command.ExecuteScalar();
Console.WriteLine(String.Format("{0}", result));
Run Code Online (Sandbox Code Playgroud)

It will execute the query, and returns the first column of the first row in the result set returned by the query. Additional columns or rows are ignored.

As you want only one row in return, remove this use of SqlDataReader from your code

using (SqlDataReader reader = command.ExecuteReader())
{
    // iterate your results here
    Console.WriteLine(String.Format("{0}",reader["id"]));
}
Run Code Online (Sandbox Code Playgroud)

because it will again execute your command and effect your page performance.