我想通过c#中的windows表单应用程序上传excel文件,并希望将数据导入数据库(Mysql服务器).我怎样才能做到这一点???我创建了一个表单,要求我将excel文件上传到mysql数据库.它是一个批量插入数据到数据库表.
我的Excel文件包含userid,password,first_name,last_name,user_group和MySql Database表(aster_users)等列,包含许多列,如userid,password,first_name,last_name,user_group,queue,active,created_date,created_by,role ..
我需要将该excel文件上传到我的数据库,其他列将变为空或null,这不是问题.
我的表格设计是
这是我的c#代码:
using MySql.Data.MySqlClient; using System; using System.Data.SqlClient; using System.Windows.Forms; namespace UploadFileToDatabase { public partial class Form1 : Form { public Form1() { InitializeComponent(); } String MyConString = "SERVER=******;" + "DATABASE=dbs;" + "UID=root;" + "PASSWORD=pwsd;" + "Convert Zero Datetime = True"; private void BtnSelectFile_Click(object sender, EventArgs e) { OpenFileDialog dlg = new OpenFileDialog(); dlg.Filter = "Text files | *.csv"; if (dlg.ShowDialog() == DialogResult.OK) { string fileName; fileName = dlg.FileName; txtfilepath.Text = fileName; } } private void btnUpload_Click(object sender, EventArgs e) { string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + txtfileparth.Text + ";Extended Properties=\"Excel 12.0;HDR=YES;\""; using (OleDbConnection connection = new OleDbConnection(connectionString)) { OleDbCommand command = new OleDbCommand ("Select * FROM [Sheet1$]", connection); connection.Open(); using (DbDataReader dr = command.ExecuteReader()) { string sqlConnectionString = MyConString; using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString)) { bulkCopy.ColumnMappings.Add("[userid]", "userid"); bulkCopy.ColumnMappings.Add("password", "password"); bulkCopy.ColumnMappings.Add("first_name", "first_name"); bulkCopy.ColumnMappings.Add("last_name", "last_name"); bulkCopy.ColumnMappings.Add("user_group", "user_group"); bulkCopy.DestinationTableName = "aster_users"; bulkCopy.WriteToServer(dr); MessageBox.Show("Upload Successfull!"); } } } }
这是我试过的方式.我得到了这样的错误信息
附加信息:外部表格不是预期的格式.
在这行 连接.Open(); .如何才能做到这一点?
using System; using System.Data; using System.Data.OleDb; using System.Data.SqlClient; using System.Windows.Forms; namespace IMPORT { public partial class Form1 : Form { public Form1() { InitializeComponent(); } String MyConString = "SERVER=******;" + "DATABASE=db;" + "UID=root;" + "PASSWORD=pws;"; private void btnSelectFile_Click(object sender, EventArgs e) { OpenFileDialog openfiledialog1 = new OpenFileDialog(); openfiledialog1.ShowDialog(); openfiledialog1.Filter = "allfiles|*.xls"; txtfilepath.Text = openfiledialog1.FileName; } private void btnUpload_Click(object sender, EventArgs e) { string path = txtfilepath.Text; string ConnString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties = Excel 8.0"; DataTable Data = new DataTable(); using (OleDbConnection conn =new OleDbConnection(ConnString)) { conn.Open(); OleDbCommand cmd = new OleDbCommand(@"SELECT * FROM [dataGridView1_Data$]", conn); OleDbDataAdapter adapter = new OleDbDataAdapter(cmd); adapter.Fill(Data); conn.Close(); } string ConnStr = MyConString; using (SqlBulkCopy bulkCopy = new SqlBulkCopy(ConnStr)) { bulkCopy.DestinationTableName = "TABLE NAME"; bulkCopy.ColumnMappings.Add("userid", "userid"); bulkCopy.ColumnMappings.Add("password", "password"); bulkCopy.ColumnMappings.Add("first_name", "first_name"); bulkCopy.ColumnMappings.Add("last_name", "last_name"); bulkCopy.ColumnMappings.Add("user_group", "user_group"); bulkCopy.WriteToServer(Data); MessageBox.Show("UPLOAD SUCCESSFULLY"); } } }
一个例子是 http://technico.qnownow.com/bulk-copy-data-from-excel-to-destination-db-using-sql-bulk-copy/. 和 错误:附加信息:外部表格不是预期的格式