這是從文件中取數據線的線,並將其保存到數據庫>>
string line;
string[] subLine;
DialogResult result = openFileDialog1.ShowDialog();
try
{
if (result == DialogResult.OK)
{
string file = openFileDialog1.FileName;
StreamReader inputData = new StreamReader(file);
const int maxRecords = 500;
int actualRecords = 0;
while ((line = inputData.ReadLine()) != null)
{
subLine = line.Split(',');
try
{
string unknown = "unknown Fileld";
con.Open();
// int j=subLine.Length;
//for(int i=0;i<subLine.Length;i++)
if (actualRecords < maxRecords)
{
int i = 0;
//int j=subLine.Length;
//int index=subLine.Length-j;
// SQL= "insert into tradeFile values('" + ID + "','" + subLine[i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + unknown + "')";
cmd = new SqlCommand("insert into tradeFile values('" + subLine[i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + unknown + "')", con);
// cmd = new OleDbCommand("insert into tradeFile values('" + subLine[i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + unknown + "')", con);
cmd.ExecuteNonQuery();
actualRecords++;
// j--;
}
else
{
int i = 0;
//int j=subLine.Length;
//int index=subLine.Length-j;
cmd = new SqlCommand("insert into tradeFile values('" + subLine[i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + unknown + "')", con);
//cmd = new OleDbCommand("insert into tradeFile values('" + subLine[i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + unknown + "')", con);
cmd.ExecuteNonQuery();
actualRecords = 0;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
//counter++;
}
inputData.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
希望您能申請進一步的邏輯,它會證明有助於你的代碼。
嘗試與您的節點js c#web應用程序 – 1Mayur 2013-03-21 06:01:20
'FileSystemWatcher'可能會訣竅。 – jordanhill123 2013-03-21 06:14:36