[英]Bulk Data extraction to DB File in C#
I have Huge data say 45,00,000 rows of data in notepad file , 我有大量数据说记事本文件中有45,000万行数据,
I split that gaint file into small files, 我把那个获取的文件分割成小文件,
I have the data as follows: 我的数据如下:
('1','dsamp','tty','tmp'....)
and so on
Now i am reading the files one by one and using the insert script and a piece of C# code I am writing them to a .mdf file , but when i get some error I am unable to find where the error is and i want to start from beginning and insert from row 0. 现在我正在逐个读取文件,并使用插入脚本和一段C#代码将它们写入.mdf文件,但是当我遇到一些错误时,我无法找到错误所在,我想开始从头开始并从第0行插入。
Is there any best way or code or tool to do this My code looks like this 有没有最好的方法或代码或工具来执行此操作我的代码如下所示
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());
}
}
}
} }
Thanks in Advance 提前致谢
What I would do is having a try/catch
block for your ExecuteNonQuery()
call. 我要做的是为您的
ExecuteNonQuery()
调用添加一个try/catch
块。 Something like this: 像这样:
while ((line = file.ReadLine()) != null)
{
line = line.Trim();
line = line.TrimEnd(',', ',',',', '.');
cmd.CommandText = const_state + line+";";
try
{
cmd.ExecuteNonQuery();
}
catch
{
// dump cmd.CommandText somewhere as well as
// the actual exception details
//
// that'll give you two things: 1) the specific
// issue, and 2) the actual INSERT statement that
// failed
}
i++;
}
See my comments in the catch { }
block on how I would handle an INSERT error 在
catch { }
块中查看有关如何处理INSERT错误的评论
By having the try/catch
around the ExecuteNonQuery()
call, you'll have the granular issue on which INSERT
statement failed, as well as the particular exception with the error. 通过具有
try/catch
周围ExecuteNonQuery()
调用,你必须在其上的颗粒问题INSERT
语句失败,以及与错误的特定异常。 The other benefit to this is it would allow you to continue execution instead of bubbling that exception up to the outer try/catch
logic. 这样做的另一个好处是,它将允许您继续执行而不是将异常冒泡到外部
try/catch
逻辑。 Unless, of course, you want to stop execution, in which case you can just rethrow the exception from the inner catch { }
block. 当然,除非您要停止执行,否则可以从内部
catch { }
块中抛出异常。 It all depends on how you want a failure handled. 这完全取决于您要如何处理故障。
Note: for your outer try/catch
block, you should include a finally { }
where you call SqlConnection.Dispose()
to release the connection and dispose of the object ( cn.Dispose()
). 注意:对于外部
try/catch
块,应包括一个finally { }
,在其中调用SqlConnection.Dispose()
以释放连接并处置对象( cn.Dispose()
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.