2012-03-31 11 views
1

私はパラメータを持つNameValueCollectionを持っています。 Spを実行するためにSqlCommandのすべてのパラメータを渡したいと思います。 誰でもこれを手伝ってもらえますか?SqlCommand C#でNameValueCollectionを渡す方法は?

+0

のforeach/AddWithValue、ないための機能を作成し、接続文字列

//Connection String for LocalHost public static string SQLConnectionString = @"Data Source=Manoj;Initial Catalog=TMS;Integrated Security = true"; 

を作成します。 ? –

答えて

0

そして、ビジネスロジックからこの関数は以下のようである実行コマンド

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); 
    } 
関連する問題