存储过程系列之存储过程sql数据库调用和程序代码调用

 

1、存储过程,无参数的存储过程

创建无参数存储存储过程

Create Procedure DCEMREMR_TEMPLATE
As
SELECT TOP 10 [FILENAME],[FILETITLE],[FILECONTENT] from [DCEMR].[dbo].[EMR_TEMPLATE];

调用无参数存储存储过程

sql 数据库中的额调用  exec DCEMREMR_TEMPLATE;

sql程序代码调用

//无参数存储过程
string connecting = "Data Source=localhost;Integrated Security=SSPI;Initial Catalog=DCEMR";
SqlConnection theConnect = new SqlConnection(connecting);
theConnect.Open();
SqlCommand theCommand = theConnect.CreateCommand();
theCommand.CommandText = "DCEMREMR_TEMPLATE";
theCommand.CommandType = CommandType.StoredProcedure;
SqlDataReader theReader = theCommand.ExecuteReader();
while (theReader.Read())
{
string xx = theReader.GetString(0).ToString();
}
theConnect.Close();

 

2、有参数存储过程,无返回值

创建有参数存储存储过程,无返回值

Go
Create Procedure DCEMREMRTEMPLATE100
@filename nvarchar(500)
As
SELECT [FILENAME],[FILETITLE],[FILECONTENT] from [DCEMR].[dbo].[EMR_TEMPLATE] where [FILEname]=@filename;

调用有参数存储过程,无返回值

sql 数据库中的额调用  exec DCEMREMRTEMPLATE100 ‘新建目录’;

sql程序代码调用

//有参数存储过程,无返回值
string connecting = "Data Source=localhost;Integrated Security=SSPI;Initial Catalog=DCEMR";
SqlConnection theConnect = new SqlConnection(connecting);
theConnect.Open();
SqlCommand theCommand = theConnect.CreateCommand();
theCommand.CommandText = "DCEMREMRTEMPLATE101";
theCommand.CommandType = CommandType.StoredProcedure;
theCommand.Parameters.Add("@filename",SqlDbType.NVarChar);
theCommand.Parameters["@filename"].Value = "新建目录";
SqlDataReader theReader = theCommand.ExecuteReader();
while (theReader.Read())
{
string xx = theReader.GetString(0).ToString();
}
theConnect.Close();

3、有参数存储过程,有返回值(参数@filename,返回参数@Rowcount)

创建有参数存储过程,有返回值

Go
Create Procedure DCEMREMRTEMPLATE101
@filename nvarchar(500),
@Rowcount int output
As
SELECT [FILENAME],[FILETITLE],[FILECONTENT] from [DCEMR].[dbo].[EMR_TEMPLATE] where [FILEname]=@filename
set @Rowcount=@Rowcount;

调用参数存储存储过程,有返回值

sql 数据库中的额调用  exec DCEMREMRTEMPLATE101 ‘新建目录’,2;

sql程序代码调用

//有参数存储过程
string connecting = "Data Source=localhost;Integrated Security=SSPI;Initial Catalog=DCEMR";
SqlConnection theConnect = new SqlConnection(connecting);
theConnect.Open();
SqlCommand theCommand = theConnect.CreateCommand();
theCommand.CommandText = "DCEMREMRTEMPLATE101";
theCommand.CommandType = CommandType.StoredProcedure;
theCommand.Parameters.Add("@filename",SqlDbType.NVarChar);
theCommand.Parameters["@filename"].Value = "新建目录";
theCommand.Parameters.Add("@Rowcount", SqlDbType.Int);
theCommand.Parameters["@Rowcount"].Direction = ParameterDirection.Output;
//theCommand.Parameters["@Rowcount"].Value = 2;

//theCommand.ExecuteNonQuery();
object ss = theCommand.ExecuteScalar();
//MessageBox.Show( theCommand.Parameters["@Rowcount"].Value.ToString());
SqlDataReader theReader = theCommand.ExecuteReader();
while (theReader.Read())
{
string xx = theReader.GetString(0).ToString();
}
theConnect.Close();

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值