如何通过c#将文件中的数据存储到sqlserver数据库的表中,文件为test.txt,文件中的内

2024-11-01 15:17:27
推荐回答(1个)
回答1:

SqlConnection conn = new SqlConnection("your connection string");
SqlCommand sc = new SqlCommand("insert into table(C1,C2,c3) values(@c1,@c2,@c3)",conn);
conn.Open();
StreamReader sr = File.OpenText("test.txt");
string str;
while((str=sr.ReadLine())!=null)
{
string []aStr = str.Split(',');
sc.Parameters["@c1"].Value = aStr[0];
sc.Parameters["@c2"].Value = aStr[1];
sc.Parameters["@c3"].Value = aStr[2];
sc.ExecuteNonQuery();
}
sr.Close();
conn.Close();