protected void btnOK_Click(object sender,EventArgs e)
{
string strProductId,strName,strCategoryID,strDescn,strImage;
string strConn = "",strSql = "";
strProductId = TextBox1.Text;
strCategoryID = TextBox2.Text;
strName = TextBox3.Text;
strDescn = TextBox4.Text;
strImage = TextBox4.Text;
try
{
strConn = ConfigurationManager.ConnectionStrings["MSPetShop4ConnectionString2"].ToString();
SqlConnection TempConn = new SqlConnection(strConn);
SqlCommand SqlComm = new SqlCommand("addbale",TempConn );
SqlComm.CommandType = CommandType.StoredProcedure;
SqlParameter ProductID = SqlComm.Parameters.Add("@ProductID",SqlDbType.VarChar,10);
ProductID.Value = strProductId;
SqlParameter CategoryID = SqlComm.Parameters.Add("@CategoryID",SqlDbType.VarChar,10);
CategoryID.Value = strCategoryID;
SqlParameter Name = SqlComm.Parameters.Add("@Name",SqlDbType.VarChar,80);
Name.Value = strName;
SqlParameter Descn = SqlComm.Parameters.Add("@Descn",SqlDbType.VarChar,255);
Descn.Value = strDescn;
SqlParameter Image = SqlComm.Parameters.Add("@Image",SqlDbType.VarChar,80);
Image.Value = strImage;
TempConn.Open( );
}
catch
{
throw new ArgumentNullException( );
}该段代码。如果数据合法的话,能顺利插入一条记录,但是如果用户输入了受外键约束的非法数据,插入记录将失败。但是怎么在客户端判断,记录是否插入成功了,存储过程中倒是做了判断,但是无法在C#代码中获取存储过程的错误代码?存储过程如下:Create procedure addbale
@ProductId varchar(10),
@CategoryId varchar(10),
@Name varchar(80),
@Descn varchar(255),
@Image varchar(80)
as
begin
INSERT INTO Product VALUES(@ProductId,@CategoryId,@Name,@Descn,@Image)if @@ROWCOUNT=0
begin
raiserror('error',16,1)
rollback transaction
endend
{
string strProductId,strName,strCategoryID,strDescn,strImage;
string strConn = "",strSql = "";
strProductId = TextBox1.Text;
strCategoryID = TextBox2.Text;
strName = TextBox3.Text;
strDescn = TextBox4.Text;
strImage = TextBox4.Text;
try
{
strConn = ConfigurationManager.ConnectionStrings["MSPetShop4ConnectionString2"].ToString();
SqlConnection TempConn = new SqlConnection(strConn);
SqlCommand SqlComm = new SqlCommand("addbale",TempConn );
SqlComm.CommandType = CommandType.StoredProcedure;
SqlParameter ProductID = SqlComm.Parameters.Add("@ProductID",SqlDbType.VarChar,10);
ProductID.Value = strProductId;
SqlParameter CategoryID = SqlComm.Parameters.Add("@CategoryID",SqlDbType.VarChar,10);
CategoryID.Value = strCategoryID;
SqlParameter Name = SqlComm.Parameters.Add("@Name",SqlDbType.VarChar,80);
Name.Value = strName;
SqlParameter Descn = SqlComm.Parameters.Add("@Descn",SqlDbType.VarChar,255);
Descn.Value = strDescn;
SqlParameter Image = SqlComm.Parameters.Add("@Image",SqlDbType.VarChar,80);
Image.Value = strImage;
TempConn.Open( );
}
catch
{
throw new ArgumentNullException( );
}该段代码。如果数据合法的话,能顺利插入一条记录,但是如果用户输入了受外键约束的非法数据,插入记录将失败。但是怎么在客户端判断,记录是否插入成功了,存储过程中倒是做了判断,但是无法在C#代码中获取存储过程的错误代码?存储过程如下:Create procedure addbale
@ProductId varchar(10),
@CategoryId varchar(10),
@Name varchar(80),
@Descn varchar(255),
@Image varchar(80)
as
begin
INSERT INTO Product VALUES(@ProductId,@CategoryId,@Name,@Descn,@Image)if @@ROWCOUNT=0
begin
raiserror('error',16,1)
rollback transaction
endend
解决方案 »
- 【求助】遍历Repeater行中的HTML控件,如何赋值、取值?
- 关于一些DataAdapter的一些应用
- C/S和B/S的 利弊有哪些以及区别
- 关于<form action=""> ,没有Codebehind="***.aspx.cs" ,<input type=submit name=name1>提交表单时谁来来处理表单?
- SQL语句错误,在线等待~~~~~~~~
- Microsoft.ApplicationBlocks.Data.dll
- 关于正则表达式(用户名)
- 如何得到 用户和你的网站的连接速度
- asp.net(c#)取sql sever 中 image
- asp的Session问题,找不到Asp板块,就在这里提问了。
- 如何获取HTML字段中,所有URL地址(过滤掉其它所有)
- oracle null->0
@ProductId varchar(10),
@CategoryId varchar(10),
@Name varchar(80),
@Descn varchar(255),
@Image varchar(80),
@Result int out --参数
as
begin
set @Result =0 --初始化
INSERT INTO Product VALUES(@ProductId,@CategoryId,@Name,@Descn,@Image) if @@ROWCOUNT=0
begin
set @Result =1
raiserror( 'error ',16,1)
rollback transaction
end end 代码里面接收到 @Result 的值来判断
{
//操作代码
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}你可以通这在存储过程中添加输出参数来把存储过程中的信息返回到程序里,比如:
Create procedure addbale
@strMessage varchar(10) output,
...
as
if error<>0
begin
set @strMessage="error message";
rollback transaction
end 在程序里通过SqlParameter.Direction的值为Output来获取这个信息