当前位置: 编程技术>.net/c#/asp.net
C#导入文本文件的例子
来源: 互联网 发布时间:2014-08-30
本文导语: c#代码实现文本文件导入到数据库。 1,文本文件格式: "000-0001-1-0A","JEF",229.00,0.0345 "000-0002-1-TA","JEF",205.00,0.0351 "000-0003-1-NA","JEF",7516.00,0.0382 "000-0003-1-S1","JEF",1681.00,0.0675 "000-0004-1-0A","JEF",213.00,0.0563 2,实现思路: 使用StreamRead...
c#代码实现文本文件导入到数据库。
1,文本文件格式:
"000-0001-1-0A","JEF",229.00,0.0345
"000-0002-1-TA","JEF",205.00,0.0351
"000-0003-1-NA","JEF",7516.00,0.0382
"000-0003-1-S1","JEF",1681.00,0.0675
"000-0004-1-0A","JEF",213.00,0.0563
"000-0002-1-TA","JEF",205.00,0.0351
"000-0003-1-NA","JEF",7516.00,0.0382
"000-0003-1-S1","JEF",1681.00,0.0675
"000-0004-1-0A","JEF",213.00,0.0563
2,实现思路:
使用StreamReader一行一行的读取TXT文件,然后把每一行的记录写入到已经打开的DataSet的Table中去,最后,使用SqlCommandBuilder自动填充数据并把数据Update到SQL SERVER数据库中去。从而完成数据的读取。
3,代码:
代码示例:
string Path;
string ReadLine;
string[] array;
Path=@ImportPath.Text.Trim();
//Path=@"E:INOUT.TXT";
if (File.Exists(Path))
{
StreamReader reader=new StreamReader(Path,System.Text.Encoding.GetEncoding("GB2312"));
while (reader.Peek()>=0)
{
try
{
ReadLine=reader.ReadLine();
if (ReadLine!="")
{
ReadLine=ReadLine.Replace(""","");
array=ReadLine.Split(',');
if (array.Length != 9)
{
MessageBox.Show("您选择的导入数据类型有误,请重试!");
return;
}
SqlDataAdapter Adapter=new SqlDataAdapter();
DataSet ds=new DataSet();
Adapter.SelectCommand=new SqlCommand("Sp_GetData",con);
Adapter.SelectCommand.CommandType=CommandType.StoredProcedure;
Adapter.Fill(ds,"table");
SqlCommandBuilder ComBuilder=new SqlCommandBuilder(Adapter);
DataTable DItem =ds.Tables["table"];
DataRow DRow=DItem.NewRow();
DRow["Type"]=array[0];
DRow["Refno"]=array[1];
DRow["Flag"]=array[2];
DRow["PartNo"]=array[3];
if (array[4]==null || array[4]=="")
{
DRow["Quantity"]=0;
}
else
{
DRow["Quantity"]=Convert.ToDouble(array[4]);
}
if (array[5]==null || array[5]=="")
{
DRow["TranCode"]=0;
}
else
{
DRow["TranCode"]=Convert.ToInt32(array[5]);
}
DRow["Origin"]=array[6];
DRow["Location"]=array[7];
if (array[8]==null || array[8]=="")
{
DRow["StdCost"]=0;
}
else
{
DRow["StdCost"]=Convert.ToDouble(array[8]);
}
DItem.Rows.Add(DRow);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
try
{
//Adapter.InsertCommand=ComBuilder.GetInsertCommand();
//Adapter.UpdateCommand=ComBuilder.GetUpdateCommand();
//Adapter.DeleteCommand=ComBuilder.GetDeleteCommand();
Adapter.Update(ds,"table");
ds.AcceptChanges();
MessageBox.Show("数据读取完毕");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
else
{
MessageBox.Show("您输入的路径文件不存在!");
}
}
else
{
MessageBox.Show("请选择导入的数据类型!");
}
string ReadLine;
string[] array;
Path=@ImportPath.Text.Trim();
//Path=@"E:INOUT.TXT";
if (File.Exists(Path))
{
StreamReader reader=new StreamReader(Path,System.Text.Encoding.GetEncoding("GB2312"));
while (reader.Peek()>=0)
{
try
{
ReadLine=reader.ReadLine();
if (ReadLine!="")
{
ReadLine=ReadLine.Replace(""","");
array=ReadLine.Split(',');
if (array.Length != 9)
{
MessageBox.Show("您选择的导入数据类型有误,请重试!");
return;
}
SqlDataAdapter Adapter=new SqlDataAdapter();
DataSet ds=new DataSet();
Adapter.SelectCommand=new SqlCommand("Sp_GetData",con);
Adapter.SelectCommand.CommandType=CommandType.StoredProcedure;
Adapter.Fill(ds,"table");
SqlCommandBuilder ComBuilder=new SqlCommandBuilder(Adapter);
DataTable DItem =ds.Tables["table"];
DataRow DRow=DItem.NewRow();
DRow["Type"]=array[0];
DRow["Refno"]=array[1];
DRow["Flag"]=array[2];
DRow["PartNo"]=array[3];
if (array[4]==null || array[4]=="")
{
DRow["Quantity"]=0;
}
else
{
DRow["Quantity"]=Convert.ToDouble(array[4]);
}
if (array[5]==null || array[5]=="")
{
DRow["TranCode"]=0;
}
else
{
DRow["TranCode"]=Convert.ToInt32(array[5]);
}
DRow["Origin"]=array[6];
DRow["Location"]=array[7];
if (array[8]==null || array[8]=="")
{
DRow["StdCost"]=0;
}
else
{
DRow["StdCost"]=Convert.ToDouble(array[8]);
}
DItem.Rows.Add(DRow);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
try
{
//Adapter.InsertCommand=ComBuilder.GetInsertCommand();
//Adapter.UpdateCommand=ComBuilder.GetUpdateCommand();
//Adapter.DeleteCommand=ComBuilder.GetDeleteCommand();
Adapter.Update(ds,"table");
ds.AcceptChanges();
MessageBox.Show("数据读取完毕");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
else
{
MessageBox.Show("您输入的路径文件不存在!");
}
}
else
{
MessageBox.Show("请选择导入的数据类型!");
}