我的存储过程声明如下所示:
alter procedure [dbo].[usp_A_MySproc]
@aID int,
@bID int,
@cID int
我的应用层代码:
using (var transaction = Session.Transaction)
{
try
{
transaction.Begin();
Session.CreateSQLQuery("exec usp_A_MySproc ?, ?, ?")
.SetParameter("aID", 1)
.SetParameter("bID", 2)
.SetParameter("cID", 3);
transaction.Commit();;
}
}
异常:“无法找到命名参数[aID]”
你能看错吗?
谢谢!
答案 0 :(得分:3)
nhibernate
Session.CreateSQLQuery("exec usp_A_MySproc @aID = 1, @bID =2, @cID = 3")
或
IQuery query = Session.CreateSQLQuery("exec usp_A_MySproc @aID =:aID, @bID =:bID , @cID =:cID");
query.SetInt32("aID",1);
query.SetInt32("bID",2);
query.SetInt32("cID",3);
query.ExecuteUpdate();
或与你正在做的事情相匹配
Session.CreateSQLQuery("exec usp_A_MySproc @aID =:aID, @bID =:bID , @cID =:cID")
.SetParameter("aID", 1)
.SetParameter("bID", 2)
.SetParameter("cID", 3);
要使用SqlCommand
修改以下代码,connectionString
代表您的连接字符串
using (SqlConnection con = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand("usp_A_MySproc ", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@aID", SqlDbType.Int32).Value = 1;
cmd.Parameters.Add("@bID", SqlDbType.Int32).Value = 2;
cmd.Parameters.Add("@cID", SqlDbType.Int32).Value = 3;
try
{
con.Open();
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
或
using (SqlConnection con = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand("usp_A_MySproc ", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@aID", 1) ;
cmd.Parameters.AddWithValue("@bID", 2) ;
cmd.Parameters.AddWithValue("@cID", 3) ;
try
{
con.Open();
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
参考有关SQLCommand的MSDN文章如何正确添加参数
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters(v=vs.110).aspx
答案 1 :(得分:1)
您需要按位置设置参数:
using (var transaction = Session.BeginTransaction())
{
Session.CreateSQLQuery("exec usp_A_MySproc ?, ?, ?")
.SetInt32(0, aIDValue)
.SetInt32(1, bIDValue)
.SetInt32(2, cIDValue)
.ExecuteUpdate();
transaction.Commit();
}