1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | ①为什么要使用存储过程? 因为它比SQL语句执行快. ②存储过程是什么? 把一堆SQL语句罗在一起,还可以根据条件执行不通SQL语句.(AX写作本文时观点) ③来一个最简单的存储过程 CREATE PROCEDURE dbo.testProcedure_AX AS select userID from USERS order by userid desc 注:dbo.testProcedure_AX是你创建的存储过程名,可以改为:AXzhz等,别跟关键字冲突就行了.AS下面就是一条SQL语句,不会写SQL语句的请回避. ④我怎么在Asp.Net中调用这个存储过程? 下面黄底的这两行就够使了. 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(存储过程) ⑤写个带参数的存储过程吧,上面这个简单得有点惨不忍睹,不过还是蛮实用的. 参数带就带两,一个的没面子,太小家子气了. 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中调用这个带参数的存储过程? 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(); } } 注:把命令的参数添加进去,就OK了 ⑦我还想看看SQL命令执行成功了没有. 注意看下面三行红色的语句 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(这个是系统自动给出的语句是否执行成功,0为成功,其它为失败),最后通过 select 把它选择出来,某位高人说可以通过Return返回,超出本人的认知范围,俺暂时不会,以后再补充吧 ⑧那怎么从后台获得这个执行成功与否的值呢? 下面这段代码可以告诉你答案: 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%正确,现在没时间验证,期待您的测试!!! 1)执行一个没有参数的存储过程的代码如下: SqlConnection conn= new SqlConnection(“connectionString”); SqlDataAdapter da = new SqlDataAdapter(); da.selectCommand = new SqlCommand(); da.selectCommand.Connection = conn; da.selectCommand.CommandText = "NameOfProcedure" ; da.selectCommand.CommandType = CommandType.StoredProcedure; (2)执行一个有参数的存储过程的代码如下 SqlConnection conn= new SqlConnection(“connectionString”); SqlDataAdapter da = new SqlDataAdapter(); da.selectCommand = new SqlCommand(); da.selectCommand.Connection = conn; da.selectCommand.CommandText = "NameOfProcedure" ; da.selectCommand.CommandType = CommandType.StoredProcedure; param = new SqlParameter( "@ParameterName" , SqlDbType.DateTime); param.Direction = ParameterDirection.Input; param.Value = Convert.ToDateTime(inputdate); da.selectCommand.Parameters.Add(param); 若需要添加输出参数: param = new SqlParameter( "@ParameterName" , SqlDbType.DateTime); param.Direction = ParameterDirection.Output; param.Value = Convert.ToDateTime(inputdate); da.selectCommand.Parameters.Add(param); 若要获得参储过程的返回值: param = new SqlParameter( "@ParameterName" , SqlDbType.DateTime); param.Direction = ParameterDirection.ReturnValue; param.Value = Convert.ToDateTime(inputdate); da.selectCommand.Parameters.Add(param); 两种不同的存储过程调用方法 为了突出新方法的优点,首先介绍一下在.NET中调用存储过程的“官方”方法。另外,本文的所有示例程序均工作于SqlServer数据库上,其它情况类似,以后不再一一说明。本文所有例子均采用C#语言。 要在应用程序中访问数据库,一般性的步骤是:首先声明一个数据库连接SqlConnection,然后声明一个数据库命令SqlCommand,用来执行SQL语句和存储过程。有了这两个对象后,就可以根据自己的需要采用不同的执行方式达到目的。需要补充的是,不要忘记在页面上添加如下的引用语句: using System.Data.SqlClient。 就执行存储过程来说,如果执行的是第一类存储过程,那么就要用一个DataAdapter将结果填充到一个DataSet中,然后就可以使用数据网格控件将结果呈现在页面上了;如果执行的是第二和第三种存储过程,则不需要此过程,只需要根据特定的返回判定操作是否成功完成即可。 (1)执行一个没有参数的存储过程的代码如下: SqlConnection conn= new SqlConnection(“connectionString”); SqlDataAdapter da = new SqlDataAdapter(); da.selectCommand = new SqlCommand(); da.selectCommand.Connection = conn; da.selectCommand.CommandText = "NameOfProcedure" ; da.selectCommand.CommandType = CommandType.StoredProcedure; 然后只要选择适当的方式执行此处过程,用于不同的目的即可。 (2)执行一个有参数的存储过程的代码如下(我们可以将调用存储过程的函数声明为ExeProcedure( string inputdate)): SqlConnection conn= new SqlConnection(“connectionString”); SqlDataAdapter da = new SqlDataAdapter(); da.selectCommand = new SqlCommand(); da.selectCommand.Connection = conn; da.selectCommand.CommandText = "NameOfProcedure" ; da.selectCommand.CommandType = CommandType.StoredProcedure; (以上代码相同,以下为要添加的代码) param = new SqlParameter( "@ParameterName" , SqlDbType.DateTime); param.Direction = ParameterDirection.Input; param.Value = Convert.ToDateTime(inputdate); da.selectCommand.Parameters.Add(param); 这样就添加了一个输入参数。若需要添加输出参数: param = new SqlParameter( "@ParameterName" , SqlDbType.DateTime); param.Direction = ParameterDirection.Output; param.Value = Convert.ToDateTime(inputdate); da.selectCommand.Parameters.Add(param); 若要获得参储过程的返回值: param = new SqlParameter( "@ParameterName" , SqlDbType.DateTime); param.Direction = ParameterDirection.ReturnValue; param.Value = Convert.ToDateTime(inputdate); da.selectCommand.Parameters.Add(param); 从上面的代码我们可以看出,当存储过程比较多或者存储过程的参数比较多时,这种方法会大大影响开发的速度;另外一方面,如果项目比较大,那么这些用于数据库逻辑的函数在以后的维护中也是一个很大的负担。那么,有没有一种改进的方法可以解决这个问题呢?想到在执行没有参数的存储过程时只需要传入一个存储过程的名字就可以调用相应的存储过程,而且在SqlServer数据库中我们可以直接在查询分析器中敲入“存储过程名(参数列表)”样的字符串就可以执行存储过程,那么,是否可以把这种思想应用到应用程序中呢? 于是在编译器中键入相应代码。这些代码是在调用不带参数的存储过程的代码的基础上改的。具体代码如下: SqlConnection conn= new SqlConnection(“connectionString”); SqlDataAdapter da = new SqlDataAdapter(); da.selectCommand = new SqlCommand(); da.selectCommand.Connection = conn; da.selectCommand.CommandText = "NameOfProcedure('para1','para2',para3)" ; da.selectCommand.CommandType = CommandType.StoredProcedure; 存储过程的方法,使我在添加数据中走了不少的弯路,最近,在查阅了大量的资料之后,终于在微软的一个实例中找到了一种良好的方法。 首先编写好一有返回值的存储过程 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)); command.paraments.add( "@para1" ), "name1" ); //输入参数,职员姓名 command.paraments.add( new sqlparament( "@para2" , //生成一输出参数 SqlDbType.Int; //参数数据类型 ParamenterDirection.OutPut, //输入输出类型 0, 0, string .Emplty, 用SqlCommand和DataSet: SqlConnection conn= new SqlConnection( "server=(local);uid=;password=;database=" ); SqlCommand cmd= new SqlCommand( "StoreProcedure" ,connn); cmd.CommandType=CommandType.StoreProcedure; SqlDataAdapter dsCommand= new SqlDataAdapter(cmd); DataSet ds= new DataSet(); dsCommand.Fill(ds); 2.用SqlCommand和SqlDataAdapter Sqlconnection conn= new SqlConnection( "server=(local);uid=;password=;database=" ); SqlCommand cmd= new SqlCommand( "StoreProcedure" ,conn); cmd.CommandType=CommandType.StoreProcedure; SqlDataReader dr=cmd.ExecuteReader() while (dr.Read()) { Response.Write(dr.Item[ "Field" ]); } DataRowVerstion.Default, null ) //参数值,输入参数时需提供 ); command.commandtype=commandtype.StoredProcedure; command.connection.open(); command.executenonQuery(); int pkid=( int )command.Parameters[ "@para2" ].value; //得到输出参数的值 command.connection.close(); 字串6 此处是引用输出参数,如果要引用返回值(是否成功添加数据)则只需把ParamenterDirection的类型改为returnvalue;再自己改一个参数名就可以了. |
联系客服