asp.Netmssqlserverストレージ・プロシージャ

6779 ワード

mssql serverはマルチテーブル結果セットを返します
 
mssqlserverコード
create PROCEDURE [dbo].[gd]

AS
BEGIN


    SELECT 1,12

    SELECT 21,22
END

 
 
C#コード
    using (SqlConnection conn = new SqlConnection("Data Source=   ;Initial Catalog=   ;Persist Security Info=True;User ID=   ;Password=  "))
            {
                //       ,    (                      /      )
                SqlDataAdapter da = new SqlDataAdapter("gd", conn);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                //     
                DataTable dt = new DataTable();
                DataSet ds = new DataSet();
                //         
                da.Fill(ds);
                conn.Close();
                da.SelectCommand.Dispose();
                da.SelectCommand.Parameters.Clear();

                
            }

 
アクセス形式
                string tab1row1col1 = ds.Tables[0].Rows[0][0].ToString();

                string tab1row1col2 = ds.Tables[0].Rows[0][1].ToString();

                string tab2row1col1 = ds.Tables[1].Rows[0][0].ToString();

                string tab2row1col2 = ds.Tables[1].Rows[0][1].ToString();

 
mssql serverは、単一テーブル結果セットを返します.
 
C#コード
 
  using (SqlConnection conn = new SqlConnection("Data Source=   ;Initial Catalog=   ;Persist Security Info=True;User ID=   ;Password=  "))
            {
                SqlDataAdapter da = new SqlDataAdapter("gd", conn);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                //     
                DataTable dt = new DataTable();
                //         
                da.Fill(dt);
                conn.Close();
                da.SelectCommand.Dispose();
                da.SelectCommand.Parameters.Clear();

            }

 
mssql server return戻り
 
mssqlserverコード
create PROCEDURE [dbo].[gd_return]

AS
BEGIN


    return '123'

    
END

 
C#コード
  using (SqlConnection conn = new SqlConnection("        "))
            {
                SqlParameter[] cmdParms = {new SqlParameter("@return",SqlDbType.VarChar)};

                cmdParms[0].Direction= ParameterDirection.ReturnValue;

                SqlCommand sqlCommand = new SqlCommand("gd_return", conn);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.Parameters.AddRange(cmdParms);
                conn.Open();
                sqlCommand.ExecuteNonQuery();
                object bj = cmdParms[0].Value;
                string result = bj.ToString();
            }

 
mssql server output戻り
mssqlserverコード
create PROCEDURE [dbo].[gd_output]
   @test1 int output,
   @test2 int output
AS
BEGIN

   set @test1=1;

   set @test2=2;

    
END

C#コード
using (SqlConnection conn = new SqlConnection("        "))
            {
                SqlParameter[] cmdParms = {new SqlParameter("@test1",SqlDbType.Int),
                                          new SqlParameter("@test2",SqlDbType.Int)
                                          };

                cmdParms[0].Direction= ParameterDirection.Output;
                cmdParms[1].Direction = ParameterDirection.Output;

                SqlCommand sqlCommand = new SqlCommand("gd_output", conn);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.Parameters.AddRange(cmdParms);
                conn.Open();
                sqlCommand.ExecuteNonQuery();
                object ob1 = cmdParms[0].Value;
                object ob2 = cmdParms[0].Value;
                string result1 = ob1.ToString();
                string result2 = ob2.ToString();

            }