我有一个带参数的NameValueCollection。我想传递SqlCommand中的所有参数来执行Sp。 任何人都可以帮我吗?
答案 0 :(得分:0)
创建连接字符串
//Connection String for LocalHost
public static string SQLConnectionString = @"Data Source=Manoj;Initial Catalog=TMS;Integrated Security = true";
然后创建执行命令的函数
public DataSet ExecuteDataSetSP(string spName, NameValueCollection paramNameValue, Boolean withOutPara)
{
SqlDataAdapter daDataAdapter = new SqlDataAdapter();
DataSet dsDataSet = new DataSet();
IEnumerator iEnum = default(IEnumerator);
if (paramNameValue != null)
{
iEnum = paramNameValue.GetEnumerator();
}
// Initialize the Exception ...
try
{
// Prepare DataAdapter ...
daDataAdapter.SelectCommand = new SqlCommand();
daDataAdapter.SelectCommand.Connection = cnnConnection;
daDataAdapter.SelectCommand.CommandText = spName;
daDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
// Pass Parameters ...
if (paramNameValue != null)
{
while (iEnum.MoveNext())
{
daDataAdapter.SelectCommand.Parameters.AddWithValue(iEnum.Current.ToString(), paramNameValue.GetValues(iEnum.Current.ToString()).GetValue(0).ToString().Trim());
}
}
if (withOutPara == true)
{
// Add output parameters ...
daDataAdapter.SelectCommand.Parameters.Add("@o_ErrorMesg", SqlDbType.VarChar, 250).Direction = ParameterDirection.Output;
}
// Open Database Connection ...
cnnConnection.Open();
// Fill the DataSet ...
daDataAdapter.Fill(dsDataSet);
// Read values of Output Parameters
if (withOutPara == true)
{
// Read Values of Output Paramters and Stored into Property
mErrorMsg = (string)(daDataAdapter.SelectCommand.Parameters["@o_ErrorMesg"].Value);
}
else
{
mErrorMsg = "";
}
}
catch (Exception exception)
{
// Set the Exception ...
mException = exception;
dsDataSet = null;
}
finally
{
if (cnnConnection.State == ConnectionState.Open) cnnConnection.Close();
}
return dsDataSet;
}
public void ExecuteSP(string spName, NameValueCollection paramNameValue, Boolean withOutPara)
{
SqlCommand cmdSQLCommand = new SqlCommand();
IEnumerator iEnum = default(IEnumerator);
if (paramNameValue != null)
{
iEnum = paramNameValue.GetEnumerator();
}
try
{
// Prepare DataAdapter ...
cmdSQLCommand.Connection = cnnConnection;
cmdSQLCommand.CommandText = spName;
cmdSQLCommand.CommandType = CommandType.StoredProcedure;
// Pass Parameters ...
if (paramNameValue != null)
{
while (iEnum.MoveNext())
{
cmdSQLCommand.Parameters.AddWithValue(iEnum.Current.ToString(), paramNameValue.GetValues(iEnum.Current.ToString()).GetValue(0).ToString().Trim());
}
}
if (withOutPara == true)
{
// Add output parameters ...
cmdSQLCommand.Parameters.Add("@o_ErrorMesg", SqlDbType.VarChar, 250).Direction = ParameterDirection.Output;
}
// Open Database Connection ...
cnnConnection.Open();
cmdSQLCommand.ExecuteNonQuery();
// Read values of Output Parameters
if (withOutPara == true)
{
// Read Values of Output Paramters and Stored into Property
mErrorMsg = (string)(cmdSQLCommand.Parameters["@o_ErrorMesg"].Value);
}
else
{
mErrorMsg = "";
}
}
catch (Exception exception)
{
// Set the Exception ...
mException = exception;
if (cnnConnection.State == ConnectionState.Open) cnnConnection.Close();
}
finally
{
if (cnnConnection.State == ConnectionState.Open) cnnConnection.Close();
}
}
调用Business Logic中的此功能就像....
public void Insert(string UserName, string Password, string Name)
{
NameValueCollection para = new NameValueCollection();
para.Add("@i_UserName", UserName);
para.Add("@i_Password", Password);
para.Add("@i_Name", Name);
DataAccess.DataAccess objDA = new DataAccess.DataAccess();
objDA.ExecuteSP("usp_User_Insert", para, true);
}