如果從C#輸入的值不是NULL
,我想更新表TBL_Log
的任何列。這裏是我的存儲過程:如果值不爲NULL,如何更新列
Alter PROCEDURE [dbo].[SP_Update_User]
(@User_id as int,@User_Names as nvarchar(max),@End as char(8),@Start as nvarchar(max) ,@Count as int)
AS
BEGIN
UPDATE [dbo].[TBL_Log]
SET User_Names = @User_Names
,[Start] = @start
,[End] = @End
,[Count] = @Count
where User_id = @User_id
END
我試圖使這項工作,但一直沒有成功。類D1
代碼:
public static DataSet Update_User(int @User_id, string @User_Names, string @End, string @Start, int @Count)
{
SqlConnection myConnection = new SqlConnection(strConecctionString);
SqlDataAdapter sqlcmd = new SqlDataAdapter("SP_Update_UserData_Bot", myConnection);
sqlcmd.SelectCommand.CommandType = CommandType.StoredProcedure;
SqlParameter parameterID_category_ID = new SqlParameter("@User_id", SqlDbType.Int);
parameterID_category_ID.Value = User_id;
sqlcmd.SelectCommand.Parameters.Add(parameterID_category_ID);
SqlParameter parameterID_Part_ID = new SqlParameter("@User_Names", SqlDbType.Int);
parameterID_Part_ID.Value = User_Names;
sqlcmd.SelectCommand.Parameters.Add(parameterID_Part_ID);
SqlParameter parameterID_Series_ID = new SqlParameter("@End", SqlDbType.Char);
parameterID_Series_ID.Value = End;
sqlcmd.SelectCommand.Parameters.Add(parameterID_Series_ID);
SqlParameter parameterID_Model_ID = new SqlParameter("@start", SqlDbType.NVarChar);
parameterID_Model_ID.Value = start;
sqlcmd.SelectCommand.Parameters.Add(parameterID_Model_ID);
SqlParameter parameterID_Count = new SqlParameter("@Count", SqlDbType.Int);
parameterID_Count.Value = Count;
sqlcmd.SelectCommand.Parameters.Add(parameterID_Count);
sqlcmd.SelectCommand.CommandTimeout = int.MaxValue;
DataSet DS = new DataSet();
sqlcmd.Fill(DS);
return DS;
}
你的問題還不清楚。你是說如果C#中提供的'@ End'值不是'null',那麼你想要將所有列的End''爲'NULL'的行更新爲'@ End',對於所有其他參數也是如此?這些參數是獨立的,即「@ Start」隻影響其中'Start'爲NULL的行,而不考慮'@ End'或'End'? – HABO
不是你的問題的答案,而是你應該閱讀的文章。 http://sqlperformance.com/2012/10/t-sql-queries/sp_prefix –