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 ...
随机推荐
- FSM(状态机)、HFSM(分层状态机)、BT(行为树)的区别
游戏人工智能AI中最常听见的就是这三个词拉: FSM 这个不用说拉,百度一大堆解释, 简单将就是将游戏AI行为分为一个一个的状态,状态与状态之间的过渡通过事件的触发来形成. 比如士兵的行为有“巡逻”, ...
- leetcode算法分类
利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...
- 关于firefox对font awesome本地环境无法加载问题
问题描述 昨天尝试使用font awesome加载字体图标,直接在本地引入相关文件,测试发现图标在chrome和IE环境支持,但是在firefox上怎么都显示不出来. 解决方法 通过测试发现通过htt ...
- Python 简易聊天机器人
聊天机器人 | |-----MySql | |---module--"逻辑运算层" | | | |---ciku--"与词库交互" | | | |---dict ...
- 如何让两个div在同一行显示?一个float搞定
最近在学习div和css,遇到了一些问题也解决了很多以前以为很难搞定的问题.比如:如何让两个div显示在同一行呢?(不是用table表格,table对SE不太友好)其实,<div> 是一个 ...
- 项目vue2.0仿外卖APP(五)
header组件 vue-resourse应用 https://github.com/pagekit/vue-resource vue-resource是Vue.js的一款插件,它可以通过XMLHtt ...
- 在WPF中使用WinForm控件方法
1. 首先添加对如下两个dll文件的引用:WindowsFormsIntegration.dll,System.Windows.Forms.dll. 2. 在要使用WinForm控 ...
- Microsoft QAS架接项目
1,p位置玩文件后.运行程序命令是: QCSQueryLabelWithLES.exe -c %CD%\FinalQASModelDir --variant AMyMovie --outputFull ...
- yum 保存下载包
--- 1 --- $ sudo yum install yum-plugin-downloadonly $ sudo yum install --downloadonly --downloaddir ...
- Markdown基本语法
Markdown 基本语法记录 # 欢迎使用 Cmd Markdown 编辑阅读器 ------ 我们理解您需要更便捷更高效的工具记录思想,整理笔记.知识,并将其中承载的价值传播给他人,**Cmd M ...