gpt4 book ai didi

c# - "Error converting data type nvarchar to int."执行存储过程

转载 作者:太空宇宙 更新时间:2023-11-03 10:49:56 24 4
gpt4 key购买 nike

我有一个存储过程,但从 C# 代码执行它时出现此错误。

Error converting data type nvarchar to int.

我的存储过程:

CREATE PROCEDURE [dbo].[donateBloodProc] 
@donorName varchar(50),
@gender varchar(20),
@email varchar(50),
@bloodGroup varchar(50),
@contact_number int, @L_D_D date,
@city varchar(50),
@arid_number varchar(50),
@DepId int
AS
INSERT INTO
tblDonors(Dname, gender, bloodGroup, email, contact_number,
L_D_D,city,arid_number,DeptID,dateDonated)
VALUES
(@donorName,@gender,@bloodGroup,@email,@contact_number,
@L_D_D,@city,@arid_number,@DepId,GETDATE());

这是我的 C# 代码:

SqlCommand commandForSP = new SqlCommand("donateBloodProc",DALobj.openConnection());
commandForSP.CommandType = System.Data.CommandType.StoredProcedure;

commandForSP.Parameters.AddWithValue("@donorName", TextBox1.Text);
commandForSP.Parameters.AddWithValue("@gender", RadioButtonList1.SelectedValue);
commandForSP.Parameters.AddWithValue("@email", TextBox5.Text);
commandForSP.Parameters.AddWithValue("@bloodGroup", DropDownList1.SelectedValue);
commandForSP.Parameters.AddWithValue("@contact_number", TextBox2.Text);
commandForSP.Parameters.AddWithValue("@L_D_D", TextBox3.Text);
commandForSP.Parameters.AddWithValue("@city", DropDownList2.SelectedValue);
commandForSP.Parameters.AddWithValue("@arid_number", TextBox1.Text);
commandForSP.Parameters.AddWithValue("@DepId", TextBox4.Text);

commandForSP.ExecuteNonQuery();

我的存储过程中所有列的数据类型都与我的数据库表 tblDonors

最佳答案

正如@bejger 所说,这是因为您发送的是字符串而不是 int 到 contact 和 depid 参数。

但我认为发送 INT 列的 String/VARCHAR 值是可以接受的,如果它是一个有效的整数,它不会抛出异常。

当提供的字符串无法转换为整数时抛出异常。因此我建议您在将值发送到参数之前使用 Int32.TryParse() 方法执行解析。

Int32.TryParse() 方法如果解析成功则返回 true,否则返回 false。

完整代码:

if(!Int32.TryParse(TextBox2.Text,out var contact))
{
//Error here you can return or display some warning
return;
}

if(!Int32.TryParse(TextBox4.Text,out var depid))
{
//Error here you can return or display some warning
return;
}

SqlCommand commandForSP = new SqlCommand("donateBloodProc",DALobj.openConnection());
commandForSP.CommandType = System.Data.CommandType.StoredProcedure;

commandForSP.Parameters.AddWithValue("@donorName", TextBox1.Text);
commandForSP.Parameters.AddWithValue("@gender", RadioButtonList1.SelectedValue);
commandForSP.Parameters.AddWithValue("@email", TextBox5.Text);
commandForSP.Parameters.AddWithValue("@bloodGroup", DropDownList1.SelectedValue);
commandForSP.Parameters.AddWithValue("@contact_number",contact);
commandForSP.Parameters.AddWithValue("@L_D_D", TextBox3.Text);
commandForSP.Parameters.AddWithValue("@city", DropDownList2.SelectedValue);
commandForSP.Parameters.AddWithValue("@arid_number", TextBox1.Text);
commandForSP.Parameters.AddWithValue("@DepId", depid);

commandForSP.ExecuteNonQuery();

关于c# - "Error converting data type nvarchar to int."执行存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21795045/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com