将数据从Excel文件导入数据库表

编程入门 行业动态 更新时间:2024-10-26 08:28:46
本文介绍了将数据从Excel文件导入数据库表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好, 我最近一直在寻找能够上传excel文件并将数据导入数据库表的代码我发现似乎没有工作。 谢谢, Leroy

Hi everyone, I have been looking around recently for some codes that would allow me to upload an excel file and import the data into a database table but those that I have found dont seem to work. Thanks, Leroy

推荐答案

尝试下面的代码按钮导入点击 Try this below code on button import click protected void btnImport_Click(object sender, EventArgs e) { if (!FileUpload1.HasFile) { try { string path = string.Concat(Server.MapPath("~/Uploaded Folder/" + FileUpload1.FileName)); FileUpload1.SaveAs(path); // Connection String to Excel Workbook string excelConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 8.0", path); OleDbConnection connection = new OleDbConnection(); connection.ConnectionString = excelConnectionString; OleDbCommand command = new OleDbCommand("select * from [Sheet1

,连接); connection.Open(); // 创建DbDataReader到数据工作表 DbDataReader dr = command.ExecuteReader( ); // SQL Server连接字符串 string sqlConnectionString = @ 数据源= MYPC;初始目录=学生;用户ID = SA;密码= Wintellect的; // 批量复制到SQL Server SqlBulkCopy bulkInsert = new SqlBulkCopy(sqlConnectionString); bulkInsert.DestinationTableName = Student_Record; bulkInsert.WriteToServer(dr); Label1.Text = Ho Gaya; } Catch(例外情况) { label.text = ex.Message.ToString(); } } ", connection); connection.Open(); // Create DbDataReader to Data Worksheet DbDataReader dr = command.ExecuteReader(); // SQL Server Connection String string sqlConnectionString = @"Data Source=MYPC;Initial Catalog=Student;User ID=sa;Password=wintellect"; // Bulk Copy to SQL Server SqlBulkCopy bulkInsert = new SqlBulkCopy(sqlConnectionString); bulkInsert.DestinationTableName = "Student_Record"; bulkInsert.WriteToServer(dr); Label1.Text = "Ho Gaya"; } Catch(Exception ex) { label.text=ex.Message.ToString(); } }

更多推荐

将数据从Excel文件导入数据库表

本文发布于:2023-10-22 23:14:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519014.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库   文件   数据   Excel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!