从文本文件插入MSSQL包含整数的空值
我无法将文本文件中的/bulk NULL值插入MSSQL。 当用数字替换空值时,它不会出现问题。 将2列设置为允许空值 PublicationCaption和PublicationNumber 下面是文本文件的示例 1#迪#达根斯工业公司#435#358#2016-10-19 2#DN#Dagens Nyheter#NULL#359#2016-10-19 我认为代码中的foreach循环存在一些问题,我需要添加一些东西使其工作 这是我正在使用的代码从文本文件插入MSSQL包含整数的空值,sql,sql-server,datatable,insert,null,Sql,Sql Server,Datatable,Insert,Null,我无法将文本文件中的/bulk NULL值插入MSSQL。 当用数字替换空值时,它不会出现问题。 将2列设置为允许空值 PublicationCaption和PublicationNumber 下面是文本文件的示例 1#迪#达根斯工业公司#435#358#2016-10-19 2#DN#Dagens Nyheter#NULL#359#2016-10-19 我认为代码中的foreach循环存在一些问题,我需要添加一些东西使其工作 这是我正在使用的代码 public static DataTable
public static DataTable Publication()
{
DataTable dtPublication = new DataTable();
dtPublication.Columns.AddRange(new DataColumn[6] { new DataColumn("ID", System.Type.GetType("System.Int32")),
new DataColumn("PublicationCode", System.Type.GetType("System.String")),
new DataColumn("PublicationCaption",System.Type.GetType("System.String")),
new DataColumn("PublicationNumber", System.Type.GetType("System.Int32")),
new DataColumn("ProductNumber", System.Type.GetType("System.Int32")),
new DataColumn("CreatedDate", System.Type.GetType("System.DateTime")),
});
for (int i = 0; i < dtPublication.Columns.Count; i++)
{
dtPublication.Columns[i].AllowDBNull = true;
}
string txtData = File.ReadAllText(@"C:\Publication2.txt", System.Text.Encoding.Default);
foreach (string row in txtData.Split('\n'))
{
if (!string.IsNullOrEmpty(row))
{
dtPublication.Rows.Add();
int i = 0;
foreach (string cell in row.Split('#'))
{
dtPublication.Rows[dtPublication.Rows.Count - 1][i] = cell;
i++;
}
}
}
return dtPublication;
}
公共静态数据表发布()
{
DataTable dtPublication=新DataTable();
dtPublication.Columns.AddRange(新的DataColumn[6]{new DataColumn(“ID”,System.Type.GetType(“System.Int32”)),
新数据列(“PublicationCode”,System.Type.GetType(“System.String”),
新数据列(“PublicationCaption”,System.Type.GetType(“System.String”),
新数据列(“PublicationNumber”,System.Type.GetType(“System.Int32”),
新数据列(“ProductNumber”,System.Type.GetType(“System.Int32”),
新数据列(“CreatedDate”,System.Type.GetType(“System.DateTime”),
});
for(int i=0;i
调试时收到Im(输入字符串的格式不正确。无法存储在PublicationNumber列中。应键入Int32。)
请给我一些建议,帮我解决这个问题。
谢谢您的时间。数据库不知道您尝试插入的“NULL”字符串实际上意味着NULL值。要解决此问题,请将“NULL”字符串更改为DBNull。值:
if (cell == "NULL")
dtPublication.Rows[dtPublication.Rows.Count - 1][i] = DBNull.Value;
else
dtPublication.Rows[dtPublication.Rows.Count - 1][i] = cell;
没有将字符串
“NULL”
转换为数据表中可为NULL的字段的功能。您必须自己实施它:
object value = DBNull.Value;
if(!"NULL".Equals(cell, StringComparison.InvariantCultureIgnoreCase))
value = cell;
dtPublication.Rows[dtPublication.Rows.Count - 1][i] = value;
如果在字符串中,而不是在单词NULL中,会有空的空间呢?如1#DI#Dagens Industri#435#358#2016-10-19 2#DN#Dagens Nyheter#2016-10-19#如果是int,则使用int.Parse(cell)
。通常,使用调试器。当单元格失败时,它的值是多少?@Whencesoever它给我这样一个错误(输入字符串的格式不正确。无法存储在PublicationNumber列中。应键入Int32)@TimSchmelter失败时单元格的值是“NULL”@HenriThiesen:那么你有答案了,字符串怎么可能是“NULL”
是否转换为数字?这不起作用foreach循环在Im获取时出错(输入字符串的格式不正确。无法存储在ID列中。应键入Int32)。此解决方案同样有效。最后添加i++1+如何实现?当前上下文中不存在名称“StringComparison”。@HenriThiesen:看我的答案;)我现在明白了,StringComparison从一开始就拼写错误;)无论如何,我认为这是解决方案,但我的foreachloop现在跳过ID,在实现它时从第二列开始;最后谢谢@TimSchmelter@HenriThiesen:请随意投票或接受答案:)