这是从文件中逐行获取数据并将其保存到数据库的代码>>
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);
}
希望您能应用进一步的逻辑,这将对您有所帮助。