我有一個任務,我需要讀取文本文件,然後將每行分解成列,然後我需要將其插入到數據庫中。讀取文本文件,然後將行分解成列
這是最好的方法是什麼?任何幫助將不勝感激,如果你可以提供代碼會更好。
這是我迄今爲止
string filename = Server.MapPath("~/Text_File_4.txt");
StreamReader sr = new StreamReader(filename);
string styl;
string colr;
string sdim;
string size;
string qty;
string line;
string sprice;
string sretail;
while ((line = sr.ReadLine()) != null)
{
styl = line.Substring(0, 6);
colr = line.Substring(6, 2);
sdim = line.Substring(8, 1);
size = line.Substring(14, 3);
qty = line.Substring(19, 5);
sprice = line.Substring(27, 6);
sretail = line.Substring(38, 4);
con.Open();
cmd = new SqlCommand("insert into ststyl00(ststyl, stcolr, stsdim, stszcd, stprq, strprq) values(@ststyl, @stcolr, @stsdim, @stszcd, @stprq, @strprq)", con);
cmd.Parameters.Add("@ststyl", SqlDbType.VarChar, 15).Value = styl;
cmd.Parameters.Add("@stcolr", SqlDbType.VarChar, 3).Value = colr;
cmd.Parameters.Add("@stsdim", SqlDbType.VarChar, 8).Value = sdim;
cmd.Parameters.Add("@stszcd", SqlDbType.VarChar, 3).Value = size;
cmd.Parameters.Add("@stprq", SqlDbType.VarChar, 8).Value = sprice;
cmd.Parameters.Add("@strprq", SqlDbType.VarChar, 8).Value = sretail;
cmd.ExecuteNonQuery();
con.Close();
}
到目前爲止你有什麼 – 2012-07-24 03:23:35
列寬是固定的嗎? CSV? – 2012-07-24 03:23:39