我正在使用ADO.Net类检索数据。
private DataSet GetData(string clientconstr,string actionparam,string userid)
{
DataSet dsData = null;
SqlParameter[] objSqlParam = new SqlParameter[2];
objSqlParam[0] = new SqlParameter("@ACTION", SqlDbType.VarChar, 50);
objSqlParam[0].Value = actionparam;
objSqlParam[1] = new SqlParameter("@USERID", SqlDbType.VarChar, 100);
objSqlParam[1].Value = userid;
dbc = new dbClass(clientconstr);
dsData = dbc.ExecuteNonQuery("SPLINV", "SP", objSqlParam);
}
class dbClass
{
SqlCommand cmd = null;
SqlConnection con = null;
SqlDataAdapter da = null;
string connectionstring = "";
public dbClass(string conStr)
{
connectionstring = conStr;
}
public DataSet ExecuteNonQuery(string query, string querytype, SqlParameter[] objArrSqlParamas)
{
DataSet ds = null;
try
{
con = new SqlConnection(connectionstring);
con.Open();
cmd = new SqlCommand();
cmd.Connection = con;
if (querytype.Equals("sp"))
cmd.CommandType = CommandType.StoredProcedure;
else
cmd.CommandType = CommandType.Text;
cmd.CommandText = query;
cmd.CommandTimeout = 300;
if (objArrSqlParamas != null)
{
for(int idx=0;idx<objArrSqlParamas.Length;idx++)
cmd.Parameters.Add(objArrSqlParamas[idx]);
}
da = new SqlDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (cmd != null)
cmd.Dispose();
if (da != null)
da.Dispose();
if (con != null)
{
con.Dispose();
con.Close();
}
}
return ds;
}
}
即使我将所有参数传递给SP&#34; SPLINV&#34;,我在GetData函数中遇到错误。
System.Data.SqlClient.SqlException:过程或函数&#39; SPLINV&#39; 期望参数&#39; @ ACTION&#39;,这是未提供的。
&#34; SPLINV&#34; Sp创建如下。
CREATE PROCEDURE SPLINV
(
@ACTION VARCHAR(50),
@USERID VARCHAR(100)
)
我只是想知道为什么会发生这个问题,即使我将所有参数传递给SP以及如何解决这个问题。
答案 0 :(得分:2)
问题在于: - if(querytype.Equals(“sp”))
等于区分大小写。您正在传递“SP”并检查“sp”。所以命令以文本形式执行。请仔细阅读更改的代码。
private DataSet GetData(string clientconstr,string actionparam,string userid)
{
DataSet dsData = null;
SqlParameter[] objSqlParam = new SqlParameter[2];
objSqlParam[0] = new SqlParameter("@ACTION", SqlDbType.VarChar, 50);
objSqlParam[0].Value = actionparam;
objSqlParam[1] = new SqlParameter("@USERID", SqlDbType.VarChar, 100);
objSqlParam[1].Value = userid;
dbc = new dbClass(clientconstr);
dsData = dbc.ExecuteNonQuery("SPLINV", "SP", objSqlParam);
}
class dbClass
{
SqlCommand cmd = null;
SqlConnection con = null;
SqlDataAdapter da = null;
string connectionstring = "";
public dbClass(string conStr)
{
connectionstring = conStr;
}
public DataSet ExecuteNonQuery(string query, string querytype, SqlParameter[] objArrSqlParamas)
{
DataSet ds = null;
try
{
con = new SqlConnection(connectionstring);
con.Open();
cmd = new SqlCommand();
cmd.Connection = con;
if (querytype.Equals("SP"))
cmd.CommandType = CommandType.StoredProcedure;
else
cmd.CommandType = CommandType.Text;
cmd.CommandText = query;
cmd.CommandTimeout = 300;
if (objArrSqlParamas != null)
{
for(int idx=0;idx<objArrSqlParamas.Length;idx++)
cmd.Parameters.Add(objArrSqlParamas[idx]);
}
da = new SqlDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (cmd != null)
cmd.Dispose();
if (da != null)
da.Dispose();
if (con != null)
{
con.Dispose();
con.Close();
}
}
return ds;
}
}
答案 1 :(得分:0)
您是否尝试将参数说明设为“输入”?
objSqlParam [0] .Direction = ParameterDirection.Input; objSqlParam [1] .Direction = ParameterDirection.Input;