存储过程

lude8880 2008-09-11 01:58:55
怎么从c#里面运行一个存储过程,给他参数然后将结果村到一个变量里?
...全文
138 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
yagebu1983 2008-09-11
  • 打赏
  • 举报
回复
已经有正解,
接分!!
lijin84100 2008-09-11
  • 打赏
  • 举报
回复


[Quote=引用 4 楼 ZengHD 的回复:]
SQL codeCREATE PROCEDURE GetCount
@iReturn int output
AS

select @iReturn =count(1) from t1
return 111
GO




C# code private void button1_Click_1(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=db;Persist Security Info=True;User ID=sa;Password=12345;");
SqlCommand cmd = new SqlComma…
[/Quote]
这种方法可行
wzy_love_sly 2008-09-11
  • 打赏
  • 举报
回复
过程设置 @return out
parameters[index].Direction = ParameterDirection.Output;
C#执行过程后
string returnValue = parameters[index].Value.ToString();
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 ZengHD 的回复:]
SQL codeCREATEPROCEDUREGetCount@iReturnintoutputASselect@iReturn=count(1)fromt1return111GO

C# codeprivatevoidbutton1_Click_1(objectsender, EventArgs e)
{
SqlConnection conn=newSqlConnection("Data Source=127.0.0.1;Initial Catalog=db;Persist Security Info=True;User ID=sa;Password=12345;");
SqlCommand cmd=newSqlCommand("GetCount", conn);

cmd.CommandTy…
[/Quote]
这个够用了
zzyhuian06142 2008-09-11
  • 打赏
  • 举报
回复
SqlConnection conn = new SqlConnection(连接字符);
SqlCommand cmd = new SqlCommand(存储过程名称);
cmd.CommandType=CommandType.StoreProc;
cmd.Parameters.add("参数名",类型);
cmd.Parameters["参数名"].Direction = out或者input//out就是输出,input就是输入的一样
大概的就是这样的(手动写的,有点单词可能错了)
ZengHD 2008-09-11
  • 打赏
  • 举报
回复
CREATE PROCEDURE GetCount 
@iReturn int output
AS

select @iReturn =count(1) from t1
return 111
GO


        private void button1_Click_1(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=db;Persist Security Info=True;User ID=sa;Password=12345;");
SqlCommand cmd = new SqlCommand("GetCount", conn);

cmd.CommandType = CommandType.StoredProcedure;


SqlParameter[] parm = {new SqlParameter("@iReturn",SqlDbType.Int),
new SqlParameter("@XX",SqlDbType.NVarChar)
};

//输出参数
parm[0] = cmd.Parameters.Add("@iReturn", SqlDbType.Int);
parm[0].Direction = ParameterDirection.Output;

//返回值
parm[1] = cmd.Parameters.Add("@XX", SqlDbType.Int);
parm[1].Direction = ParameterDirection.ReturnValue;

conn.Open();

SqlDataReader reader = cmd.ExecuteReader();

MessageBox.Show(parm[0].Value.ToString() + " " + parm[1].Value.ToString());

reader.Close();
conn.Close();
}
ZengHD 2008-09-11
  • 打赏
  • 举报
回复
传到存储过程里?
宝_爸 2008-09-11
  • 打赏
  • 举报
回复
使用输出参数.
loucc 2008-09-11
  • 打赏
  • 举报
回复
就和用SELECT语句是一样的

110,579

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧