我将excel导入sql server db excel表有三列:
id(仅限数字)|数据|护照
在导入之前我想检查某些事情,例如:
我可以检查护照,但即使我使用的是用于检查护照的相同代码,我也无法检查身份证。
using (DbDataReader dr = command.ExecuteReader())
{
// SQL Server Connection String
string sqlConnectionString = "Data Source=DITSEC3;Initial Catalog=test;Integrated Security=True";
con.Open();
DataTable dt7 = new DataTable();
dt7.Load(dr);
DataRow[] ExcelRows = new DataRow[dt7.Rows.Count];
DataColumn[] ExcelColumn = new DataColumn[dt7.Columns.Count];
//=================================================
for (int i1 = 0; i1 < dt7.Rows.Count; i1++)
{
if (dt7.Rows[i1]["passport"] == null)
{
dt7.Rows[i1]["passport"] = 0;
}
if (dt7.Rows[i1]["id"] == null)
{
dt7.Rows[i1]["id"] = 0;
}
string a = Convert.ToString(dt7.Rows[i1]["passport"]);
string b = dt7.Rows[i1]["id"].ToString();
if (!string.IsNullOrEmpty(b))
{
int idlen = b.Length;
for (int j = 0; j < idlen; j++)
{
if (Char.IsDigit(b[j]))
{
//action
}
if(!Char.IsDigit(b[j]))
{
flag = flag + 1;
int errline = i1 + 2;
Label12.Text = "Error at line: " + errline.ToString();
//Label12.Visible = true;
}
}
if (!String.IsNullOrEmpty(a))
{
int len = a.Length;
for (int j = 1; j < len; j++)
{
if (Char.IsLetter(a[0]) && Char.IsDigit(a[j]) && !Char.IsSymbol(a[j]))
{
//action
}
else
{
flag = flag + 1;
int errline = i1 + 2;
Label12.Text = "Error at line: " + errline.ToString();
//Label12.Visible = true;
}
}
}
}
由于某些奇怪的原因,当我使用断点时,我可以看到id的值,只要id是excel中的数字,当时流量到达具有id为25h547的值的单元格,如果b转为“”任何原因?如果您需要,我可以给你全部代码。
答案 0 :(得分:2)
似乎正在发生的事情是,当数据导入到保持数据表中并且列中的第一个记录是字母数字时,如果第一个记录是数字,它将假定列中的所有记录都是字母数字,它将假定所有列中的记录是数字,因此对于出现在列中某处的字母数字记录,它们将为空白。我通过修改连接字符串来解决问题:“Excel 8.0; IMEX = 1; HDR = NO; TypeGuessRows = 0; ImportMixedTypes = Text”
“IMEX = 1;”告诉驱动程序始终将“混合”(数字,日期,字符串等)数据列读作文本。
答案 1 :(得分:0)
specify the imex mode in connectionstring to handle mixed values
请参阅:Mixed values in excel rows
缺少价值观。 Excel驱动程序读取一定数量的行(按 默认情况下,8行)在指定的源中猜测数据类型 每一栏。当列似乎包含混合数据类型时, 特别是与文本数据混合的数字数据,驱动程序决定 支持多数数据类型,并返回单元格的空值 包含其他类型的数据。 (在平局中,数字类型 获胜。)Excel工作表中的大多数单元格格式选项似乎都没有 影响此数据类型的确定。您可以修改此行为 通过指定导入模式的Excel驱动程序。要指定导入模式, 将IMEX = 1添加到连接中的扩展属性的值 “属性”窗口中的Excel连接管理器的字符串