C# 在c中为空字段存储0(零)值时无法更新#
我有一个非强制性的电话文本框字段,数据类型为int。如果我将电话字段留空并提交表单,则电话列在数据库中存储为0(零)。我面临更新问题: 当手机为“0”时进行更新时,相应的行不会更新。 我需要将phone的默认值设置为null吗?如果是,请解释 这是我的更新查询C# 在c中为空字段存储0(零)值时无法更新#,c#,sql-server,database,sql-update,C#,Sql Server,Database,Sql Update,我有一个非强制性的电话文本框字段,数据类型为int。如果我将电话字段留空并提交表单,则电话列在数据库中存储为0(零)。我面临更新问题: 当手机为“0”时进行更新时,相应的行不会更新。 我需要将phone的默认值设置为null吗?如果是,请解释 这是我的更新查询 cmd.CommandText = "UPDATE client_final SET companyname = @companyname, url = @url, industry = @industry, contactperson1
cmd.CommandText = "UPDATE client_final SET companyname = @companyname, url = @url, industry = @industry, contactperson1 = @contactperson1, contactperson2 = @contactperson2, designation = @designation, fax = @fax, phone = @phone, mobile = @mobile, emailid1 = @emailid1, emailid2 = @emailid2, baddress = @baddress, bcity = @bcity, bstate = @bstate, bzipcode = @bzipcode, bcountry = @bcountry, regaddress = @regaddress, regcity = @regcity, regstate = @regstate, regzipcode = @regzipcode, regcountry = @regcountry WHERE client_id = @client_id";
cmd.Parameters.AddWithValue("@client_id", txtClientId.Text);
cmd.Parameters.AddWithValue("@companyname", txtcompanyname.Text);
cmd.Parameters.AddWithValue("@url", txturl.Text);
cmd.Parameters.AddWithValue("@industry", drpindustry.Text);
cmd.Parameters.AddWithValue("@contactperson1", txtcontactperson1.Text);
cmd.Parameters.AddWithValue("@contactperson2", txtcontactperson2.Text);
cmd.Parameters.AddWithValue("@designation", txtdesignation.Text);
cmd.Parameters.AddWithValue("@fax", txtfaxnumber.Text);
cmd.Parameters.AddWithValue("@phone", txtphone.Text);
cmd.Parameters.AddWithValue("@mobile", txtmobile.Text);
cmd.Parameters.AddWithValue("@emailid1", txtemailid1.Text);
cmd.Parameters.AddWithValue("@emailid2", txtemailid2.Text);
cmd.Parameters.AddWithValue("@baddress", txtbaddress.InnerText);
cmd.Parameters.AddWithValue("@bcity", txtbcity.Text);
cmd.Parameters.AddWithValue("@bstate", txtbstate.Text);
cmd.Parameters.AddWithValue("@bzipcode", txtbzipcode.Text);
cmd.Parameters.AddWithValue("@bcountry", bddlCountries.Text);
cmd.Parameters.AddWithValue("@regaddress", txtraddress.InnerText);
cmd.Parameters.AddWithValue("@regcity", txtrcity.Text);
cmd.Parameters.AddWithValue("@regstate", txtrstate.Text);
cmd.Parameters.AddWithValue("@regzipcode", txtrzipcode.Text);
cmd.Parameters.AddWithValue("@regcountry", rddlCountries.Text);
因为电话列的数据类型是int,int的默认值是0。请在数据库中将此列指定为可为Null,然后一切都应正常工作。因为电话列的数据类型为int,int的默认值为0。请在数据库中指定该列为可空的,然后所有的内容都应该工作。在任何表列中, 如果您不想标记为强制,则必须设置允许NULL,因为如果它考虑默认值<强>“0”< /强>,如在您的场景中,而不是<强> null < /强>,则可能会在稍后阶段产生问题。最好的方法是为您的场景设置允许Null
例如,您有另一列表示TypeId,它的类型是INT,所以现在如果用户没有在该列中输入,最好将设置为NULL,而不是0。因为某个时间用户使用的EnUM对象可能有0个值,与任何类型相关。< P>在任何表列中,如果您不想标记为强制,则必须设置允许NULL,因为如果它考虑默认值<强>“0”类似于您的场景,而不是NULL,这样可能会在后期产生问题。最好的方法是为您的场景设置允许Null
DECLARE @sampletable TABLE (ID INT IDENTITY(1,1),phonenumber INT)
INSERT INTO @sampletable VALUES('') -- here is your insertion
SELECT * FROM @sampletable -- as you said column is updated with 0 if sent blank
UPDATE @sampletable SET phonenumber = '' WHERE ID = 1 --if we update, this works fine
SELECT * FROM @sampletable -- again it is updated with 0 because we passed blank
--Where is the problem??
例如,您有另一列表示TypeId,它的类型是INT,所以现在如果用户没有在该列中输入,最好将设置为NULL,而不是0。因为用户有时会使用枚举对象,该对象的值可能为0,与任何类型关联。我认为它将在您的场景中工作
DECLARE @sampletable TABLE (ID INT IDENTITY(1,1),phonenumber INT)
INSERT INTO @sampletable VALUES('') -- here is your insertion
SELECT * FROM @sampletable -- as you said column is updated with 0 if sent blank
UPDATE @sampletable SET phonenumber = '' WHERE ID = 1 --if we update, this works fine
SELECT * FROM @sampletable -- again it is updated with 0 because we passed blank
--Where is the problem??
替换更新查询,如下所示
UPDATE client_final
SET companyname = @companyname,
url = @url,
industry = @industry,
contactperson1 = @contactperson1,
contactperson2 = @contactperson2,
designation = @designation,
fax = @fax,
phone = NullIf(@phone, ’0’),
mobile = @mobile,
emailid1 = @emailid1,
emailid2 = @emailid2,
baddress = @baddress,
bcity = @bcity,
bstate = @bstate,
bzipcode = @bzipcode,
bcountry = @bcountry,
regaddress = @regaddress,
regcity = @regcity,
regstate = @regstate,
regzipcode = @regzipcode,
regcountry = @regcountry
WHERE
client_id = @client_id;
使用函数更新空值,而不是0我认为它在您的场景中会起作用 替换更新查询,如下所示
UPDATE client_final
SET companyname = @companyname,
url = @url,
industry = @industry,
contactperson1 = @contactperson1,
contactperson2 = @contactperson2,
designation = @designation,
fax = @fax,
phone = NullIf(@phone, ’0’),
mobile = @mobile,
emailid1 = @emailid1,
emailid2 = @emailid2,
baddress = @baddress,
bcity = @bcity,
bstate = @bstate,
bzipcode = @bzipcode,
bcountry = @bcountry,
regaddress = @regaddress,
regcity = @regcity,
regstate = @regstate,
regzipcode = @regzipcode,
regcountry = @regcountry
WHERE
client_id = @client_id;
使用函数更新空值而不是0更新时会出现什么错误?@Dheeraj Sharma error不会显示,但当单击按钮时,所有字段都会变为空。可能是代码中有一些错误而不是查询,因为任何字段中的0都不会停止查询的执行,请详细说明你的问题。我已经添加了我的更新查询,如果查询有问题,那么我不应该在任何条件下工作。你的代码很好,问题在其他地方,您可以使用try-catch块并使用断点来获取错误。更新时会出现什么错误?@Dheeraj Sharma error不会显示,但单击按钮时,所有字段都会变为空白。您的代码中可能存在一些错误,而不是查询,因为任何字段中的0都不会停止查询的执行,请详细说明您的问题。我已经添加了我的更新查询,如果它是查询问题,那么我不应该在任何条件下工作。您的代码很好,问题在其他地方,您可以使用try-catch块并使用断点来捕获错误。