【发布时间】:2014-11-02 14:08:47
【问题描述】:
我正在尝试在我的 C# 代码中运行 SQL Select 查询。但我总是在
上得到 -1 输出int result = command.ExecuteNonQuery();
但是,如果我使用 delete 或 insert 使用同一张表...
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();
查询在 SQL Server 上运行良好,但我不明白为什么只有选择查询不起作用。
所有其他查询都在工作。
【问题讨论】:
-
为什么要执行两次命令?
-
正是 Marc 并试图在不执行
Read()方法的情况下读取记录。 -
帮自己一个忙,使用存储过程。
标签: c# sql asp.net sql-server