zoukankan
html css js c++ java
C#中调用SQL存储过程(带输入输出参数的例子)
Code
1
using
System;
2
using
System.Collections.Generic;
3
using
System.Text;
4
using
System.Data;
5
using
System.Data.SqlClient;
6
7
namespace
StoreProduceTest
8
{
9
public
class
Program
10
{
11
/**/
/*
*
12
* 存储过程
13
*
14
* create procedure queryStuNameById
15
@stuId varchar(10),--输入参数
16
@stuName varchar(10) output --输出参数
17
as
18
select @stuName=stuName from stuInfo where stuId=@stuId
19
*
20
*/
21
22
23
static
void
Main(
string
[] args)
24
{
25
Operater op
=
new
Operater();
26
string
name
=
op.QueryStuNameById(
"
1234
"
);
27
28
Console.WriteLine(
string
.Format(
"
学号为1234的学生的姓名为{0}
"
, name));
29
}
30
31
}
32
33
public
class
Operater
34
{
35
private
string
ConStr
=
"
server=.;database=User;uid=sa;pwd=1234
"
;
36
private
SqlConnection sqlCon
=
null
;
37
private
SqlCommand sqlComm
=
null
;
38
SqlDataReader dr
=
null
;
39
40
public
string
QueryStuNameById(
string
Id)
41
{
42
43
string
name
=
""
;
44
45
try
46
{
47
using
(sqlCon
=
new
SqlConnection(ConStr))
48
{
49
50
sqlCon.Open();
51
sqlComm
=
new
SqlCommand(
"
queryStuNameById
"
, sqlCon);
52
//
设置命令的类型为存储过程
53
sqlComm.CommandType
=
CommandType.StoredProcedure;
54
55
//
设置参数
56
sqlComm.Parameters.Add(
"
@stuId
"
, SqlDbType.VarChar);
57
//
注意输出参数要设置大小,否则size默认为0,
58
sqlComm.Parameters.Add(
"
@stuName
"
, SqlDbType.VarChar,
10
);
59
//
设置参数的类型为输出参数,默认情况下是输入,
60
sqlComm.Parameters[
"
@stuName
"
].Direction
=
ParameterDirection.Output;
61
62
//
为参数赋值
63
sqlComm.Parameters[
"
@stuId
"
].Value
=
"
1234
"
;
64
//
执行
65
sqlComm.ExecuteNonQuery();
66
//
得到输出参数的值,把赋值给name,注意,这里得到的是object类型的,要进行相应的类型轮换
67
name
=
sqlComm.Parameters[
"
@stuName
"
].Value.ToString();
68
69
}
70
71
}
72
catch
(Exception ex)
73
{
74
75
Console.WriteLine(ex.ToString());
76
}
77
return
name;
78
79
}
80
}
81
82
}
83
84
查看全文
相关阅读:
Java设计模式概述之结构型模式(装饰器模式)
Java设计模式概述之结构型模式(代理模式)
Java设计模式概述之结构型模式(适配器模式)
Java设计模式概述之创建型模式
小诀窍
iframe的一种应用场景
linux网络
ANT
Eclipse使用
mac 安装tomcat
原文地址:https://www.cnblogs.com/chenbg2001/p/1604074.html
最新文章
SQL 重命名
List Schema Name and Table Name for Database
SQL Server 基础语句学习(二)
Deploy Web Application 遇到的几个问题
azkaban配置
kafka和flume集成
kafka新旧配置文件详解及API
kafka配置及基本命令
flume中自定义sink InterCeptor
Avro和protobuf序列化
热门文章
flume的配置详解
sqoop数据导入导出工具
hadoop的负载监控软件ganglia
Hbase的jdbc工具phoenix
Java设计模式之行为型模式(命令模式)
Java设计模式之行为型模式(责任链模式)
Java设计模式概述之结构型模式(桥接模式)
Java设计模式概述之结构型模式(外观模式)
Java设计模式概述之结构型模式(享元模式)
Java设计模式概述之结构型模式(组合模式)
Copyright © 2011-2022 走看看