当DataColumn是AllowDBNull并且默认值为“0”时,将插入什么值? [英] What value will be inserted when the DataColumn is AllowDBNull and has Default value as “0”?

查看:167
本文介绍了当DataColumn是AllowDBNull并且默认值为“0”时,将插入什么值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将记录插入DB时,我遇到了数据集的问题。我有一个表地址,其中phno(AllowNulls = False,Defaultvalue =''0'')和mbno(AllowNulls = True,Defaultvalue =''0 '')。当我没有向两列提供任何输入时,通过数据集将记录插入数据库时​​,phno采用默认值(''0'')而mbno采取空。我想两者都必须采取没有输入时的默认值。



我不知道为什么会这样。我写了这样的代码

I have faced a problem with the dataset when inserting record into DB.I have a table address in which phno (AllowNulls=False,Defaultvalue=''0'') and mbno(AllowNulls=True,Defaultvalue=''0'') .While inserting record into database through dataset when I didn''t supply any input to the both columns phno is taking default value(''0'') and mbno is taking "Null".I suppose both has to take default value when there is no input.

I dont know why this is happening.I have written code like this

SqlConnection con = new SqlConnection(connStr);
DataSet ds = new DataSet();
DataRow dr;
SqlCommand cmd = new SqlCommand("select * from address",con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
SqlCommandBuilder cmdBuilder;
da.FillSchema (ds,SchemaType.Source );
da.Fill(ds, "address");

dr = ds.Tables["address"].NewRow();
dr["sname"] = "tomy";
dr["fname"] = "peter";

ds.Tables["address"].Rows.Add(dr);

cmdBuilder = new SqlCommandBuilder(da);
da.Update(ds, "address");

推荐答案

在mob编号中,你设置为allownulls为true。因此,如果你没有提供任何手机号码,那么它将从c#传递为null到db.so它将接受空值。如果你想设置默认值然后将其设置为allow nulls为true或者你可以在C3方面处理检查值是否为null设置为0.
Here in mob number you are set allownulls true. so if you are not giving any mobile number then it will pass as null from c# to db.so it will accept null value.if you want to set default value then set it into allow nulls is true or you can handle in C3 side by checking if values are null set to 0.


这篇关于当DataColumn是AllowDBNull并且默认值为“0”时,将插入什么值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆