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
查看全文
相关阅读:
LeetCode#160-Intersection of Two Linked Lists-相交链表
LeetCode#2-Add Two Numbers-两数相加
LeetCode#141-Linked List Cycle-环形链表
LeetCode#66-Plus One-加一
LeetCode#35-Search Insert Position-搜索插入位置
LeetCode#203-Remove Linked List Elements-移除链表元素
基姆拉尔森公式
[leetcode] 树(Ⅲ)
常用算法合集(一)
离散数学 II(最全面的知识点汇总)
原文地址:https://www.cnblogs.com/chenbg2001/p/1604074.html
最新文章
python基本操作-文件、目录及路径
MinIO 的分布式部署
利用 MinIO 轻松搭建静态资源服务
搞定SpringBoot多数据源(3):参数化变更源
搞定SpringBoot多数据源(2):动态数据源
搞定SpringBoot多数据源(1):多套源策略
沉淀-我的2019
2019读过的好书推荐
java开发必学知识:动态代理
MyBatis批量删除功能实现
热门文章
Maven Jar包下载失败及解决方案
Ajax表单提交方式
Spring+MyBatis+SpringMvc+Mysql+Druid+PageHelper分页实现
使用IDEA实现SSM整合(Maven+Spring+Mybatis+SpringMvc)
给你3分钟白嫖:我写博客常用软件和在线工具
Spring事务管理实现方式(注解,Xml)
深入理解Spring中Bean的生命周期
Spring Aop实现方式(注解和Xml)
Spring IOC 知识点总结
图解大顶堆的构建、排序过程
Copyright © 2011-2022 走看看