這是SQL Server 2014我的存儲過程:無效的列名
CREATE PROCEDURE [dbo].[spSelectUserFromProfileUsers]
@TableName NVARCHAR(20),
@User NVARCHAR(20)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @query NVARCHAR(MAX);
SET @query = 'SELECT * FROM ' + QUOTENAME(@TableName) + ' WHERE Users =' + @User
EXECUTE sp_executesql @query
END
,這裏是我的Visual Studio代碼:
if (TableFunctions.doesTableExist(ComboBoxSelectedProfile.SelectedItem + "Users", dbConnectionString))
{
// DynamicSQL
using (SqlCommand command = new SqlCommand("spSelectUserFromProfileUsers", connection))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("@TableName", ComboBoxSelectedProfile.SelectedItem + "Users");
command.Parameters.AddWithValue("@User", TextBoxUserName.Text);
command.ExecuteNonQuery();
}
}
,我得到的錯誤:
Invalid column name /Text that I entered the textbox/
我一直在尋找解決方案很久,我找不到任何東西,我會非常感謝您的幫助!
串聯的結果應該是這樣的:'SELECT * from [YourTable] WHERE Users = Some_User_Name' ....看到問題?,'Some_User_Name'應該是''Some_User_Name'' – Lamak
但如果我添加'@User'它不會將@ User作爲參數 – WeinForce
並且此代碼仍然打開ti sql因爲你只是在sproc中使用連接。 –