2023年6月21日发(作者:)

sql存储过程简单教程①为什么要使⽤存储过程?因为它⽐SQL语句执⾏快.②存储过程是什么?把⼀堆SQL语句罗在⼀起,还可以根据条件执⾏不通SQL语句.(AX写作本⽂时观点)③来⼀个最简单的存储过程CREATE PROCEDURE ocedure_AXASselect userID from USERS order by userid desc注:ocedure_AX是你创建的存储过程名,可以改为:AXzhz等,别跟关键字冲突就⾏了.AS下⾯就是⼀条SQL语句,不会写SQL语句的请回避.④我怎么在中调⽤这个存储过程?下⾯黄底的这两⾏就够使了. public static string GetCustomerCName(ref ArrayList arrayCName,ref ArrayList arrayID) { SqlConnection con=Connection(); SqlCommand cmd=new SqlCommand("testProcedure_AX",con); dType=Procedure; (); try { SqlDataReader dr=eReader(); while(()) { if(dr[0].ToString()=="") { (dr[1].ToString()); } } ();

return "OK!"; } catch(Exception ex) { (); return ng(); } }注:其实就是把以前SqlCommand cmd=new SqlCommand("select userID from USERS order by userid desc",con);中的SQL语句替换为存储过程名,再把cmd的类型标注为Procedure(存储过程)

⑤写个带参数的存储过程吧,上⾯这个简单得有点惨不忍睹,不过还是蛮实⽤的.参数带就带两,⼀个的没⾯⼦,太⼩家⼦⽓了.CREATE PROCEDURE /*这⾥写注释*/@startDate varchar(16),@endDate varchar(16)

ASselect id from table_AX where commentDateTime>@startDate and commentDateTime<@endDate order by contentownerid DESC注:@startDate varchar(16)是声明@startDate 这个变量,多个变量名间⽤【,】隔开.后⾯的SQL就可以使⽤这个变量了.⑥我怎么在中调⽤这个带参数的存储过程?public static string GetCustomerCNameCount(string startDate,string endDate,ref DataSet ds){ SqlConnection con=Connection();//-----------------------注意这⼀段-------------------------------------------------------------------------------------------------------- SqlDataAdapter da=new SqlDataAdapter("AXzhz",con); para0=new SqlParameter("@startDate",startDate); para1=new SqlParameter("@endDate",endDate); (para0); (para1); dType=Procedure;//------------------------------------------------------------------------------------------------------------------------------- try { (); (ds); (); return "OK"; } catch(Exception ex) { return ng(); }

}

注:把命令的参数添加进去,就OK了⑦我还想看看SQL命令执⾏成功了没有.注意看下⾯三⾏红⾊的语句CREATE PROCEDURE /* @parameter1 ⽤户名 @parameter2 新密码*/@passWord nvarchar(20),@userName nvarchar(20)ASdeclare @err0 intupdate WL_user set password=@password where UserName=@userNameset @err0=@@error

select @err0 as err0注:先声明⼀个整型变量@err0,再给其赋值为@@error(这个是系统⾃动给出的语句是否执⾏成功,0为成功,其它为失败),最后通过select把它选择出来,某位⾼⼈说可以通过Return返回,超出本⼈的认知范围,俺暂时不会,以后再补充吧⑧那怎么从后台获得这个执⾏成功与否的值呢?下⾯这段代码可以告诉你答案: public static string GetCustomerCName() { SqlConnection con=Connection();

SqlCommand cmd=new SqlCommand("AXzhz",con); dType=Procedure; para0=new SqlParameter("@startDate","2006-9-10"); para1=new SqlParameter("@endDate","2006-9-20"); (para0); (para1);

(); try { Int32 re=(int32)eScalar();

();

if (re==0) return "OK!"; else return "false"; } catch(Exception ex) { (); return ng(); } }注:就是通过SqlCommand的ExecuteScalar()⽅法取回这个值,这句话是从MSDN上找的,俺认为改成: int re=(int)eScalar(); 99%正确,现在没时间验证,期待您的测试

1)执⾏⼀个没有参数的存储过程的代码如下:

SqlConnection conn=new SqlConnection(“connectionString”);

SqlDataAdapter da = new SqlDataAdapter();

Command = new SqlCommand();

tion = conn;

dText = "NameOfProcedure";

dType = Procedure;

(2)执⾏⼀个有参数的存储过程的代码如下

SqlConnection conn=new SqlConnection(“connectionString”);

SqlDataAdapter da = new SqlDataAdapter();

Command = new SqlCommand();

tion = conn;

dText = "NameOfProcedure";

dType = Procedure;

param = new SqlParameter("@ParameterName", me);

ion = ;

= Time(inputdate);

(param);

若需要添加输出参数:

param = new SqlParameter("@ParameterName", me);

ion = ;

= Time(inputdate);

(param);

若要获得参储过程的返回值:

param = new SqlParameter("@ParameterName", me);

ion = Value;

= Time(inputdate);

(param);两种不同的存储过程调⽤⽅法

为了突出新⽅法的优点,⾸先介绍⼀下在.NET中调⽤存储过程的“官⽅”⽅法。另外,本⽂的所有⽰例程序均⼯作于SqlServer数据库上,其它情况类似,以后不再⼀⼀说明。本⽂所有例⼦均采⽤C#语⾔。

要在应⽤程序中访问数据库,⼀般性的步骤是:⾸先声明⼀个数据库连接SqlConnection,然后声明⼀个数据库命令SqlCommand,⽤来执⾏SQL语句和存储过程。有了这两个对象后,就可以根据⾃⼰的需要采⽤不同的执⾏⽅式达到⽬的。需要补充的就执⾏存储过程来说,如果执⾏的是第⼀类存储过程,那么就要⽤⼀个DataAdapter将结果填充到⼀个DataSet中,然后就可以使⽤数据⽹格控件将结果呈现在页⾯上了;如果执⾏的是第⼆和第三种存储过程,则不需要此过程,只需要根据特定的返回(1)执⾏⼀个没有参数的存储过程的代码如下:

SqlConnection conn=new SqlConnection(“connectionString”);

SqlDataAdapter da = new SqlDataAdapter();

Command = new SqlCommand();

tion = conn;

dText = "NameOfProcedure";

dType = Procedure;

然后只要选择适当的⽅式执⾏此处过程,⽤于不同的⽬的即可。

(2)执⾏⼀个有参数的存储过程的代码如下(我们可以将调⽤存储过程的函数声明为ExeProcedure(string inputdate)):

SqlConnection conn=new SqlConnection(“connectionString”);

SqlDataAdapter da = new SqlDataAdapter();

Command = new SqlCommand();

tion = conn;

dText = "NameOfProcedure";

dType = Procedure;

(以上代码相同,以下为要添加的代码)

param = new SqlParameter("@ParameterName", me);

ion = ;

= Time(inputdate);

(param);

这样就添加了⼀个输⼊参数。若需要添加输出参数:

param = new SqlParameter("@ParameterName", me);

ion = ;

= Time(inputdate);

(param);

若要获得参储过程的返回值:

param = new SqlParameter("@ParameterName", me);

ion = Value;

= Time(inputdate);

(param);

从上⾯的代码我们可以看出,当存储过程⽐较多或者存储过程的参数⽐较多时,这种⽅法会⼤⼤影响开发的速度;另外⼀⽅⾯,如果项⽬⽐较⼤,那么这些⽤于数据库逻辑的函数在以后的维护中也是⼀个很⼤的负担。那么,有没有⼀种改进的⽅法可于是在编译器中键⼊相应代码。这些代码是在调⽤不带参数的存储过程的代码的基础上改的。具体代码如下:

SqlConnection conn=new SqlConnection(“connectionString”);

SqlDataAdapter da = new SqlDataAdapter();

Command = new SqlCommand();

tion = conn;

dText = "NameOfProcedure('para1','para2',para3)";

dType = Procedure;

存储过程的⽅法,使我在添加数据中⾛了不少的弯路,最近,在查阅了⼤量的资料之后,终于在微软的⼀个实例中找到了⼀种良好的⽅法。⾸先编写好⼀有返回值的存储过程create procedure proc_name

@para1 nchar(20), --输⼊参数 @para2 int = null out --输出参数,供程序使⽤as

set nocount on if ( not exists (select * from employee where em_name=@para1)) begin insert into employee(name) values(@para1)

select @para2=@@identity --返回添加记录的ID return 1 --返回是否成功添加数据

字串6 end else return 0 --返回失败go然后是调⽤存储过程的⽅法sqlcommand command;command = new sqlcommand(proc_name,new sqlconnection(connectionstr));("@para1"),"name1"); //输⼊参数,职员姓名(new sqlparament("@para2", //⽣成⼀输出参数;             //参数数据类型,      //输⼊输出类型0,0,,⽤SqlCommand和DataSet:SqlConnection conn=new SqlConnection("server=(local);uid=;password=;database=");SqlCommand cmd=new SqlCommand("StoreProcedure",connn);dType=rocedure;SqlDataAdapter dsCommand=new SqlDataAdapter(cmd);DataSet ds=new DataSet();(ds);2.⽤SqlCommand和SqlDataAdapterSqlconnection conn=new SqlConnection("server=(local);uid=;password=;database=");SqlCommand cmd=new SqlCommand("StoreProcedure",conn);dType=rocedure;SqlDataReader dr=eReader()while(()){ (["Field"]);}t,null)                 //参数值,输⼊参数时需提供);dtype=Procedure;();enonQuery();int pkid=(int)ters["@para2"].value; //得到输出参数的值(); 字串6

此处是引⽤输出参数,如果要引⽤返回值(是否成功添加数据)则只需把ParamenterDirection的类型改为returnvalue;再⾃⼰改⼀个参数名就可以了.

2023年6月21日发(作者:)

sql存储过程简单教程①为什么要使⽤存储过程?因为它⽐SQL语句执⾏快.②存储过程是什么?把⼀堆SQL语句罗在⼀起,还可以根据条件执⾏不通SQL语句.(AX写作本⽂时观点)③来⼀个最简单的存储过程CREATE PROCEDURE ocedure_AXASselect userID from USERS order by userid desc注:ocedure_AX是你创建的存储过程名,可以改为:AXzhz等,别跟关键字冲突就⾏了.AS下⾯就是⼀条SQL语句,不会写SQL语句的请回避.④我怎么在中调⽤这个存储过程?下⾯黄底的这两⾏就够使了. public static string GetCustomerCName(ref ArrayList arrayCName,ref ArrayList arrayID) { SqlConnection con=Connection(); SqlCommand cmd=new SqlCommand("testProcedure_AX",con); dType=Procedure; (); try { SqlDataReader dr=eReader(); while(()) { if(dr[0].ToString()=="") { (dr[1].ToString()); } } ();

return "OK!"; } catch(Exception ex) { (); return ng(); } }注:其实就是把以前SqlCommand cmd=new SqlCommand("select userID from USERS order by userid desc",con);中的SQL语句替换为存储过程名,再把cmd的类型标注为Procedure(存储过程)

⑤写个带参数的存储过程吧,上⾯这个简单得有点惨不忍睹,不过还是蛮实⽤的.参数带就带两,⼀个的没⾯⼦,太⼩家⼦⽓了.CREATE PROCEDURE /*这⾥写注释*/@startDate varchar(16),@endDate varchar(16)

ASselect id from table_AX where commentDateTime>@startDate and commentDateTime<@endDate order by contentownerid DESC注:@startDate varchar(16)是声明@startDate 这个变量,多个变量名间⽤【,】隔开.后⾯的SQL就可以使⽤这个变量了.⑥我怎么在中调⽤这个带参数的存储过程?public static string GetCustomerCNameCount(string startDate,string endDate,ref DataSet ds){ SqlConnection con=Connection();//-----------------------注意这⼀段-------------------------------------------------------------------------------------------------------- SqlDataAdapter da=new SqlDataAdapter("AXzhz",con); para0=new SqlParameter("@startDate",startDate); para1=new SqlParameter("@endDate",endDate); (para0); (para1); dType=Procedure;//------------------------------------------------------------------------------------------------------------------------------- try { (); (ds); (); return "OK"; } catch(Exception ex) { return ng(); }

}

注:把命令的参数添加进去,就OK了⑦我还想看看SQL命令执⾏成功了没有.注意看下⾯三⾏红⾊的语句CREATE PROCEDURE /* @parameter1 ⽤户名 @parameter2 新密码*/@passWord nvarchar(20),@userName nvarchar(20)ASdeclare @err0 intupdate WL_user set password=@password where UserName=@userNameset @err0=@@error

select @err0 as err0注:先声明⼀个整型变量@err0,再给其赋值为@@error(这个是系统⾃动给出的语句是否执⾏成功,0为成功,其它为失败),最后通过select把它选择出来,某位⾼⼈说可以通过Return返回,超出本⼈的认知范围,俺暂时不会,以后再补充吧⑧那怎么从后台获得这个执⾏成功与否的值呢?下⾯这段代码可以告诉你答案: public static string GetCustomerCName() { SqlConnection con=Connection();

SqlCommand cmd=new SqlCommand("AXzhz",con); dType=Procedure; para0=new SqlParameter("@startDate","2006-9-10"); para1=new SqlParameter("@endDate","2006-9-20"); (para0); (para1);

(); try { Int32 re=(int32)eScalar();

();

if (re==0) return "OK!"; else return "false"; } catch(Exception ex) { (); return ng(); } }注:就是通过SqlCommand的ExecuteScalar()⽅法取回这个值,这句话是从MSDN上找的,俺认为改成: int re=(int)eScalar(); 99%正确,现在没时间验证,期待您的测试

1)执⾏⼀个没有参数的存储过程的代码如下:

SqlConnection conn=new SqlConnection(“connectionString”);

SqlDataAdapter da = new SqlDataAdapter();

Command = new SqlCommand();

tion = conn;

dText = "NameOfProcedure";

dType = Procedure;

(2)执⾏⼀个有参数的存储过程的代码如下

SqlConnection conn=new SqlConnection(“connectionString”);

SqlDataAdapter da = new SqlDataAdapter();

Command = new SqlCommand();

tion = conn;

dText = "NameOfProcedure";

dType = Procedure;

param = new SqlParameter("@ParameterName", me);

ion = ;

= Time(inputdate);

(param);

若需要添加输出参数:

param = new SqlParameter("@ParameterName", me);

ion = ;

= Time(inputdate);

(param);

若要获得参储过程的返回值:

param = new SqlParameter("@ParameterName", me);

ion = Value;

= Time(inputdate);

(param);两种不同的存储过程调⽤⽅法

为了突出新⽅法的优点,⾸先介绍⼀下在.NET中调⽤存储过程的“官⽅”⽅法。另外,本⽂的所有⽰例程序均⼯作于SqlServer数据库上,其它情况类似,以后不再⼀⼀说明。本⽂所有例⼦均采⽤C#语⾔。

要在应⽤程序中访问数据库,⼀般性的步骤是:⾸先声明⼀个数据库连接SqlConnection,然后声明⼀个数据库命令SqlCommand,⽤来执⾏SQL语句和存储过程。有了这两个对象后,就可以根据⾃⼰的需要采⽤不同的执⾏⽅式达到⽬的。需要补充的就执⾏存储过程来说,如果执⾏的是第⼀类存储过程,那么就要⽤⼀个DataAdapter将结果填充到⼀个DataSet中,然后就可以使⽤数据⽹格控件将结果呈现在页⾯上了;如果执⾏的是第⼆和第三种存储过程,则不需要此过程,只需要根据特定的返回(1)执⾏⼀个没有参数的存储过程的代码如下:

SqlConnection conn=new SqlConnection(“connectionString”);

SqlDataAdapter da = new SqlDataAdapter();

Command = new SqlCommand();

tion = conn;

dText = "NameOfProcedure";

dType = Procedure;

然后只要选择适当的⽅式执⾏此处过程,⽤于不同的⽬的即可。

(2)执⾏⼀个有参数的存储过程的代码如下(我们可以将调⽤存储过程的函数声明为ExeProcedure(string inputdate)):

SqlConnection conn=new SqlConnection(“connectionString”);

SqlDataAdapter da = new SqlDataAdapter();

Command = new SqlCommand();

tion = conn;

dText = "NameOfProcedure";

dType = Procedure;

(以上代码相同,以下为要添加的代码)

param = new SqlParameter("@ParameterName", me);

ion = ;

= Time(inputdate);

(param);

这样就添加了⼀个输⼊参数。若需要添加输出参数:

param = new SqlParameter("@ParameterName", me);

ion = ;

= Time(inputdate);

(param);

若要获得参储过程的返回值:

param = new SqlParameter("@ParameterName", me);

ion = Value;

= Time(inputdate);

(param);

从上⾯的代码我们可以看出,当存储过程⽐较多或者存储过程的参数⽐较多时,这种⽅法会⼤⼤影响开发的速度;另外⼀⽅⾯,如果项⽬⽐较⼤,那么这些⽤于数据库逻辑的函数在以后的维护中也是⼀个很⼤的负担。那么,有没有⼀种改进的⽅法可于是在编译器中键⼊相应代码。这些代码是在调⽤不带参数的存储过程的代码的基础上改的。具体代码如下:

SqlConnection conn=new SqlConnection(“connectionString”);

SqlDataAdapter da = new SqlDataAdapter();

Command = new SqlCommand();

tion = conn;

dText = "NameOfProcedure('para1','para2',para3)";

dType = Procedure;

存储过程的⽅法,使我在添加数据中⾛了不少的弯路,最近,在查阅了⼤量的资料之后,终于在微软的⼀个实例中找到了⼀种良好的⽅法。⾸先编写好⼀有返回值的存储过程create procedure proc_name

@para1 nchar(20), --输⼊参数 @para2 int = null out --输出参数,供程序使⽤as

set nocount on if ( not exists (select * from employee where em_name=@para1)) begin insert into employee(name) values(@para1)

select @para2=@@identity --返回添加记录的ID return 1 --返回是否成功添加数据

字串6 end else return 0 --返回失败go然后是调⽤存储过程的⽅法sqlcommand command;command = new sqlcommand(proc_name,new sqlconnection(connectionstr));("@para1"),"name1"); //输⼊参数,职员姓名(new sqlparament("@para2", //⽣成⼀输出参数;             //参数数据类型,      //输⼊输出类型0,0,,⽤SqlCommand和DataSet:SqlConnection conn=new SqlConnection("server=(local);uid=;password=;database=");SqlCommand cmd=new SqlCommand("StoreProcedure",connn);dType=rocedure;SqlDataAdapter dsCommand=new SqlDataAdapter(cmd);DataSet ds=new DataSet();(ds);2.⽤SqlCommand和SqlDataAdapterSqlconnection conn=new SqlConnection("server=(local);uid=;password=;database=");SqlCommand cmd=new SqlCommand("StoreProcedure",conn);dType=rocedure;SqlDataReader dr=eReader()while(()){ (["Field"]);}t,null)                 //参数值,输⼊参数时需提供);dtype=Procedure;();enonQuery();int pkid=(int)ters["@para2"].value; //得到输出参数的值(); 字串6

此处是引⽤输出参数,如果要引⽤返回值(是否成功添加数据)则只需把ParamenterDirection的类型改为returnvalue;再⾃⼰改⼀个参数名就可以了.