我将一个值传递给存储过程,但是它一直给我一个错误,错误是将数据类型nvarchar转换为int。需要更改哪些内容才能删除错误?下面是我的实际调用和存储过程
private static string ReturnPhoneNumber()
{
string username = "Roy Orbinson";
string databaseConnection = "Data Source=EmployeeServer;Initial Catalog=StoreInfo;Integrated Security=True;MultipleActiveResultSets=True";
SqlConnection connection = new SqlConnection(databaseConnection);
SqlCommand command = new SqlCommand("GimmeThatPhoneNumber", connection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@username", username);
connection.Open();
var result = Convert.ToString(command.ExecuteScalar());
connection.Close();
return result;
}
SQL:
[dbo].[GimmeThatPhoneNumber]
(
@username int
)
as
Set NoCount On
SELECT phonenumber
FROM personelinformation
where employeename = @username
答案 0 :(得分:8)
问题是您的输入参数具有int
类型 - 但需要是字符串类型:
[dbo].[GimmeThatPhoneNumber]
(
@username int
)
@username
应该是:
@username nvarchar(200) --pick an appropriate size
答案 1 :(得分:0)
您传递的是@username,其值为username。但是变量username是一个字符串,在您的存储过程中,您将@username定义为整数。 您应该将过程中的@username类型更改为nvarchar,或将字符串username转换为int。