gpt4 book ai didi

c# - 将数据表作为类型传递给存储过程不能插入空值

转载 作者:太空宇宙 更新时间:2023-11-03 10:24:57 24 4
gpt4 key购买 nike

所以我有以下 C# 方法将 DataTable 上传到 SQL 表 (Sql Server 2008)

public bool ImportData(DataTable dtImport, string _importtype, string _custno)
{
bool success = false;
string constr = System.Configuration.ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
try
{
con.Open();
if (con.State == ConnectionState.Open)
{
SqlCommand cmdProc = new SqlCommand("JR_SP_ImportDatatable", con);
cmdProc.CommandType = CommandType.StoredProcedure;
cmdProc.Parameters.AddWithValue("@Details", dtImport);
cmdProc.Parameters.AddWithValue("@Type", _importtype);
cmdProc.Parameters.AddWithValue("@Custno", _custno);
cmdProc.ExecuteNonQuery();
success = true;
}
}
catch (Exception ex)
{
string msg = "Fetch Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{

}
return success;
}

我还创建了一个用户定义的表类型,如下...

CREATE TYPE [dbo].[ImportTable] AS TABLE(
[fname] [varchar](max) NULL,
[lname] [varchar](max) NULL,
[date1] [varchar](max) NULL,
[fname2] [varchar](max) NULL,
[lname2] [varchar](max) NULL,
[date2] [varchar](max) NULL,
[salutation] [varchar](max) NULL,
[name1] [varchar](max) NULL,
[name2] [varchar](max) NULL,
[company] [varchar](max) NULL,
[address] [varchar](max) NULL,
[address2] [varchar](max) NULL,
[city] [varchar](max) NULL,
[state] [varchar](max) NULL,
[zip] [varchar](max) NULL,
[custno] [varchar](max) NULL
)
GO

请注意,在我的 Sql 表中,我不允许在任何这些字段中使用 NULL 值,因此当它们在数据表中为空白时,默认值为 (''),因此它会插入一个空白值。

我的问题是它抛出错误“获取错误:无法将值 NULL 插入列‘fname2’,表‘dbo.UsersMailingData’;列不允许空值。INSERT 失败。声明已终止。

但是,当我调试要发送到存储过程的数据表时,它的列 fname2 具有空白值。有什么理由认为它是空的?为了简短起见,我的 fname2 字段仅在我的 sql 表中...

[fname2] [nvarchar](max) NOT NULL

有这个约束

ALTER TABLE [dbo].[UsersMailingData] ADD  CONSTRAINT [DF_UsersMailingDataFinal_fname2]  DEFAULT (' ') FOR [fname2]
GO

最后这是我用来插入记录的存储过程...

CREATE PROCEDURE [dbo].[JR_SP_ImportDatatable]

@Details ImportTable Readonly,
@Type varchar(max),
@Custno varchar(max)

AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
DECLARE @rownum int
IF EXISTS(SELECT * FROM UsersMailingData WHERE custno = @Custno)
SET @rownum = (SELECT MAX(recip_id)+1 FROM UsersMailingData WHERE custno = @Custno)
ELSE
SET @rownum = 0

IF(@Type = 'Add')
BEGIN
INSERT INTO UsersMailingData(recip_id, fname, lname, date1, fname2, lname2, date2, salutation, name1, name2, company, address, address2, city, state, zip, custno)
SELECT row_number() over (order by custno)+@rownum, fname, lname, date1, fname2, lname2, date2, salutation, name1, name2, company, address, address2, city, state, zip, custno FROM @Details
OPTION (RECOMPILE);
END
IF(@Type = 'Replace')
BEGIN
DELETE FROM UsersMailingData
WHERE Custno = @Custno
INSERT INTO UsersMailingData(recip_id, fname, lname, date1, fname2, lname2, date2, salutation, name1, name2, company, address, address2, city, state, zip, custno)
SELECT row_number() over (order by custno)+@rownum, fname, lname, date1, fname2, lname2, date2, salutation, name1, name2, company, address, address2, city, state, zip, custno FROM @Details
OPTION (RECOMPILE);
END
END


GO

如果有人对我为什么会收到此错误有任何想法,那将会很有帮助。

最佳答案

列默认值仅在您未为其提供值时应用。两者之间存在差异:

Insert TableWithThreeColumns(One,Two) values ('One','Two')
-- Third, unnamed column gets default

insert TableWithThreeColumns(One,Two,Tree) values ('One','Two',NULL)
-- Column will NOT get default: it will try to put in NULL

您可以在选择期间从您的源代码中测试 null,而不是使用默认约束:

insert into table (column1,column2)
select isnull(column1,''),isnull(column2,'')

关于c# - 将数据表作为类型传递给存储过程不能插入空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31953004/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com