asp.net执行SqlServer存储过程!(详解!)
ASP.NET执行存储过程
一. 执行一个没有参数的存储过程的代码如下:
connectionString为连接字符串
SqlConnection conn=new SqlConnection(connectionString);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand();
da.SelectCommand.Connection = conn;
//myProc存储过程的名字
da.SelectCommand.CommandText = "myProc";
da.SelectCommand.CommandType = CommandType.StoredProcedure; 二. 执行一个有参数的存储过程的代码如下
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); try
{
//****进行数据连接****//
string conString="server=localhost;database=stuDB;uid=sa";//连接串
SqlConnection sqlConnection=new SqlConnection(conString);//创建连接对象
sqlConnection.Open();//打开连接
SqlCommand sqlCommand=new SqlCommand();//创建SqlCommand命令对象
sqlCommand.Connection=sqlConnection;//SqlCommand命令对象的连接属性赋值
sqlCommand.CommandType=CommandType.StoredProcedure;//**************命令对象的类型为执行数据库的存储过程***********
string sql = string.Format("proc_insert_stuClass");//Sql语句为数据库的存储过程
sqlCommand.CommandText=sql;//命令文本 //****设置存储过程的参数****//
SqlParameter sp1=new SqlParameter("@outcome",SqlDbType.Bit);//创建参数对象,并设置@outcome参数的类型为Bit类型
sp1.Direction=System.Data.ParameterDirection.Output;//设置此项参数的类型为输出参数
sqlCommand.Parameters.Add(sp1);//将此项参数添加到命令参数集 SqlParameter sp=new SqlParameter("@classNo",SqlDbType.VarChar);
sp.Direction=System.Data.ParameterDirection.Input;//设置此项参数的类型为输入参数
sp.Value=this.textBox1.Text;//给输入参数赋值
sqlCommand.Parameters.Add(sp); sp=new SqlParameter("@classCount",SqlDbType.Int);
sp.Direction=System.Data.ParameterDirection.Input;//设置此项参数的类型为输入参数
sp.Value=this.textBox2.Text;//给输入参数赋值
sqlCommand.Parameters.Add(sp); sp=new SqlParameter("@classTeacher",SqlDbType.VarChar);
sp.Direction=System.Data.ParameterDirection.Input;//设置此项参数的类型为输入参数
sp.Value=this.textBox3.Text;//给输入参数赋值
sqlCommand.Parameters.Add(sp); sp=new SqlParameter("@classNote",SqlDbType.VarChar);
sp.Direction=System.Data.ParameterDirection.Input;//设置此项参数的类型为输入参数
sp.Value=this.textBox4.Text;//给输入参数赋值
sqlCommand.Parameters.Add(sp); //****执行存储过程****//
sqlCommand.ExecuteNonQuery();//执行存储过程
string outcome=sp1.Value.ToString();//将输出参数的值取出
Console.WriteLine(outcome);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
asp.net执行SqlServer存储过程!(详解!)的更多相关文章
- SqlServer存储过程详解
SqlServer存储过程详解 1.创建存储过程的基本语法模板: if (exists (select * from sys.objects where name = 'pro_name')) dro ...
- SQLServer 存储过程详解
Transact-SQL中的存储过程,非常类似于Java语言中的方法,它可以重复调用.当存储过程执行一次后,可以将语句缓存中,这样下次执行的时候直接使用缓存中的语句.这样就可以提高存储过程的性能. Ø ...
- mysql存储过程详解
mysql存储过程详解 1. 存储过程简介 我们常用的操作数据库语言SQL语句在执行的时候需要要先编译,然后执行,而存储过程(Stored Procedure)是一组为了完成特定功能的S ...
- mysql 存储过程详解 存储过程
mysql存储过程详解 1. 存储过程简介 我们常用的操作数据库语言SQL语句在执行的时候需要要先编译,然后执行,而存储过程(Stored Procedure)是一组为了完成 ...
- MySQL存储过程详解 mysql 存储过程
原文地址:MySQL存储过程详解 mysql 存储过程作者:王者佳暮 mysql存储过程详解 1. 存储过程简介 我们常用的操作数据库语言SQL语句在执行的时候需要要先编译,然后执行,而存储 ...
- MySQL 执行计划explain详解
MySQL 执行计划explain详解 2015-08-10 13:56:27 分类: MySQL explain命令是查看查询优化器如何决定执行查询的主要方法.这个功能有局限性,并不总会说出真相,但 ...
- MySQL存储过程详解 mysql 存储过程(二)
mysql存储过程详解 1. 存储过程简介 我们常用的操作数据库语言SQL语句在执行的时候需要要先编译,然后执行,而存储过程(Stored Procedure)是一组为了完成特定功能的SQL ...
- Asp.net中GridView使用详解(引)【转】
Asp.net中GridView使用详解(引) GridView无代码分页排序 GridView选中,编辑,取消,删除 GridView正反双向排序 GridView和下拉菜单DropDownList ...
- mySQL的存储过程详解
mysql存储过程详解 1. 存储过程简介 我们常用的操作数据库语言SQL语句在执行的时候需要要先编译,然后执行,而存储过程(Stored Procedure)是一组为了完成特定功能的S ...
随机推荐
- vmware workstation安装 Mosrosoft Runtime DLL安装程序未能完成安装
不要点确定.开始菜单运行输入'%temp%',在弹出的窗体中找到一个文件名中含'{132E3257-14F1-411A-BC6C-0CA32D3A9BC6}~setup'(不一定一样,反正就是第一行的 ...
- C#
1.类型推导 ; Console.WriteLine(age.GetType().ToString()); var 关键字还可以配合生成匿名类型,如: , Time = }: 如果有可以将对象转成JS ...
- Python数据分析笔记目录
速查笔记 使用实例 Pandas-数据导入 (未完成) Pandas-数据探索 基础属性 shape indexs columns values dtype/dtypes 汇总和计算描述统计 coun ...
- cat命令使用
cat:concatenate files and print on the standard output合并文件并输出 主要用法 1.cat f1.txt,查看f1.txt文件的内容. 2.cat ...
- 关于BCGControlbar16.1版本的安装与使用
csdn上有BCGControlbar16.1版本的下载,地址:http://download.csdn.net/detail/wangxiangdong_sl/4821726带key,个人亲测VS2 ...
- java从基础知识(七)java集合
一.集合类介绍 1.List(元素有放入顺序,可重复) 1.1.List的实现 1.1.1.ArrayList ArrayList就是动态数组(需要连续的存储空间),用MSDN中的说法,就是Array ...
- The Safe Navigation Operator (&.) in Ruby
The most interesting addition to Ruby 2.3.0 is the Safe Navigation Operator(&.). A similar opera ...
- Eclipse使用技巧
1,整体缩进 右缩进:选中+Tab 左缩进:选中+ Shift+Tab 2,Ctrl+O列出当前类所有方法和属性
- web页面跳转的几种方式
可用客户端触发或服务端触发的方式来实现页面跳转. 客户端触发 方式一:使用Javascript 利用window.location对象的href属性.assign()方法或replace()方法来实现 ...
- (转载)html中table的使用方法
colspan表示该一储存格向右打通的栏数. rowspan表示该一储存格向下打通的栏数. colspan是表示横向合并单元格,colspan=“3”表示水平合并三个td rowspan是表示竖直 ...