我制作了一个Windows表单应用程序,可以将数据导入我的sql数据库。
这是数据在数据库中的外观
问题如果我现在想要导入更多数据,并且说我正在导入的新数据中存在一个已存在于我的sql数据表中的条目,即带有与我的sql表中已存在的完全相同的值。然后我想不导入这个特定的条目并继续下一个。
我正在使用的代码非常慢,我想找到一种更快/更好的方法。请参阅#region DAX获取代码和try catch。
以下是我点击导入时执行的代码:) 谢谢!
private void dataImportButton_Click(object sender, EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;
int candle = 0;
string candleStick = "";
foreach (var item in checkedListBox1.CheckedItems)
{
candleStick += item.ToString();
}
Regex regex = new Regex(@"\d*");
Match match = regex.Match(candleStick);
if (match.Success)
{
candle = Convert.ToInt32(match.Value);
}
string nameOfTable = string.Empty;
foreach (var item in checkedListBox2.CheckedItems)
{
nameOfTable += item.ToString();
}
string filePath = textBox1.Text;
var listaNerladdat = System.IO.File.ReadLines(filePath);
if (nameOfTable == "DAX")
#region DAX
{
foreach (var item in listaNerladdat)
{
var splitLista = item.Split(';').ToList();
if (splitLista.Count < 5)
continue;
var tmpSplitInfo = new DaxSuperTabell();
DateTime tmpDate;
if (DateTime.TryParse(splitLista[0], out tmpDate))
tmpSplitInfo.TimeStampMVCR = tmpDate;
double tmpX;
if (Double.TryParse(splitLista[1].Replace('.', ','), out tmpX))
tmpSplitInfo.HighPriceMVCR = tmpX;
if (Double.TryParse(splitLista[2].Replace('.', ','), out tmpX))
tmpSplitInfo.LowPriceMVCR = tmpX;
if (Double.TryParse(splitLista[3].Replace('.', ','), out tmpX))
tmpSplitInfo.OpenPriceMVCR = tmpX;
if (Double.TryParse(splitLista[4].Replace('.', ','), out tmpX))
tmpSplitInfo.ClosePriceMVCR = tmpX;
tmpSplitInfo.CandleStick = candle;
try{
_context.DaxSuperTabell.AddRange(tmpSplitInfo);
_context.SaveChanges();
}
catch{MessageBox.Show("This entry is a double")}
}
}
#endregion
}