テキスト ファイルを読み取り、各行を列に分割して、それをデータベースに挿入する必要がある課題があります。
これに対する最善のアプローチは何ですか?コードを提供できれば、さらに良いものになります。
これは私がこれまでに持っているものです
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();
}