Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将空值插入到SQL表中_C#_Sql_Sql Server_Winforms - Fatal编程技术网

C# 将空值插入到SQL表中

C# 将空值插入到SQL表中,c#,sql,sql-server,winforms,C#,Sql,Sql Server,Winforms,我有一个windows应用程序表单,我想在sql数据库中插入文本框值,其中一些是必需的,另一些是允许插入空值的。但我不能将空值(在int的情况下)插入数据库 CREATE TABLE [dbo].[tblReg]( [Reg_ID] [int] IDENTITY(1,1) NOT NULL, [Sep_Status] [varchar](50) NOT NULL, [Pop_Grp] [varchar](50) NOT NULL, [Child_Address] [varchar](50) N

我有一个windows应用程序表单,我想在sql数据库中插入文本框值,其中一些是必需的,另一些是允许插入空值的。但我不能将空值(在int的情况下)插入数据库

 CREATE TABLE [dbo].[tblReg](
[Reg_ID] [int] IDENTITY(1,1) NOT NULL,
[Sep_Status] [varchar](50) NOT NULL,
[Pop_Grp] [varchar](50) NOT NULL,
[Child_Address] [varchar](50) NULL,
[Child_AgeYr] [int] NULL

) ON [PRIMARY]
这是我的c代码


您可以在查询中使用NULLIF

试试这个

public int SaveChilDetails(string ss, string pg,string Add,int yr)
    {
       string constring =Config.GetConnection();
       using (SqlConnection con=new SqlConnection(constring))
       {
           using (SqlCommand cmd = new SqlCommand("Insert into  tblReg(Sep_Status,Pop_Grp,Child_Address,Child_AgeYr) output INSERTED.Reg_ID values(@ss,@pg,NULLIF(@Add,''),NULLIF(@yr,0))", con))
           {
               cmd.Parameters.AddWithValue("@ss", ss);
               cmd.Parameters.AddWithValue("@pg", pg);
               cmd.Parameters.AddWithValue("@Add", Add);
               cmd.Parameters.AddWithValue("@yr", yr);
               if (con.State != ConnectionState.Open)
               con.Open();
               int createid = (int)cmd.ExecuteScalar();
               if (con.State == System.Data.ConnectionState.Open) con.Close();
               return createid;
           }
       }
    }
private void Children_Save_Click(object sender, EventArgs e)
    {
        int result=0;
        ss = SepStat.Text.ToString().Trim();
        pg = PopGr.Text.ToString().Trim();
        Add = ChildAdd.Text.ToString().Trim();
         if(int.TryParse(ChildDOBYr.Text,out result))
         {
            yr=ChildDOBYr.Text;
         }
         else
         {
           yr=result;
         }
        createdid=SaveChilDetails(ss,pg,Add,yr);

    }                
编辑

出现错误:输入字符串的格式不正确。

您需要使用

也试试这个

 public int SaveChilDetails(string ss, string pg,string Add,int yr)
        {
           string constring =Config.GetConnection();              
           using (SqlConnection con=new SqlConnection(constring))
           {
               using (SqlCommand cmd = new SqlCommand("Insert into  tblReg(Sep_Status,Pop_Grp,Child_Address,Child_AgeYr) output INSERTED.Reg_ID values(@ss,@pg,NULLIF(@Add,''),NULLIF(@yr,0))", con))
               {
                   cmd.Parameters.AddWithValue("@ss", ss);
                   cmd.Parameters.AddWithValue("@pg", pg);
                   cmd.Parameters.AddWithValue("@Add", Add);
                   cmd.Parameters.AddWithValue("@yr", yr);                      
                   if (con.State != ConnectionState.Open)
                   con.Open();
                   int createid = (int)cmd.ExecuteScalar();
                   if (con.State == System.Data.ConnectionState.Open) con.Close();
                   return createid;
               }
           }
        }
更新

试试这个

public int SaveChilDetails(string ss, string pg,string Add,int yr)
    {
       string constring =Config.GetConnection();
       using (SqlConnection con=new SqlConnection(constring))
       {
           using (SqlCommand cmd = new SqlCommand("Insert into  tblReg(Sep_Status,Pop_Grp,Child_Address,Child_AgeYr) output INSERTED.Reg_ID values(@ss,@pg,NULLIF(@Add,''),NULLIF(@yr,0))", con))
           {
               cmd.Parameters.AddWithValue("@ss", ss);
               cmd.Parameters.AddWithValue("@pg", pg);
               cmd.Parameters.AddWithValue("@Add", Add);
               cmd.Parameters.AddWithValue("@yr", yr);
               if (con.State != ConnectionState.Open)
               con.Open();
               int createid = (int)cmd.ExecuteScalar();
               if (con.State == System.Data.ConnectionState.Open) con.Close();
               return createid;
           }
       }
    }
private void Children_Save_Click(object sender, EventArgs e)
    {
        int result=0;
        ss = SepStat.Text.ToString().Trim();
        pg = PopGr.Text.ToString().Trim();
        Add = ChildAdd.Text.ToString().Trim();
         if(int.TryParse(ChildDOBYr.Text,out result))
         {
            yr=ChildDOBYr.Text;
         }
         else
         {
           yr=result;
         }
        createdid=SaveChilDetails(ss,pg,Add,yr);

    }                

您可以在查询中使用NULLIF

试试这个

public int SaveChilDetails(string ss, string pg,string Add,int yr)
    {
       string constring =Config.GetConnection();
       using (SqlConnection con=new SqlConnection(constring))
       {
           using (SqlCommand cmd = new SqlCommand("Insert into  tblReg(Sep_Status,Pop_Grp,Child_Address,Child_AgeYr) output INSERTED.Reg_ID values(@ss,@pg,NULLIF(@Add,''),NULLIF(@yr,0))", con))
           {
               cmd.Parameters.AddWithValue("@ss", ss);
               cmd.Parameters.AddWithValue("@pg", pg);
               cmd.Parameters.AddWithValue("@Add", Add);
               cmd.Parameters.AddWithValue("@yr", yr);
               if (con.State != ConnectionState.Open)
               con.Open();
               int createid = (int)cmd.ExecuteScalar();
               if (con.State == System.Data.ConnectionState.Open) con.Close();
               return createid;
           }
       }
    }
private void Children_Save_Click(object sender, EventArgs e)
    {
        int result=0;
        ss = SepStat.Text.ToString().Trim();
        pg = PopGr.Text.ToString().Trim();
        Add = ChildAdd.Text.ToString().Trim();
         if(int.TryParse(ChildDOBYr.Text,out result))
         {
            yr=ChildDOBYr.Text;
         }
         else
         {
           yr=result;
         }
        createdid=SaveChilDetails(ss,pg,Add,yr);

    }                
编辑

出现错误:输入字符串的格式不正确。

您需要使用

也试试这个

 public int SaveChilDetails(string ss, string pg,string Add,int yr)
        {
           string constring =Config.GetConnection();              
           using (SqlConnection con=new SqlConnection(constring))
           {
               using (SqlCommand cmd = new SqlCommand("Insert into  tblReg(Sep_Status,Pop_Grp,Child_Address,Child_AgeYr) output INSERTED.Reg_ID values(@ss,@pg,NULLIF(@Add,''),NULLIF(@yr,0))", con))
               {
                   cmd.Parameters.AddWithValue("@ss", ss);
                   cmd.Parameters.AddWithValue("@pg", pg);
                   cmd.Parameters.AddWithValue("@Add", Add);
                   cmd.Parameters.AddWithValue("@yr", yr);                      
                   if (con.State != ConnectionState.Open)
                   con.Open();
                   int createid = (int)cmd.ExecuteScalar();
                   if (con.State == System.Data.ConnectionState.Open) con.Close();
                   return createid;
               }
           }
        }
更新

试试这个

public int SaveChilDetails(string ss, string pg,string Add,int yr)
    {
       string constring =Config.GetConnection();
       using (SqlConnection con=new SqlConnection(constring))
       {
           using (SqlCommand cmd = new SqlCommand("Insert into  tblReg(Sep_Status,Pop_Grp,Child_Address,Child_AgeYr) output INSERTED.Reg_ID values(@ss,@pg,NULLIF(@Add,''),NULLIF(@yr,0))", con))
           {
               cmd.Parameters.AddWithValue("@ss", ss);
               cmd.Parameters.AddWithValue("@pg", pg);
               cmd.Parameters.AddWithValue("@Add", Add);
               cmd.Parameters.AddWithValue("@yr", yr);
               if (con.State != ConnectionState.Open)
               con.Open();
               int createid = (int)cmd.ExecuteScalar();
               if (con.State == System.Data.ConnectionState.Open) con.Close();
               return createid;
           }
       }
    }
private void Children_Save_Click(object sender, EventArgs e)
    {
        int result=0;
        ss = SepStat.Text.ToString().Trim();
        pg = PopGr.Text.ToString().Trim();
        Add = ChildAdd.Text.ToString().Trim();
         if(int.TryParse(ChildDOBYr.Text,out result))
         {
            yr=ChildDOBYr.Text;
         }
         else
         {
           yr=result;
         }
        createdid=SaveChilDetails(ss,pg,Add,yr);

    }                

您需要了解
null
(用于编程)与数据库值
null
不同

若要将空值插入数据库表,则需要使用
DBNull.value

试试这个:

 if (!string.IsNullOrEmpty(Add))
 {
     cmd.Parameters.AddWithValue("@Add", DBNull.Value);
 }
 else
 { 
     cmd.Parameters.AddWithValue("@Add", Add); 
 }

您需要了解
null
(用于编程)与数据库值
null
不同

若要将空值插入数据库表,则需要使用
DBNull.value

试试这个:

 if (!string.IsNullOrEmpty(Add))
 {
     cmd.Parameters.AddWithValue("@Add", DBNull.Value);
 }
 else
 { 
     cmd.Parameters.AddWithValue("@Add", Add); 
 }

使用
int?年
而非年内

parameters.Add(""@yr"", yr.HasValue ? yr.Value : (object)DBNull.Value);

参考资料:

使用
int?年
而非年内

parameters.Add(""@yr"", yr.HasValue ? yr.Value : (object)DBNull.Value);

参考资料:

运行此查询,您将了解其工作原理

create table #test(
  ID int null
)

insert into #test(ID)values(NUll)
select * from #test

drop table #test

运行这个查询,您就会知道它是如何工作的

create table #test(
  ID int null
)

insert into #test(ID)values(NUll)
select * from #test

drop table #test


当默认值为null时,为什么必须传递null?但在inti的情况下,它不是默认值。我认为问题在于convert Int partNull和DBNull。两者的值都不同。如果您试图在sql参数中设置null值,那么您应该尝试DBNull.value而不是null。当默认值为null时,为什么您必须传递null?但在inti的情况下,它不会采用默认null值。我认为问题在于convert Int partNull和DBNull.value两者都不同。如果您试图在sql参数中设置null值,那么您应该尝试DBNull.value而不是null;仍然有错误here@neel我也更新了…请看一下我的更新。我想问题出在convert Int part中,那么yr=convert.ToInt32(ChildDOBYr.Text)呢;仍然有错误here@neel我也更新了…请看我的updation@NullRefrenceException:那么yr=Convert.ToInt32(ChildDOBYr.Text)呢;仍然可以纠正错误here@neeltry yr=int.Parse(ChildDOBYr.Text)@nullreferenceexception:yr=Convert.ToInt32(ChildDOBYr.Text)又如何呢;仍然可以纠正错误here@neeltry yr=int.Parse(ChildDOBYr.Text);并使用DBNull.value从存储过程传递null值,使用DBNull.value从存储过程传递null值