程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#挪用存儲進程詳解(帶前往值、參數輸出輸入等)

C#挪用存儲進程詳解(帶前往值、參數輸出輸入等)

編輯:C#入門知識

C#挪用存儲進程詳解(帶前往值、參數輸出輸入等)。本站提示廣大學習愛好者:(C#挪用存儲進程詳解(帶前往值、參數輸出輸入等))文章只能為提供參考,不一定能成為您想要的結果。以下是C#挪用存儲進程詳解(帶前往值、參數輸出輸入等)正文


本文實例講述了C#挪用存儲進程的辦法。分享給年夜家供年夜家參考,詳細以下:

CREATE PROCEDURE [dbo].[GetNameById]
 @studentid varchar(8),
 @studentname nvarchar(50) OUTPUT
AS
BEGIN
 SELECT @studentname=studentname FROM student
  WHERE studentid=@studentid
 if @@Error<>0
 RETURN -1
 else
 RETURN 0
END

using (SqlConnection conn = new SqlConnection(connStr))
{
  try
  {
    SqlCommand cmd = new SqlCommand("GetNameById", conn);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@studentid", "09888888");  //給輸出參數賦值
    SqlParameter parOutput =cmd.Parameters.Add("@studentname", SqlDbType.NVarChar, 50);  //界說輸入參數
    parOutput.Direction = ParameterDirection.Output;  //參數類型為Output
    SqlParameter parReturn = new SqlParameter("@return", SqlDbType.Int);
    parReturn.Direction = ParameterDirection.ReturnValue;   //參數類型為ReturnValue
    cmd.Parameters.Add(parReturn);
    conn.Open();
    cmd.ExecuteNonQuery();
    MessageBox.Show(parOutput.Value.ToString());  //顯示輸入參數的值
    MessageBox.Show(parReturn.Value.ToString());  //顯示前往值
  }
  catch (System.Exception ex)
  {
    MessageBox.Show(ex.Message);
  }
}

Create PROCEDURE AddOrderTran
  @country nvarchar(100),
  @adds nvarchar(100),
  @ynames nvarchar(100),
  @pids nvarchar(100),
  @cellp nvarchar(100),
  @cphone nvarchar(100),
  @amounts nvarchar(100),
  @cartnumber nvarchar(100)
as
  Declare @id int
  BEGIN TRANSACTION
    insert into Orders(Order_Country,Order_Adress,Order_UserName,Order_PostID,Cells,Order_Phone,Total_pay,CartNumber,IsPay)
      values (@country,@adds,@ynames,@pids,@cellp,@cphone,@amounts,@cartnumber,'0')
    Select @id=@@identity
    insert into Orders_Item (OrderNumber,ProductsID,Products_Color,Products_Price,Order_Qty,Item_Total)
      select @id,Carts_Item.ProductsID,Carts_Item.Products_Color,Carts_Item.Products_Price,Carts_Item.Item_Qty,Carts_Item.Total_Pay
      from Carts_Item where Carts_Item.CartNumber=@cartnumber
    delete Carts_Item where CartNumber=@cartnumber
    IF @@error <> 0 --產生毛病
    BEGIN
      ROLLBACK TRANSACTION
      RETURN 0
    END
    ELSE
    BEGIN
      COMMIT TRANSACTION
      RETURN @id  --履行勝利
  END

#region 履行存儲進程
SqlParameter[] param = new SqlParameter[]
{
   new SqlParameter("@country",country),
   new SqlParameter("@adds",adds),
   new SqlParameter("@ynames",ynames),
   new SqlParameter("@pids", pids),
   new SqlParameter("@cellp",cellp),
   new SqlParameter("@cphone", cphone),
   new SqlParameter("@amounts",amounts),
   new SqlParameter("@cartnumber",cartnumber),
   new SqlParameter("@return",SqlDbType.Int)
};
param[8].Direction = ParameterDirection.ReturnValue;
MSCL.SqlHelper.RunProcedure("AddOrderTran", param);
object obj = param[8].Value; //接收前往值
//string connStr = System.Configuration.ConfigurationManager.AppSettings["ConStr"].ToString();
//using (SqlConnection conn = new SqlConnection(connStr))
//{
//  conn.Open();
//  SqlCommand cmd = new SqlCommand("AddOrderTran", conn);
//  cmd.CommandType = CommandType.StoredProcedure;
//  SqlParameter para1 = new SqlParameter("@country", country);
//  para1.Direction = ParameterDirection.Input; //參數偏向 為輸出參數
//  cmd.Parameters.Add(para1);
//  SqlParameter para2 = new SqlParameter("@adds", adds);
//  para2.Direction = ParameterDirection.Input;
//  cmd.Parameters.Add(para2);
//  SqlParameter para3 = new SqlParameter("@ynames", ynames);
//  para3.Direction = ParameterDirection.Input;
//  cmd.Parameters.Add(para3);
//  SqlParameter para4 = new SqlParameter("@pids", pids);
//  para4.Direction = ParameterDirection.Input;
//  cmd.Parameters.Add(para4);
//  SqlParameter para5 = new SqlParameter("@cellp", cellp);
//  para5.Direction = ParameterDirection.Input;
//  cmd.Parameters.Add(para5);
//  SqlParameter para6 = new SqlParameter("@cphone", cphone);
//  para6.Direction = ParameterDirection.Input;
//  cmd.Parameters.Add(para6);
//  SqlParameter para7 = new SqlParameter("@amounts", amounts);
//  para7.Direction = ParameterDirection.Input;
//  cmd.Parameters.Add(para7);
//  SqlParameter para8 = new SqlParameter("@cartnumber", cartnumber);
//  para8.Direction = ParameterDirection.Input;
//  cmd.Parameters.Add(para8);
//  SqlParameter paraReturn = new SqlParameter("@return", SqlDbType.Int);
//  paraReturn.Direction = ParameterDirection.ReturnValue; //參數偏向 為前往參數
//  cmd.Parameters.Add(paraReturn);
//  cmd.ExecuteNonQuery();
//  object obj = paraReturn;
//  if (obj.ToString() == "0")
//  {
//    //存儲進程履行掉敗
//  }
//  else
//  {
//    //勝利
//  }
//}
//#endregion

本文的數據庫用的是sql server自帶數據Northwind

1.只前往單一記載集的存儲進程

SqlConnection sqlconn = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand();
// 設置sql銜接
cmd.Connection = sqlconn;
// 假如履行語句
cmd.CommandText = "Categoriestest1";
// 指定履行語句為存儲進程
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter dp = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
// 填充dataset
dp.Fill(ds);
// 以下是顯示後果
GridView1.DataSource = ds;
GridView1.DataBind();

存儲進程Categoriestest1

CREATE PROCEDURE Categoriestest1
 AS
 select *
 from Categories
 GO

2. 沒有輸出輸入的存儲進程

SqlConnection sqlconn = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand();
cmd.Connection = sqlconn;
cmd.CommandText = "Categoriestest2";
cmd.CommandType = CommandType.StoredProcedure;
sqlconn.Open();
// 履行並顯示影響行數
Label1.Text = cmd.ExecuteNonQuery().ToString();
sqlconn.Close();

存儲進程Categoriestest2

CREATE PROCEDURE Categoriestest2 AS
 insert into dbo.Categories
 (CategoryName,[Description],[Picture])
 values ('test1','test1',null)
 GO

3. 有前往值的存儲進程

SqlConnection sqlconn = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand();
cmd.Connection = sqlconn;
cmd.CommandText = "Categoriestest3";
cmd.CommandType = CommandType.StoredProcedure;
// 創立參數
IDataParameter[] parameters = {
     new SqlParameter("rval", SqlDbType.Int,4)
   };
// 將參數類型設置為 前往值類型
parameters[0].Direction = ParameterDirection.ReturnValue;
// 添加參數
cmd.Parameters.Add(parameters[0]);
sqlconn.Open();
// 履行存儲進程並前往影響的行數
Label1.Text = cmd.ExecuteNonQuery().ToString();
sqlconn.Close();
// 顯示影響的行數和前往值
Label1.Text += "-" + parameters[0].Value.ToString() ;

存儲進程Categoriestest3

CREATE PROCEDURE Categoriestest3
 AS
 insert into dbo.Categories
 (CategoryName,[Description],[Picture])
 values ('test1','test1',null)
return @@rowcount
 GO

4. 有輸出參數和輸入參數的存儲進程

SqlConnection sqlconn = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand();
cmd.Connection = sqlconn;
cmd.CommandText = "Categoriestest4";
cmd.CommandType = CommandType.StoredProcedure;
// 創立參數
IDataParameter[] parameters = {
     new SqlParameter("@Id", SqlDbType.Int,4) ,
     new SqlParameter("@CategoryName", SqlDbType.NVarChar,15) ,
   };
// 設置參數類型
parameters[0].Direction = ParameterDirection.Output; // 設置為輸入參數
parameters[1].Value = "testCategoryName";
// 添加參數
cmd.Parameters.Add(parameters[0]);
cmd.Parameters.Add(parameters[1]);
sqlconn.Open();
// 履行存儲進程並前往影響的行數
Label1.Text = cmd.ExecuteNonQuery().ToString();
sqlconn.Close();
// 顯示影響的行數和輸入參數
Label1.Text += "-" + parameters[0].Value.ToString() ;

存儲進程Categoriestest4

CREATE PROCEDURE Categoriestest4
 @id int output,
 @CategoryName nvarchar(15)
 AS
 insert into dbo.Categories
 (CategoryName,[Description],[Picture])
 values (@CategoryName,'test1',null)
set @id = @@IDENTITY
 GO

5. 同時具有前往值、輸出參數、輸入參數的存儲進程

SqlConnection sqlconn = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand();
cmd.Connection = sqlconn;
cmd.CommandText = "Categoriestest5";
cmd.CommandType = CommandType.StoredProcedure;
// 創立參數
IDataParameter[] parameters = {
     new SqlParameter("@Id", SqlDbType.Int,4) ,
     new SqlParameter("@CategoryName", SqlDbType.NVarChar,15) ,
     new SqlParameter("rval", SqlDbType.Int,4)
   };
// 設置參數類型
parameters[0].Direction = ParameterDirection.Output;    // 設置為輸入參數
parameters[1].Value = "testCategoryName";         // 給輸出參數賦值
parameters[2].Direction = ParameterDirection.ReturnValue; // 設置為前往值
// 添加參數
cmd.Parameters.Add(parameters[0]);
cmd.Parameters.Add(parameters[1]);
cmd.Parameters.Add(parameters[2]);
sqlconn.Open();
// 履行存儲進程並前往影響的行數
Label1.Text = cmd.ExecuteNonQuery().ToString();
sqlconn.Close();
// 顯示影響的行數,輸入參數和前往值
Label1.Text += "-" + parameters[0].Value.ToString() + "-" + parameters[2].Value.ToString();

存儲進程Categoriestest5

CREATE PROCEDURE Categoriestest5
 @id int output,
 @CategoryName nvarchar(15)
 AS
 insert into dbo.Categories
 (CategoryName,[Description],[Picture])
 values (@CategoryName,'test1',null)
set @id = @@IDENTITY
return @@rowcount
 GO

6. 同時前往參數和記載集的存儲進程

SqlConnection sqlconn = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand();
cmd.Connection = sqlconn;
cmd.CommandText = "Categoriestest6";
cmd.CommandType = CommandType.StoredProcedure;
// 創立參數
IDataParameter[] parameters = {
     new SqlParameter("@Id", SqlDbType.Int,4) ,
     new SqlParameter("@CategoryName", SqlDbType.NVarChar,15) ,
     new SqlParameter("rval", SqlDbType.Int,4)          // 前往值
  };
// 設置參數類型
parameters[0].Direction = ParameterDirection.Output;    // 設置為輸入參數
parameters[1].Value = "testCategoryName";          // 給輸出參數賦值
parameters[2].Direction = ParameterDirection.ReturnValue;  // 設置為前往值
// 添加參數
cmd.Parameters.Add(parameters[0]);
cmd.Parameters.Add(parameters[1]);
cmd.Parameters.Add(parameters[2]);
SqlDataAdapter dp = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
// 填充dataset
dp.Fill(ds);
// 顯示成果集
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
Label1.Text = "";
// 顯示輸入參數和前往值
Label1.Text += parameters[0].Value.ToString() + "-" + parameters[2].Value.ToString();

存儲進程Categoriestest6

CREATE PROCEDURE Categoriestest6
 @id int output,
 @CategoryName nvarchar(15)
 AS
 insert into dbo.Categories
 (CategoryName,[Description],[Picture])
 values (@CategoryName,'test1',null)
set @id = @@IDENTITY
 select * from Categories
return @@rowcount
 GO

7. 前往多個記載集的存儲進程

SqlConnection sqlconn = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand();
cmd.Connection = sqlconn;
cmd.CommandText = "Categoriestest7";
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter dp = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
// 填充dataset
dp.Fill(ds);
// 顯示成果集1
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
// 顯示成果集2
GridView2.DataSource = ds.Tables[1];
GridView2.DataBind();

存儲進程Categoriestest7

CREATE PROCEDURE Categoriestest7
 AS
 select * from Categories
 select * from Categories
 GO

願望本文所述對年夜家C#法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved