我通过c#读取文件,文件格式是这样的。
输入文件
00001740, 0.125, 0, able
00001740, 0.125, 0, unable
00002098, 0, 0.75, country
00002312, 0, 0, love
我想要这个文件在 MYSQL 数据库中。我编写的代码将每一行存储在数据库的每一行中。但是下面的代码只写了文本文件的第一行
private void button1_Click(object sender, EventArgs e)
{
string MyConString = "server=localhost;" +
"database=zahid;" + "password=zia;" +
"User Id=root;";
MySqlConnection connection = new MySqlConnection(MyConString);
MySqlCommand command = connection.CreateCommand();
MySqlDataReader Reader;
connection.Open();
StreamReader reader = new StreamReader("D:\\out.txt");
string line;
while ((line = reader.ReadLine()) != null)
{
string[] parts = line.Split(',');
command.CommandText = "insert into score(POS_ID,Pos,Neg,Word) values('" + parts[1] + "','" + parts[2] + "','" + parts[3] + "','" + parts[4] + "')";
Reader = command.ExecuteReader();
}
}
此代码仅返回第一行,但我希望所有文本文件行都存储在数据库中。提前致谢