我在将参数传递给SqlCommand
的SQL字符串时遇到问题。当我使用选项1(见下文)时,代码可以正常工作。当我使用选项2时,它不起作用。我不确定如何使.AddWithValue
方法使用SqlCommand
。
任何帮助将不胜感激!
private string [] GetOrderInfo (string folder)
{
string [] order = new string [] { "date", "order#", "storeid", "storename", "username" };
using (SqlConnection conn = new SqlConnection (_connectionString))
{
conn.Open ();
// Option 1: this line works.
//string sql = "select * from OrderProduct where OrderProductID=26846";
// Option 2: this line doesn't work.
string sql = "select * from OrderProduct where OrderProductID=@folder;";
using (SqlCommand command = new SqlCommand (sql, conn))
{
command.Parameters.AddWithValue ("@folder", folder);
using (SqlDataReader reader = command.ExecuteReader ())
{
while (reader.Read ())
order [1] = Convert.ToString (reader.GetInt32 (1));
}
}
conn.Close ();
} // using (SqlConnection conn = new SqlConnection (connectionString))
return order;
}
答案 0 :(得分:3)
尝试使用
station date he actual forecast as_of
KSBN 2/1/2016 4 10.4 15.1 1/31/2016 6:00
KSBN 2/1/2016 5 12.7 11.32 1/31/2016 6:00
答案 1 :(得分:1)
AddWithValue 方法使用值的类型来定义正确的 SqlDbType 。因此,如果您的字段 OrderProductID 是 INT 的类型,则需要添加 int 。
样品:
command.Parameters.AddWithValue ("@folder", 26846);
另一种简单方法是使用简单对象映射器,如SqlDatabaseCommand或Dapper。
using (var cmd = new SqlDatabaseCommand(_connection))
{
cmd.CommandText.AppendLine(" SELECT * ")
.AppendLine(" FROM EMP ")
.AppendLine(" WHERE EMPNO = @EmpNo ")
.AppendLine(" AND HIREDATE = @HireDate ");
cmd.Parameters.AddValues(new
{
EmpNo = 7369,
HireDate = new DateTime(1980, 12, 17)
});
var emps = cmd.ExecuteTable<Employee>();
}
答案 2 :(得分:0)
您可以尝试:
userService.getUsers().then(function(userList) {
console.log(userList);
})