关于DbhelperSql对存储过程的操作

小_虎 2009-02-13 04:00:39
        private static SqlCommand BuildQueryCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters)
{
SqlCommand command = new SqlCommand(storedProcName, connection);
command.CommandType = CommandType.StoredProcedure;
foreach (SqlParameter parameter in parameters)
{
if (parameter != null)
{
// 检查未分配值的输出参数,将其分配以DBNull.Value.
if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
(parameter.Value == null))
{
parameter.Value = DBNull.Value;
}
command.Parameters.Add(parameter);
}
}

return command;
}

/// <summary>
/// 执行存储过程,返回影响的行数
/// </summary>
/// <param name="storedProcName">存储过程名</param>
/// <param name="parameters">存储过程参数</param>
/// <param name="rowsAffected">影响的行数</param>
/// <returns></returns>
public static int RunProcedure(string storedProcName, IDataParameter[] parameters, out int rowsAffected)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
int result;
connection.Open();
SqlCommand command = BuildIntCommand(connection, storedProcName, parameters);
rowsAffected = command.ExecuteNonQuery();
result = (int)command.Parameters["ReturnValue"].Value;
//Connection.Close();
return result;
}
}

/// <summary>
/// 创建 SqlCommand 对象实例(用来返回一个整数值)
/// </summary>
/// <param name="storedProcName">存储过程名</param>
/// <param name="parameters">存储过程参数</param>
/// <returns>SqlCommand 对象实例</returns>
private static SqlCommand BuildIntCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters)
{
SqlCommand command = BuildQueryCommand(connection, storedProcName, parameters);
command.Parameters.Add(new SqlParameter("ReturnValue",
SqlDbType.Int, 4, ParameterDirection.ReturnValue,
false, 0, 0, string.Empty, DataRowVersion.Default, null));
return command;
}
#endregion


关于上面的"执行存储过程,返回影响的行数"我怎么搞不太明白,
result = (int)command.Parameters["ReturnValue"].Value;
//Connection.Close();
return result;

command.Parameters.Add(new SqlParameter("ReturnValue",
SqlDbType.Int, 4, ParameterDirection.ReturnValue,
false, 0, 0, string.Empty, DataRowVersion.Default, null));
return command;
结合看起来,是当我们写的存储过程里面要写了有返回值,然后才会返回那个值吧,怎么是返回受影响的行数呢..

比如我写了一个插入表的存储过程,我现在应该调用这个RunProcedure方法吧..
很郁闷的是一个我不太明白,一个是我出错了...不知道为什么会有:在将 varchar 值 'abc' 转换成数据类型 int 时失败。
...全文
225 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
小_虎 2009-02-16
  • 打赏
  • 举报
回复
the_pain 2009年02月13日 16点30分33秒 说:
贴错了。这个才是的。
the_pain 2009年02月13日 16点30分45秒 说:
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<asp:TextBox id="txtid" style="Z-INDEX: 101; LEFT: 240px; POSITION: absolute; TOP: 96px" runat="server"></asp:TextBox>
<asp:TextBox id="txtname" style="Z-INDEX: 102; LEFT: 240px; POSITION: absolute; TOP: 136px" runat="server"&
the_pain 2009年02月13日 16点31分03秒 说:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace WebApplication1
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox txtid;
protected System.Web.UI.WebControls.TextBox txtname;
protected System.Web.UI.WebControls.TextBox txtage;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.Button Button1;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
the_pain 2009-02-13
  • 打赏
  • 举报
回复
default.aspx


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>无标题页 </title>
<script type="text/javascript">

function createButton()
{
var nod=document.getElementById("div1");//这个div1是页面上的层
nod.innerHTML=" <input id='Button100' type='button' value='让文本框显示' onclick='aaa('cccccccc')' />";
}
function aaa(sUrl)
{
var text1=document.getElementById("Text1");//Text1是页面上的文本框
text1.value=sUrl;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<input id="Button1" type="button" value="新建按钮" onclick="createButton()" />
<div id="div1" style="border:solid 1px #336699;"> </div>
<input id="Text1" type="text" />
</form>
</body>
</html>
the_pain 2009-02-13
  • 打赏
  • 举报
回复
sql:

create database stu

use stu

create table student
(
id int primary key,
name varchar(10),
age int
)

create procedure upStuAdd
@id int,
@name varchar(10),
@age int
as
insert into student values(@id,@name,@age)

exec upStuAdd 1,'dick',23


create procedure upStuQuery
@id int,
@name varchar(10) output
as

select @name=name from student where id=@id

if @name is null
return 0
else
return 1


select * From student


declare @name varchar(100)
exec upStuQuery 1, @name output
print @name



the_pain 2009-02-13
  • 打赏
  • 举报
回复
你要有返回值的话要在存储过程里面定义一个output参数.
小_虎 2009-02-13
  • 打赏
  • 举报
回复
下了看了,没什么用处...
小_虎 2009-02-13
  • 打赏
  • 举报
回复
看下这个例子.
http://download.csdn.net/source/630313

汗你一个,好贵..下了
小_虎 2009-02-13
  • 打赏
  • 举报
回复
已经发现那个
:在将 varchar 值 'abc' 转换成数据类型 int 时失败。
the_pain 2009-02-13
  • 打赏
  • 举报
回复
看下这个例子.
http://download.csdn.net/source/630313
小_虎 2009-02-13
  • 打赏
  • 举报
回复
我再把存储过程:贴出来:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go


ALTER procedure [dbo].[AddProposer]
@Proposer_Name varchar(30),
@phone1 int,
@Company varchar(255),
@Address varchar(255),
@Jzmj int,
@Postalcode int
as
insert into Proposer_info(
Proposer_Name,
Company,
PhoneNo,
Address,
Jzmj,
Postalcode
)
values
(
@Proposer_Name,
@phone1,
@Company,
@Address,
@Jzmj,
@Postalcode
)




public int AddProposer(string Proposer_Name, int phone1, string Company, string Address, int Jzmj, int Postalcode)
{
SqlDataBase MyDb = new SqlDataBase();
SqlParameter[] parameters={
new SqlParameter("@Proposer_Name",SqlDbType.VarChar,30),
new SqlParameter("@phone1",SqlDbType.Int),
new SqlParameter("@Company",SqlDbType.VarChar,255),
new SqlParameter("@Address",SqlDbType.VarChar,255),
new SqlParameter("@Jzmj",SqlDbType.Int),
new SqlParameter("@Postalcode",SqlDbType.Int)};
parameters[0].Value=Proposer_Name;
parameters[1].Value=phone1;
parameters[2].Value=Company;
parameters[3].Value=Address;
parameters[4].Value=Jzmj;
parameters[5].Value=Postalcode;
return MyDb.RunInsert("AddProposer", parameters);//这里是调用另个一个DbhelpSql,也差不多
页面上,我是这样调用的
protected void Button1_Click(object sender, EventArgs e)
{
default1 de=new default1();
de.AddProposer(txtName.Text.ToString(), Convert.ToInt32(txtPhone1.Text.ToString()), txtCompany.Text.ToString(), txtAddress.Text.ToString().ToString(), Convert.ToInt32(txtJzmj.Text.ToString()), Convert.ToInt32(txtPostalcode.Text.ToString()));
}

62,025

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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