我正在尝试使用以下查询,但它没有给出预期的结果。 以下是我的询问:
string Rapdate = Convert.ToString(db.ExecuteNonQuery("select top(1) convert(varchar(50),PrcT_Date,106) from PriceMasterTemp",CommandType.Text));
它返回-1而不是返回日期。 可能是什么问题?
答案 0 :(得分:0)
使用 EXecuteReader ()代替ExecuteNonQuery
因为 ExecuteNonQuery 用于插入,更新和删除查询
EXecuteReader 用于从表
中检索值例如:
String sql = "select top(1) convert(varchar(50),PrcT_Date,106) from PriceMasterTemp";
SqlCommand cmd = new SqlCommand(sql,conn);
ResultLabel.Text = (String) cmd.ExecuteScalar();
答案 1 :(得分:0)
来自MSDN关于ExecuteNonQuery
Executes a Transact-SQL statement against the connection and returns the **number of rows affected**
此方法仅返回受影响的行数,如果您想获取日期值,并且只有一个值,则可以使用ExecuteScalar方法
SqlCommand cmd = new SqlCommand("select top(1) convert(varchar(50),PrcT_Date,106) from PriceMasterTemp",connetcion)
string date = (string)cmd.ExecuteScalar();