c#調用預存程序

來源:互聯網
上載者:User

標籤:

預存程序(Stored Procedure)即用來儲存資料表操作的一個過程,是把對資料表操作的方法儲存到一起的一個對象,是儲存在資料庫中。

優點:1.降低網路傳輸資料量:通過預存程序的名稱和參數傳遞即可調用執行,不用傳輸sql。

2.執行效率高:sqlserver會事先將預存程序編譯成可執行檔二進位代碼,運行預存程序時,無需再次編譯。

3.封裝性:也可叫模組式編程,將實現某種功能的多條sql封裝到一個對象中,可多次重複調用,可移植性強。

4.安全:可針對不同的許可權的使用者使用不同的預存程序。

下面講C#如何調用寫好的預存程序,下面是對資料操作封裝幾個函數

 1  //執行對錶的增刪改操作的sql語句 2 public static int ExecuteCommand(string text) 3         { 4             using (SqlConnection conn = new SqlConnection(connectionString)) 5             { 6                 conn.Open(); 7                 using (SqlCommand cmd = new SqlCommand(text, conn)) 8                 { 9                     int i = cmd.ExecuteNonQuery();10                     return i;11                 }               12             }            13         }14 15 public static int ExecStoredProcedure(string procName, params SqlParameter[] parameters)16         {17             int rtn = 0;18             using (SqlConnection conn = new SqlConnection(connectionString))19             {20                 conn.Open();21                 using (SqlCommand cmd = conn.CreateCommand())22                 {                   23                     SqlTransaction st = conn.BeginTransaction();24                     cmd.Transaction = st;25                     try26                     {27                         cmd.CommandText = procName;28                         cmd.CommandType = CommandType.StoredProcedure;29                         cmd.Parameters.AddRange(parameters);30                         rtn= cmd.ExecuteNonQuery();31                         st.Commit();32                         return rtn;33                     }34                     catch(SqlException sqlex)35                     {36                         st.Rollback();37                         throw sqlex;38                     }                                       39                 }40             }41         }42         public static int ExecuteStoredProcedure(string procName,43             params SqlParameter[] parameters)44         {45             using (SqlConnection conn = new SqlConnection(connectionString))46             {47                 conn.Open();48                 using (SqlCommand cmd = conn.CreateCommand())49                 {50                     cmd.CommandText = procName;51                     cmd.CommandType = CommandType.StoredProcedure;52                     cmd.Parameters.AddRange(parameters);53                     return cmd.ExecuteNonQuery();54                 }55             }56             57         }
View Code

下面是執行預存程序的三種方式:

 1 protected void Button1_Click(object sender, EventArgs e) 2     { 3         string mainName = "圓夢組"; 4         string detailName="劉能|趙本山|沈燕|歐陽鋒"; 5         string detailAge="23|32|18|19"; 6         int rtn=DBHelper.ExecuteCommand(string.Format("exec dbo.Proc_TestBatchMainDetailIns ‘{0}‘,‘{1}‘,‘{2}‘", mainName, detailName,detailAge)); 7     } 8     protected void Button2_Click(object sender, EventArgs e) 9     {10         string mainName = "起航組2";11         string detailName="段毅2|喬峰2|楊過2|李莫愁2";12         string detailAge="18|28|jj|35";13         SqlParameter[] sps = new SqlParameter[] { 14         new SqlParameter("@mainName",mainName),15             new SqlParameter("@detailNameStr",detailName),16              new SqlParameter("@detailAgeStr",detailAge)17         };18         try19         {           20             int rtn = DBHelper.ExecuteStoredProcedure("dbo.Proc_TestBatchMainDetailIns", sps);21         }22         catch (Exception ex)23         {24             Response.Write(ex.Message);25         }26         27     }28     protected void Button3_Click(object sender, EventArgs e)29     {30         string mainName = "起航組2";31         string detailName = "段毅2|喬峰2|楊過2|李莫愁2";32         string detailAge = "18|28|jj|35";33         SqlParameter[] sps = new SqlParameter[] { 34         new SqlParameter("@mainName",mainName),35             new SqlParameter("@detailNameStr",detailName),36              new SqlParameter("@detailAgeStr",detailAge)37         };38         try39         {40             int rtn = DBHelper.ExecStoredProcedure("dbo.Proc_TestBatchMainDetailIns", sps);41         }42         catch (Exception ex)43         {44             Response.Write(ex.Message);45         }46     }
View Code

推薦用第三種方式,用到了交易處理,若只要有一條不通過全部復原,避免髒資料的產生。也可以在預存程序中使用事務,後續會繼續更新...

c#調用預存程序

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.