①為什么要使用存儲過程? 因為它比SQL語句執(zhí)行快. ②存儲過程是什么? 把一堆SQL語句羅在一起,還可以根據(jù)條件執(zhí)行不同的SQL語句 ③來一個最簡單的存儲過程 CREATE PROCEDURE dbo.testProcedure_AX AS select userID from USERS order by userid desc 注:dbo.testProcedure_AX是你創(chuàng)建的存儲過程名,可以改為:AXzhz等,別跟關鍵字沖突就行了.AS下面就是一條SQL語句,不會寫SQL語句的請回避. ④我怎么在ASP.NET中調(diào)用這個存儲過程? 下面黃底的這兩行就夠使了. public static string GetCustomerCName(ref ArrayList arrayCName,ref ArrayList arrayID) { SqlConnection con=ADConnection.createConnection(); SqlCommand cmd=new SqlCommand("testProcedure_AX",con); cmd.CommandType=CommandType.StoredProcedure; con.Open(); try { SqlDataReader dr=cmd.ExecuteReader(); while(dr.Read()) { if(dr[0].ToString()=="") { arrayCName.Add(dr[1].ToString()); } } con.Close(); return "OK!"; } catch(Exception ex) { con.Close(); return ex.ToString(); } } 注:其實就是把以前 SqlCommand cmd=new SqlCommand("select userID from USERS order by userid desc",con); 中的SQL語句替換為存儲過程名,再把cmd的類型標注為CommandType.StoredProcedure(存儲過程) ⑤寫個帶參數(shù)的存儲過程吧,上面這個簡單得有點慘不忍睹,不過還是蠻實用的. 參數(shù)帶就帶兩,一個的沒面子,太小家子氣了. CREATE PROCEDURE dbo.AXzhz /* 這里寫注釋 */ @startDate varchar(16), @endDate varchar(16) AS select id from table_AX where commentDateTime>@startDate and commentDateTime<@endDate order by contentownerid DESC 注:@startDate varchar(16)是聲明@startDate 這個變量,多個變量名間用【,】隔開.后面的SQL就可以使用這個變量了. ⑥我怎么在ASP.NET中調(diào)用這個帶參數(shù)的存儲過程? public static string GetCustomerCNameCount(string startDate,string endDate,ref DataSet ds) { SqlConnection con=ADConnection.createConnection(); //-----------------------注意這一段------------------------------------------- SqlDataAdapter da=new SqlDataAdapter("AXzhz",con); para0=new SqlParameter("@startDate",startDate); para1=new SqlParameter("@endDate",endDate); da.SelectCommand.Parameters.Add(para0); da.SelectCommand.Parameters.Add(para1); da.SelectCommand.CommandType=CommandType.StoredProcedure; //---------------------------------------------------------------------------- try { con.Open(); da.Fill(ds); con.Close(); return "OK"; } catch(Exception ex) { return ex.ToString(); } } 注:把命令的參數(shù)添加進去,就OK了 鳥的,改字體顏色的東西太垃圾了,改不好,大家湊活著看. ⑦我還想看看SQL命令執(zhí)行成功了沒有. 注意看下面三行紅色的語句 CREATE PROCEDURE dbo.AXzhz /* @parameter1 用戶名 @parameter2 新密碼 */ @password nvarchar(20), @userName nvarchar(20) AS declare @err0 int update WL_user set password=@password where UserName=@userName set @err0=@@error select @err0 as err0 注:先聲明一個整型變量@err0,再給其賦值為@@error(這個是系統(tǒng)自動給出的語句是否執(zhí)行成功,0為成功,其它為失敗),最后通過select把它選擇出來,某位高人說可以通過Return返回,超出本人的認知范圍,俺暫時不會,以后再補充吧 ⑧那怎么從后臺獲得這個執(zhí)行成功與否的值呢? 下面這段代碼可以告訴你答案: public static string GetCustomerCName() { SqlConnection con=ADConnection.createConnection(); SqlCommand cmd=new SqlCommand("AXzhz",con); cmd.CommandType=CommandType.StoredProcedure; para0=new SqlParameter("@startDate","2006-9-10"); para1=new SqlParameter("@endDate","2006-9-20"); da.SelectCommand.Parameters.Add(para0); da.SelectCommand.Parameters.Add(para1); con.Open(); try { Int32 re=(int32)cmd.ExecuteScalar(); //返回結果集中第一行第一列值 con.Close(); if (re==0) return "OK!"; else return "false"; } catch(Exception ex) { con.Close(); return ex.ToString(); } } 注:就是通過SqlCommand的ExecuteScalar()方法取回這個值,這句話是從MSDN上找的,俺認為改成: int re=(int)cmd.ExecuteScalar(); 99%正確,現(xiàn)在沒時間驗證,期待您的測試!!! ⑨我要根據(jù)傳入的參數(shù)判斷執(zhí)行哪條SQL語句!!~ 下面這個存儲過程可以滿足我們的要求,竟然是Pascal/VB的寫法,Begin----End ,不是{},,,對使用C#的我來說,這個語法有點惡心......... ALTER PROCEDURE dbo.selectCustomerCNameCount @customerID int AS if @customerID=-1 begin select contentownerid ,userCName,count(*) as countAll from view_usercomment group by contentownerid,userCName order by contentownerid DESC end else begin select contentownerid ,userCName,count(*) as countAll from view_usercomment where contentownerid=@customerID group by contentownerid,userCName order by contentownerid DESC end |
|