asp.net - 使用asp.net将Excel数据导入sql数据库

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

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 导入数据库。我已经尝试过这样的操作,但收到此错误:

Login failed for user. The user is not associated with a trusted SQL Server connection.

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

最佳答案

Just Remove the "integrated security" part from your strConnection . Here your connection will use the windows authentication instead of the sql authentication. 

关于asp.net - 使用asp.net将Excel数据导入sql数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25030622/

相关文章:

c# - .Net 可折叠面板或可折叠链接控件

asp.net - 基准测试时 Razor 页面中的 BookSleeve Wait() 超时

asp.net - Azure 云服务上的间歇性 ASP.net IIS8.5 无法捕获 500 internal-server-error

c# - 遍历 DataTable 以查找 List 对象中的元素?

asp.net - Razor MVC3 : Partial View to Reuse

sql - 附加两个查询的结果并输出为单个表

sql-server - 如何使用 sqlcmd 从 SQL Server 将数据导出为 CSV 格式?

c# - 无法将 System.Web.UI.WebControls.Label 类型的对象转换为类型 System.IConvertible

sql - 如何将单个列值拆分为多个列值?

c# - InternalsVisibleTo - 如何防止某人创建具有相同名称的程序集并访问内部类或方法?