我有這個存儲過程:轉換數據類型爲nvarchar成int
CREATE PROCEDURE SearchEmployee
@EmployeeID int,
@EmployeeName nvarchar(256),
@Address nvarchar(256),
AS
Select
EmployeeId, EmployeeName, Address
From
Employee
Where
EmployeeID = @EmployeeID OR
EmployeeName LIKE '%' + @EmployeeName+ '%' OR
Address LIKE '%' + @Address+ '%'
然後在我的WinForms我這樣稱呼它:
using (SqlCommand mySqlCommand1 = new SqlCommand("dbo.SearchEmployee", myDatabaseConnection))
{
mySqlCommand1.CommandType = CommandType.StoredProcedure;
{
mySqlCommand1.Parameters.AddWithValue("@EmployeeID", textBox1.Text);
mySqlCommand1.Parameters.AddWithValue("@EmployeeName", textBox1.Text);
mySqlCommand1.Parameters.AddWithValue("@Address", textBox1.Text);
}
}
我將如何避免錯誤
將數據類型nvarchar轉換爲int時出錯。
當用戶輸入員工姓名而不是EmployeeID
?
EmployeeID = cast(@EmployeeID as varchar(10))
因爲我只用一個searchTextBox會發現每個字段。非常感謝 :) –