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
查看全文
相关阅读:
转载php在IIS中运行
程序员必去的网站
分享一下jQuery UI的地址
dbcp相关配置
shell学习第二弹-进阶
shell学习第一弹-初识
java servlet 3.0文件上传
Junit使用第二弹
各个数据库中,查询前n条记录的方法
junit使用第一弹
原文地址:https://www.cnblogs.com/chenbg2001/p/1604074.html
最新文章
MVC动态赋值的td选中,获取当前的td的ID或者值
MVC辅助方法
WebServer+ADO+百万数据查询
C++ 回调函数的定义与用法
TreeView 使用方法:(在View.Details模式下)
oracle创建自增长列
转载 求集合笛卡尔积
php连接oracle10数据库 转载
php连接oracle数据库转载
apache与IIS端口冲突修改和需要使用 SSL 查看该资源”错误
热门文章
C#取得当前目录 转载
数据库连接字符串转载
创建表空间及用户赋予表空间权限
oracle创建用户赋予权限
zendstudio 出现failed to create the java machine转
php连接mysql配置
转载linq to sql 的详解
linq to sql转载
要想清楚剪切板内容
转载sublime text3中package Control
Copyright © 2011-2022 走看看