如何使用linq将excelfile upload到数据库?

  • 我的团队在asp.net项目中工作
  • 这里我们必须把Excel内容上传到数据库
  • 我们正在使用linq
  • 请帮助做同样的事情

2个简单的方法不使用LINQ:

using System.IO; using System.Data; using System.Data.OleDb; 

public DataRow [] GetUsers(string path,string id){DataTable dt = new DataTable(); (File.Exists(path)){using(OleDbConnection con = new OleDbConnection(String.Format(“Provider = Microsoft.Jet.OLEDB.4.0; Data Source = {0}; Extended Properties = Excel 8.0”,path)) ){OleDbDataAdapter da = new OleDbDataAdapter(string.Format(“select * from users”,id),con); da.Fill(DT); }} string expression = String.Format(“{0} ='{1}'和{2} <>''”,id,“first_name”,“last_name”); string sort =“last_name ASC”; 返回dt.Select(expression式,sorting); }

public DataTable GetUsers(string path,string id){DataTable dt = new DataTable(); (File.Exists(path)){using(OleDbConnection con = new OleDbConnection(String.Format(“Provider = Microsoft.Jet.OLEDB.4.0; Data Source = {0}; Extended Properties = Excel 8.0”,path)) ){string expression = String.Format(“{0} ='{1}'和{2} <>''”,id,“first_name”,“last_name”); OleDbDataAdapter da = new OleDbDataAdapter(expression,con); da.Fill(DT); }} return dt; }