gpt4 book ai didi

c# - 如何使用 C# Windows 窗体应用程序将 excel 文件上传到 sql 数据库表

转载 作者:可可西里 更新时间:2023-11-01 06:41:49 25 4
gpt4 key购买 nike

我想通过 C# 中的 Windows 窗体应用程序上传一个 excel 文件,并将数据导入数据库(Mysql 服务器)。我怎样才能做到这一点???我创建了一个表格,要求我将 excel 文件上传到 mysql 数据库中。它是向数据库表批量插入数据。

我的 Excel 文件包含 userid、password、first_name、last_name、user_group 等列 ANDMySql 数据库表(aster_users) 包含许多列,如 userid,password,first_name,last_name,user_group,queue,active,created_date,created_by,role ..

我需要将该 excel 文件上传到我的数据库,其他列将变为空或 null,这不是问题。

我的表单设计是 enter image description here

这是我的 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!");
}
}

}
}

这是我的尝试方式。我收到了这样的错误消息

Additional information: External table is not in the expected format.

在这一行 connection.Open(); 中。如何做到这一点?

最佳答案

enter image description here

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/ . ERROR: Additional information: External table is not in the expected format

关于c# - 如何使用 C# Windows 窗体应用程序将 excel 文件上传到 sql 数据库表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34238675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com