Insert a null value to an int column in sql server

前端 未结 3 1062
闹比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: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 parameters = new Dictionary();
        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');
    

提交回复
热议问题