我从文本文件中向access数据库中导入数据,字段较多。我用的方法是,按行读入文本文件,将各个数据赋值给一个数组,然后用insert into的方法写入数据库,然后读下一行。
现在的问题是,我并没有加什么判断,可是当读到某一行时,如果某个字段的数值为空,那么虽然sql还是执行了,可实际上这一行数据却并没有写到数据库里去。
我希望的是,无论某个字段的数值空否,都还是把这一行写到数据库里去,因为还有很多其他字段是有数值的。
现在的问题是,我并没有加什么判断,可是当读到某一行时,如果某个字段的数值为空,那么虽然sql还是执行了,可实际上这一行数据却并没有写到数据库里去。
我希望的是,无论某个字段的数值空否,都还是把这一行写到数据库里去,因为还有很多其他字段是有数值的。
System.Data.OleDb.OleDbConnection conn=new OleDbConnection(conn_str);
System.Data.OleDb.OleDbCommand comm=new OleDbCommand();
conn.open();
StreamReader reader=readfile.OpenText();
while(reader.Peek()>=0)
{
try
{
str1=reader.ReadLine();
record_str=null; record_str=str1.Split(',');
str="insert into data_tab values ('" + record_str[0] + "','" + record_str[1] + "','" + record_str[2] + "','" + record_str[3] + "','" + record_str[4] + "')";
comm=new OleDbCommand(str,conn);
int k=comm.ExecuteNonQuery();
if (k!=1)
{
MessageBox.Show("fail");
reader.Close();
conn.Close();
return;
}
}
catch(System.Exception exc)
{
}
}
大家再看看吧,我也想给分来着。