在c#中的取得sql存储过程的output参数
一直没有找到一种好的方法来引用有返回值的存储过程的方法,使我在添加数据中走了不少的弯路,最近,在查阅了大量的资料之后,终于在微软的一个实例中找到了一种良好的方法。
首先编写好一有返回值的存储过程
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 --返回是否成功添加数据
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,
DataRowVerstion.Default,
null) //参数值,输入参数时需提供
);
command.commandtype=commandtype.StoredProcedure;
command.connection.open();
command.executenonQuery();
int pkid=(int)command.Parameters["@para2"].value; //得到输出参数的值
command.connection.close();
此处是引用输出参数,如果要引用返回值(是否成功添加数据)则只需把ParamenterDirec
相关文档:
SQL语句的优化就是将性能较低的SQL语句转换达成同样目的性能优异的SQL语句
下面我们一起来看看一些可以优化SQL的方法,希望大家多提出意见我们共同学习或者是大家有什么好的优化方法可以提出来共享一下。
第一种优化(使用指定列代替”*”)
使用“*&rdquo ......
查询优化的目的是提高数据检索速度,提高数据检索意味着减少磁盘
IO
读取或者逻辑内存读取次数,这需要从两个方面入手:数据要尽可能的缓存到内存、尽可能的使用索引。内存的问题可以参见
:
http://msdn.microsoft.com/zh-cn/library/ms188284.aspx
,本文主要是体现如何使用索引来提高速度。具体方法:
1) ......
1=1或者'a'='a'等等恒等式是T-SQL中表达true的方法。因为在T-SQL中没有true这样的关键
字或值,所以需要借助这些恒等式来表达true的概念。
相对的,同样可以使用1<>1或者1=2等来表达false。
在应用程式的安全性方面,使用这些式子是SQL注入的基本原理,所以在拼接SQL语句的时候要过滤各种各样的敏感字
符。
当然 ......
using System.Data.SqlClient;
using System.Data.OleDb;
private void button1_Click(object sender, EventArgs e)
{
OleDbConnection conn = null;
try
&nbs ......
create table "user" (
id int identity,
constraint PK_USER prim ......