1
我有一個NameValueCollection參數。我想通過SqlCommand中的所有參數來執行Sp。 任何人都可以幫助我嗎?如何在SqlCommand C#中傳遞NameValueCollection?
我有一個NameValueCollection參數。我想通過SqlCommand中的所有參數來執行Sp。 任何人都可以幫助我嗎?如何在SqlCommand C#中傳遞NameValueCollection?
創建連接字符串
//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();
}
}
呼叫此業務邏輯功能就像是....
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);
}
的foreach/AddWithValue,無? – 2012-03-31 15:54:20