對於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);
}
}
參考MSDN文章的SqlCommand如何正確添加參數
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters(v=vs.110).aspx
非常感謝! :) – Henrik