如何通过C#如何把EXCEL文件导入SQL数据库,详细点,谢谢
1个回答
2016-07-08
展开全部
private DataSet CreateDataSource(string filepath)
{
DataSet myds = new DataSet();
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + "; Extended Properties=Excel 8.0;";
OleDbConnection olecon = new OleDbConnection(strCon);
OleDbDataAdapter myda = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strCon);
myda.Fill(myds, "excel");
return myds;
}
/// <summary>
/// 将dataset存入数据库
/// </summary>
/// <param name="ds"></param>
/// <returns></returns>
public void InsertExcelToDb(DataSet ds)
{
string sql = "";
ArrayList opc = new ArrayList();
string empId = string.Empty;
DateTime ottime = new DateTime();
string bu = lblBU.Text.ToString();
string updateUser = lblLogonId.Text.ToString();
string parame_item = "EmployeeUnLock";
foreach (DataRow dr in ds.Tables[0].Rows)
{
{
DataSet myds = new DataSet();
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + "; Extended Properties=Excel 8.0;";
OleDbConnection olecon = new OleDbConnection(strCon);
OleDbDataAdapter myda = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strCon);
myda.Fill(myds, "excel");
return myds;
}
/// <summary>
/// 将dataset存入数据库
/// </summary>
/// <param name="ds"></param>
/// <returns></returns>
public void InsertExcelToDb(DataSet ds)
{
string sql = "";
ArrayList opc = new ArrayList();
string empId = string.Empty;
DateTime ottime = new DateTime();
string bu = lblBU.Text.ToString();
string updateUser = lblLogonId.Text.ToString();
string parame_item = "EmployeeUnLock";
foreach (DataRow dr in ds.Tables[0].Rows)
{
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询