还没有真正解决这个问题。但是使用 OleDBConnection 可以更轻松地读取 excel 文件中的任何内容,如下所示:
try
{
using (OleDbConnection olcon = new OleDbConnection())
{
olcon.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath(fileupFile.FileName) + ";Extended Properties=Excel 12.0";
OleDbCommand olcmd = new OleDbCommand("SELECT * FROM [Sheet1$]", olcon);
olcon.Open();
OleDbDataReader olread = olcmd.ExecuteReader();
while (olread.Read())
{
line = (String)(olread.GetString(0));
Verify(line); //calls the datatosql method
}
olcon.Close();
lblFiup.Text = "Data Inserted Sucessfully";
lblFiup.ForeColor = System.Drawing.Color.Green;
}
}
然后,传入另一个方法,该方法使用 SqlConnection 写入 SQL Server。像这样:
protected void datatosql(String url, String stat)
{
try
{
using (SqlConnection sqlcon = new SqlConnection("Server=(local);Database=URLs;Trusted_Connection=True"))
{
using (SqlCommand sqlcom = sqlcon.CreateCommand())
{
sqlcom.CommandText = "INSERT INTO WEVRecordsTest (URL, Status) VALUES ('" + url + "','" + stat + "')";
sqlcon.Open();
sqlcom.ExecuteNonQuery();
sqlcon.Close();
}
}
}
catch (SqlException se)
{
lblHist.Text = se.Message;
lblHist.ForeColor = System.Drawing.Color.Red;
}
}