调用 sqlhelper.cs 的例子(存储过程返回多个参数)

SqlConnection connection = null;
try
   {
    try
    {
     connection = GetConnection(txtConnectionString.Text);
    }
    catch
    {
     MessageBox.Show("The connection with the database can磘 be established", "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     return;
    }
    // Set up parameters (1 input and 3 output)
    SqlParameter[] arParms = new SqlParameter[4];
   
    // @ProductID Input Parameter
    // assign = 1
    arParms[0] = new SqlParameter("@ProductID", SqlDbType.Int );
    arParms[0]. = 1;               

    // @ProductName Output Parameter
    arParms[1] = new SqlParameter("@ProductName", SqlDbType.NVarChar, 40);
    arParms[1].Direction = ParameterDirection.Output;

    // @UnitPrice Output Parameter
    arParms[2] = new SqlParameter("@UnitPrice", SqlDbType.Money);
    arParms[2].Direction = ParameterDirection.Output;

    // @QtyPerUnit Output Parameter
    arParms[3] = new SqlParameter("@QtyPerUnit", SqlDbType.NVarChar, 20);
    arParms[3].Direction = ParameterDirection.Output;

    // Call ExecuteNonQuery static method of SqlHelper class
    // We pass in database connection string, command type, stored procedure name and an array of SqlParameter objects
    SqlHelper.ExecuteNonQuery(connection, CommandType.StoredProcedure, "getProductDetails", arParms);
    
    // Display results in text box using the s of output parameters 
    txtResults.Text = arParms[1].  + ", " + arParms[2].  + ", " + arParms[3].;
   }
   catch(Exception ex)
   {
    string errMessage = "";
    for( Exception tempException = ex; tempException != null ; tempException = tempException.InnerException )
    {
     errMessage += tempException.Message + Environment.NewLine + Environment.NewLine;
    }

    MessageBox.Show( string.Format( "There are some problems while trying to use the Data Access Application block, please check the following error messages: {0}"
     + Environment.NewLine + "This test requires some modifications to the Northwind database. Please make sure the database has been initialized using the SetUpDataBase.bat database , or from the Install Quickstart option on the Start menu.", errMessage ),
     "Application error", MessageBoxButtons.OK, MessageBoxIcon.Error );
   }
   finally
   {
    if(connection != null)
     connection.Dispose();
   }

你可能感兴趣的:(存储过程)