这是我用于将大于 500 MB 的文本文件导入数据库的代码。
我想分批进行,这样如果在执行过程中文本文件中出现任何与格式相关的错误,至少上传一半的内容。
如果对上传如此大的文本文件有任何其他建议,请说明。
private DataTable CreateDataTableFromFileLoop()
{
string filename = "";
if (fileuploadExcel.HasFile)
{
try
{
filename = Path.GetFileName(fuTextLoop.FileName);
fuTextLoop.SaveAs(Server.MapPath("~/callText") + filename);
//StatusLabel.Text = "Upload status: File uploaded!";
}
catch (Exception ex)
{
StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
}
}
DataTable dt = new DataTable();
DataColumn dc;
DataRow dr;
dc = new DataColumn();
dc.DataType = System.Type.GetType("System.String");
dc.ColumnName = "Sr No";
dc.Unique = false;
dt.Columns.Add(dc);
dc = new DataColumn();
dc.DataType = System.Type.GetType("System.String");
dc.ColumnName = "Mobile";
dc.Unique = false;
dt.Columns.Add(dc);
dc = new DataColumn();
dc.DataType = System.Type.GetType("System.String");
dc.ColumnName = "Name";
dc.Unique = false;
dt.Columns.Add(dc);
dc = new DataColumn();
dc.DataType = System.Type.GetType("System.String");
dc.ColumnName = "Fath_Hus_Name";
dc.Unique = false;
dt.Columns.Add(dc);
dc = new DataColumn();
dc.DataType = System.Type.GetType("System.String");
dc.ColumnName = "Address";
dc.Unique = false;
dt.Columns.Add(dc);
dc = new DataColumn();
dc.DataType = System.Type.GetType("System.String");
dc.ColumnName = "City";
dc.Unique = false;
dt.Columns.Add(dc);
dc = new DataColumn();
dc.DataType = System.Type.GetType("System.String");
dc.ColumnName = "PIN Code";
dc.Unique = false;
dt.Columns.Add(dc);
dc = new DataColumn();
dc.DataType = System.Type.GetType("System.String");
dc.ColumnName = "Contact Number";
dc.Unique = false;
dt.Columns.Add(dc);
dc = new DataColumn();
dc.DataType = System.Type.GetType("System.String");
dc.ColumnName = "Activation_date";
dc.Unique = false;
dt.Columns.Add(dc);
dc = new DataColumn();
dc.DataType = System.Type.GetType("System.String");
dc.ColumnName = "Subs_type";
dc.Unique = false;
dt.Columns.Add(dc);
dc = new DataColumn();
dc.DataType = System.Type.GetType("System.String");
dc.ColumnName = "Status";
dc.Unique = false;
dt.Columns.Add(dc);
dc = new DataColumn();
dc.DataType = System.Type.GetType("System.String");
dc.ColumnName = "Subs_name";
dc.Unique = false;
dt.Columns.Add(dc);
StreamReader sr = new StreamReader(Server.MapPath("~/callText") + filename);
SqlConnection conn = new SqlConnection("Server=.;Database=temp;Trusted_Connection=True;");
if (ddlSub.SelectedValue.ToString() == "Reliance")
{
try
{
string input;
string mob_chk;
while ((input = sr.ReadLine()) != null)
{
string[] s = input.Split(new char[] { ',' });
dr = dt.NewRow();
SqlCommand comm = new SqlCommand("select Mobile from call where Mobile='" + s[1] + "'", conn);
conn.Open();
SqlDataReader sdr = comm.ExecuteReader();
if (sdr.HasRows)
{
goto end_of_loop;
}
if (!sdr.HasRows)
{
dr["Sr No"] = s[0];
dr["Mobile"] = s[1];
dr["Name"] = s[3];
dr["Fath_Hus_Name"] = s[4]+s[5]+s[6] + s[7];
dr["Address"] = s[8]+s[9];
dr["City"] = s[10];
dr["PIN Code"] = s[11];
dr["Contact Number"] = s[16];
dr["Activation_date"] = s[18];
dr["Subs_type"] = s[15];
//dr["Status"] = s[10];
dr["Subs_name"] = ddlSub.SelectedValue.ToString();
}
dt.Rows.Add(dr);
end_of_loop:
conn.Close();
}
sr.Close();
dt.Rows[0].Delete();
}
catch (Exception ex)
{
StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
}
}
if (dt.Rows.Count > 0)
{
return dt;
}
else
{
return null;
}