2017-01-09 76 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

相关问题