【发布时间】:2015-07-30 17:50:52
【问题描述】:
我在记事本文件中有大量数据,例如 45,00,000 行数据,
我把那个gaint文件分成小文件,
我的数据如下:
('1','dsamp','tty','tmp'....)
and so on
现在我正在逐个读取文件并使用插入脚本和一段 C# 代码将它们写入 .mdf 文件,但是当我遇到一些错误时,我无法找到错误的位置,我想要从头开始并从第 0 行插入。
有没有最好的方法或代码或工具来做到这一点
我的代码如下所示
private void Form1_Load(object sender, EventArgs e)
{
int i = 0;
try
{
string const_state = "INSERT INTO Authors1 VALUES";
string conn = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=c:\users\srikanth\documents\visual studio 2013\Projects\WindowsFormsApplication1\WindowsFormsApplication1\SampleDB.mdf;Integrated Security=True;Connect Timeout=30";
SqlConnection cn = new SqlConnection(conn);
cn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
string line;
System.IO.StreamReader file = new System.IO.StreamReader("C:\\Users\\Public\\New1.txt");
while ((line = file.ReadLine()) != null)
{
line = line.Trim();
line = line.TrimEnd(',', ',',',', '.');
cmd.CommandText = const_state + line+";";
cmd.ExecuteNonQuery();
i++;
}
MessageBox.Show(i.ToString());
file.Close();
}
catch(Exception ex)
{
MessageBox.Show(i.ToString());
MessageBox.Show(ex.ToString());
}
}
}
}
提前致谢
【问题讨论】:
-
你能告诉我们你的代码吗?听起来您只需要重新处理错误处理以更好地了解故障(即哪个文件包含故障数据,故障是什么等)。