C#中如何使用DataAdapter调用可变参数的存储过程
How to use DataAdapter to call a stored procedure in C# with variable parameters
我在 C# 中调用以下代码以使用给定的存储过程 "sp1_name" 填充 dataAdapter。问题是我想用不同的参数调用不同的存储过程。 (所有 SP 都做 SELECT)
假设我的存储过程名称是 "SP_SOMESP",那么一切正常。
假设我的存储过程名称是"SP_SOMESP @Month= 10, @Year = 2010",那么它不起作用。抛出找不到这个存储过程的异常。
有什么解决办法吗?
谢谢!
//First Connection - SP1
using (SqlConnection con = new SqlConnection(conStr))
{
using (SqlCommand cmd = new SqlCommand(sp1_name, con)) //sp1_name = NAME + PARAMETERS
{
cmd.CommandTimeout = 3600;
cmd.CommandType = CommandType.StoredProcedure;
using (SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd))
{
dataAdapter.Fill(results2);
}
}
}
您必须以编程方式添加参数,请参阅 SqlCommand.Parameters。
会是这样的
cmd.Parameters.AddWithValue("@Month", 10);
cmd.Parameters.AddWithValue("@Year", 2010);
这将是在命令声明之后和执行之前。
如果你发现需要delcare数据类型,那么试试这个
cmd.Parameters.Add("@Month", SqlDbType.Int).Value = 10;
检查这个,
using (SQLCommand cmd = new SQLCommand())
{
cmd.CommandText = "SP_SOMESP";
cmd.Parameters.Add("@Month", 10);
cmd.Parameters.Add("@Year", 2010);
cmd.CommandTimeout = 3600;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;
}
using (SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd))
{
dataAdapter.SelectCommand = cmd;
dataAdapter.Fill(results2);
}
第一期:
存储过程中的参数不应与其名称一起包含
第二期:
在存储过程的名称中使用 space 不是一个好习惯。
对于隐藏代码
using(SqlConnection con = new SqlConnection("Your Connection String Here"))
{
SqlCommand cmd = new SqlCommand("sp_SomeName", con);
cmd.CommandType = CommandType.StoredProcedure;
//the 2 codes after this comment is where you assign value to the parameters you
//have on your stored procedure from SQL
cmd.Parameters.Add("@MONTH", SqlDbType.VarChar).Value = "someValue";
cmd.Parameters.Add("@YEAR", SqlDbType.VarChar).Value = "SomeYear";
SqlDataAdapter da = new SqlDataAdapter(cmd);
SqlDataSet ds = new SqlDataSet();
da.Fill(ds); //this is where you put values you get from the Select command to a
//dataset named ds, reason for this is for you to fetch the value from DB to code behind
foreach(DataRow dr in ds.Tables[0].Rows) // this is where you run through the dataset and get values you want from it.
{
someTextBox.Text = dr["Month"].ToString(); //you should probably know this code
}
}
我在 C# 中调用以下代码以使用给定的存储过程 "sp1_name" 填充 dataAdapter。问题是我想用不同的参数调用不同的存储过程。 (所有 SP 都做 SELECT) 假设我的存储过程名称是 "SP_SOMESP",那么一切正常。
假设我的存储过程名称是"SP_SOMESP @Month= 10, @Year = 2010",那么它不起作用。抛出找不到这个存储过程的异常。
有什么解决办法吗?
谢谢!
//First Connection - SP1
using (SqlConnection con = new SqlConnection(conStr))
{
using (SqlCommand cmd = new SqlCommand(sp1_name, con)) //sp1_name = NAME + PARAMETERS
{
cmd.CommandTimeout = 3600;
cmd.CommandType = CommandType.StoredProcedure;
using (SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd))
{
dataAdapter.Fill(results2);
}
}
}
您必须以编程方式添加参数,请参阅 SqlCommand.Parameters。
会是这样的
cmd.Parameters.AddWithValue("@Month", 10);
cmd.Parameters.AddWithValue("@Year", 2010);
这将是在命令声明之后和执行之前。
如果你发现需要delcare数据类型,那么试试这个
cmd.Parameters.Add("@Month", SqlDbType.Int).Value = 10;
检查这个,
using (SQLCommand cmd = new SQLCommand())
{
cmd.CommandText = "SP_SOMESP";
cmd.Parameters.Add("@Month", 10);
cmd.Parameters.Add("@Year", 2010);
cmd.CommandTimeout = 3600;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;
}
using (SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd))
{
dataAdapter.SelectCommand = cmd;
dataAdapter.Fill(results2);
}
第一期:
存储过程中的参数不应与其名称一起包含
第二期:
在存储过程的名称中使用 space 不是一个好习惯。
对于隐藏代码
using(SqlConnection con = new SqlConnection("Your Connection String Here"))
{
SqlCommand cmd = new SqlCommand("sp_SomeName", con);
cmd.CommandType = CommandType.StoredProcedure;
//the 2 codes after this comment is where you assign value to the parameters you
//have on your stored procedure from SQL
cmd.Parameters.Add("@MONTH", SqlDbType.VarChar).Value = "someValue";
cmd.Parameters.Add("@YEAR", SqlDbType.VarChar).Value = "SomeYear";
SqlDataAdapter da = new SqlDataAdapter(cmd);
SqlDataSet ds = new SqlDataSet();
da.Fill(ds); //this is where you put values you get from the Select command to a
//dataset named ds, reason for this is for you to fetch the value from DB to code behind
foreach(DataRow dr in ds.Tables[0].Rows) // this is where you run through the dataset and get values you want from it.
{
someTextBox.Text = dr["Month"].ToString(); //you should probably know this code
}
}