2017-01-09 75 views
0

如果從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; 

    } 
+0

你的問題還不清楚。你是說如果C#中提供的'@ End'值不是'null',那麼你想要將所有列的End''爲'NULL'的行更新爲'@ End',對於所有其他參數也是如此?這些參數是獨立的,即「@ Start」隻影響其中'Start'爲NULL的行,而不考慮'@ End'或'End'? – HABO

+0

不是你的問題的答案,而是你應該閱讀的文章。 http://sqlperformance.com/2012/10/t-sql-queries/sp_prefix –

回答

3

這將只更新非空的值。如果該值爲空,則該列將更新回其自己的值。

UPDATE [dbo].[TBL_Log] 
SET User_Names = isnull(@User_Names, User_Names) 
    , [Start] = isnull(@start, [Start]) 
    , [End] = isnull(@End, [End]) 
    , [Count] = isnull(@Count, [Count]) 
where User_id = @User_id 
0

你註釋掉的邏輯是罰款:

UPDATE [dbo].[TBL_Log] 
    SET User_Names = @User_Names, 
     [Start] = @start, 
     [End] = @End, 
     [Count] = @Count 
     where User_id = @User_id and 
      (@User_Names is not null and 
      @AkharinBazdid is not null and 
      @Pid_Bazdid IS NOT NULL and 
      @C_Bazdid IS NOT NULL 
      ); 

您也可以使用IF如果你喜歡。

注:

  • 不要使用SQL關鍵字和保留字作爲列名。轉義字符只是阻礙。
  • 如果startend是日期/時間,則使用適當的類型。不要使用字符串。
+0

end是日期時間20171006,類型是char(8) – RedArmy

+2

我們假設變量像'@ AkharinBazdid','@ Pid_Bazdid'和'@ C_Bazdid',因爲這些在給定的示例過程中的任何地方都沒有被定義爲變量或傳入參數?我們是否認爲這只是OP的不完整片段,還是我錯過了一些東西? – 3N1GM4

+0

Gordon Linoff,不工作,沒有更新,因爲可能我想舉個例子:@start = 20170910和其他值爲空。其他時間輸入2值爲更新,其他值爲空。 – RedArmy

1

你應該顯示你的.Net代碼。可能最好的地方是在.NET中添加檢查,如果您擔心的值爲NULL,則不要調用存儲過程。

還必須指定哪個值不能爲空,但假設你的意思是任何一個你可以這樣做:

IF (@User_Names IS NULL OR @start IS NULL OR @End IS NULL OR @Count IS NULL OR @User_Id IS NULL) 
BEGIN 
    RETURN 
END 

將在存儲過程的退出,如果任何參數爲空不更新表

鑑於你的c#代碼,你可以在值爲null或拋出異常時不要調用存儲過程。另外,您應考慮使用DateTime而不是字符串作爲日期值。

你可以在你的C#代碼如下:

if (@User_Names == null || @End == null || @Start == null) 
    return; 

或最好

if (@User_Names == null || @End == null || @Start == null) 
    throw new ArgumentNullException(); 

你甚至可以逐個檢查每個參數,並通過它的名字作爲參數傳遞給異常給一個有意義的錯誤信息。

+0

我想舉個例子:@start = 20170910和其他值爲空。其他時間輸入2值爲更新,其他值爲空。 – RedArmy

+0

send for u c#.net code – RedArmy

+0

c#answer added – Juan

相關問題