這是我擁有的文本文件中的數據。爲什麼將文本文件保存到數據庫會導致重複?
[Start]
"Pepsi"|"30"|"64"
"Coke"|"35"|"24"
"Sandwich"|"60"|"14"
"Chicken"|"120"|"10"
"Bread"|"60"|"24"
[End]
var files = from file in Directory.EnumerateFiles(@"C:\Users\K\Desktop\New folder", "*.txt", SearchOption.AllDirectories)
from line in File.ReadLines(file)
select new
{
File = file,
Line = line
};
using (MySqlConnection con = new MySqlConnection(@"server=localhost;database=test;uid=root;pwd=pw;"))
{
con.Open();
foreach (var f in files)
{
Console.WriteLine("{0}\t{1}", f.File, f.Line);
string[] Lines = File.ReadAllLines(f.File);
bool processRecord = false;
foreach (string line in Lines)
{
if (!processRecord)
{
if (Lines.Contains("[Start]"))
{
processRecord = true;
continue;
}
if (Lines.Contains("[End]"))
{
processRecord = false;
continue;
}
}
if (processRecord)
{
string[] readLineSplit = line.Split('|');
if (readLineSplit.Length > 1)
{
MySqlCommand cmd = new MySqlCommand("INSERT INTO Products(Product_Name, Product_Price, QTY) VALUES (@Product_Name, @Product_Price, @QTY)", con);
cmd.Parameters.AddWithValue("@Product_Name", readLineSplit[0].Trim('\"'));
cmd.Parameters.AddWithValue("@Product_Price", readLineSplit[1].Trim('\"'));
cmd.Parameters.AddWithValue("@QTY", readLineSplit[2].Trim('\"'));
cmd.ExecuteNonQuery();
}
}
}
}
}
這是代碼我到目前爲止,我不知道這部分代碼是造成多條記錄,是什麼問題?我需要任何幫助,謝謝。
是的,我甚至放棄了表,然後重新運行程序。 – 2015-04-02 08:52:59
_ [我如何問一個好問題?](http://stackoverflow.com/help/how-to-ask)_, – MickyD 2015-04-09 03:16:00
我已經將您的標題重寫爲_question_而不是_observation_。順便歡迎來到SO! – MickyD 2015-04-09 03:18:04