Insert a null value to an int column in sql server

前端 未结 3 987
闹比i
闹比i 2021-01-25 07:10

I have a subroutine to insert a record to sql server table. The code:

    public void Insert_Met(int Counts, char Gender)
    {
        Dictionary

        
相关标签:
3条回答
  • 2021-01-25 07:31
    int count = !(count == 0)? count ? (object)DBNull.Value
    

    -If, count is not zero save the count value to database -If it is zero save

    0 讨论(0)
  • 2021-01-25 07:34

    You could use int? counts instead of int counts, and check the value within your method:

    public void InsertMet(int? counts, char gender)
    {
        Dictionary<string, object> parameters = new Dictionary<string, object>();
        parameters.Add("@Counts", counts.HasValue ? counts.Value : (object)DBNull.Value);
        parameters.Add("@Gender", gender);
        // run a stored procedure ExecuteNonQuery
    }
    

    it is a numericupdown control in windows form. I feel that it is hard to assign a text to an int variable. How to change it?

    In order to set the count value appropriately for the above, you could do:

    int value = (int)numberUpdowncontrol1.Value;
    int? counts = !string.IsNullOrEmpty(numberUpdowncontrol1.Text) ? value : (int?)null;
    
    InsertMet(counts, 'M');
    
    0 讨论(0)
  • 2021-01-25 07:42

    Following statement will always produce not null value

    int counts = Convert.ToInt32(null); // Result is 0
    
    0 讨论(0)
提交回复
热议问题