- 1
- 0
- 约1.22万字
- 约 19页
- 2017-09-12 发布于河南
- 举报
调用存储过程方法 代码
if exists(select * from sysobjects where name=SelectStu)
drop proc SelectStu
GO
create proc SelectStu
as
select * from student
GO
--c#
SqlCommand cmd = new SqlCommand(SelectStu,con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataReader dr = cmd.ExecuteReader();
while(dr.Read())
{
Console.WriteLine(dr[LoginId]);
}
--T-SQL 带一个参数
if exists(select * from sysobjects where name=SelectStu)
drop proc SelectStu
GO
create proc SelectStu
@name varchar(50)
as
select * from student where loginid=@name
GO
--C#
SqlCommand cmd = new SqlCommand(SelectStu,con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(@name, LiDifei);
SqlDataReader dr = cmd.ExecuteReader();
while(dr.Read())
{
Console.WriteLine(dr[LoginId]);
}
--T-SQL 有返回值
if exists(select * from sysobjects where name=SelectStu)
drop proc SelectStu
GO
create proc SelectStu
@name varchar(50),
@id int output
as
select @id=studentid from student where loginid=@name
return @id
GO
--C#
SqlCommand cmd = new SqlCommand(SelectStu,con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(@name, LiDifei);
cmd.Parameters.Add(@id,SqlDbType.Int);
cmd.Parameters[@id].Direction = ParameterDirection.Output;
cmd.ExecuteScalar();
Console.WriteLine(cmd.Parameters[@id].Value);
//以前写过的一个调用存储过程的方法
public static int regInsert(string Pwd, int Friend, string NickName, int FaceID, string Sex, int Age, string Name, int starId, int BloodTypeId)
{
try
{
SqlConnection conn = createConnection();
conn.Open();
SqlCommand cmd = new SqlCommand(proc_reg, conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(@id,SqlDbType.Int);
如果是id是字符型的话,必须把
原创力文档

文档评论(0)