【问题标题】:Import Excel data to sql database using asp.net使用asp.net将Excel数据导入sql数据库
【发布时间】:2014-07-30 07:04:59
【问题描述】:
string filepath = FileUpload1.PostedFile.FileName;
string filename = Path.GetFileName(filepath);
string ext = Path.GetExtension(filename);
String strConnection = "Data Source=192.168.51.02;Initial Catalog=****;User   ID=*****;Password=*****;Integrated Security=True";
string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath + ";Extended Properties=\"Excel 12.0 Xml;HRD=YES;IMEX=1;\"";

OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);

OleDbCommand cmd = new OleDbCommand("Select [Emp ID],[Emp Name],[Log Date],[Logtime],[Type] from [One Month Report$]", excelConnection);
excelConnection.Open();

//cmd.ExecuteNonQuery();


//DataSet ds = new DataSet();
//SqlDataAdapter da = new SqlDataAdapter("Select [sno],[Name],[Designation] from [Sheet1$]", strConnection);

OleDbDataReader dReader;
dReader = cmd.ExecuteReader();
SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection);

sqlBulk.DestinationTableName = "K_Master_EmpAttendanceDet";
sqlBulk.WriteToServer(dReader);
excelConnection.Close();

我想使用 asp.net 将数据从 excel 导入数据库。我已经尝试过这样但我收到此错误:

用户登录失败。该用户未与受信任的用户关联 SQL Server 连接。

它是否需要任何 Dll 文件或我的代码中有任何错误。请帮帮我。

【问题讨论】:

    标签: asp.net sql-server c#-4.0


    【解决方案1】:
    Just Remove the "integrated security" part from your strConnection . Here your connection will use the windows authentication instead of the sql authentication. 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-22
      • 2020-02-27
      • 2010-10-04
      • 2019-03-12
      • 2018-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多