如何使用c#从excel表填充值到mysql数据库 [英] how to fill value from excel sheet to mysql database using c#
问题描述
HI朋友。
我有一张excel表,里面有十列和几千行,我需要
导入行在数据库(mysql数据库)中,excel列值需要存储在mysql数据库的不同表中。为此我们正在做的是: -
1-从Excel中读取行使用: -
< pre lang =c#> System.Data.OleDb.OleDbConnection sSourceConnection = new System.Data.OleDb.OleDbConnection(excelcon);
2-然后使用以下方法从mysqldatabase插入,更新或选择值: -
MYSQL_cmd = new MySqlCommand(query,MYSQL_connection);
this .OpenConnection(MYSQL_connection);
MYSQL_cmd.ExecuteNonQuery();
这个 .CloseConnection(MYSQL_connection);
但是问题是触发我需要打开和关闭连接的每个查询,是否正确的方法为千行打开和关闭连接不是一个好的做法我想,请建议..
< br $>
谢谢和问候....
按照以下方式执行
创建并打开MYSQL_connection
每行 excel
{
MYSQL_cmd = new MySqlCommand(query,MYSQL_connection);
MYSQL_cmd.ExecuteNonQuery();
}
最后关闭MYSQL_connection
HI Friends.
I have a excel sheet which have about Ten columns and thousands of rows,i need to
import rows in a database(mysql database),excel column values need to store in different table in mysql database. For that what am doing is : -
1- Reading rows from excel using :-
System.Data.OleDb.OleDbConnection sSourceConnection = new System.Data.OleDb.OleDbConnection(excelcon);
2- then am inserting,updating or selecting values form mysqldatabase by using :-
MYSQL_cmd = new MySqlCommand(query, MYSQL_connection);
this.OpenConnection(MYSQL_connection);
MYSQL_cmd.ExecuteNonQuery();
this.CloseConnection(MYSQL_connection);
but the problem is to fire each query i need to open and close connection ,is it proper way to do this for thousand of rows open and close connection is not a good practice i think ,Please suggest..
Thanks and Regards....
do as below
create and open MYSQL_connection for each row in excel { MYSQL_cmd = new MySqlCommand(query, MYSQL_connection); MYSQL_cmd.ExecuteNonQuery(); } finally close the MYSQL_connection
这篇关于如何使用c#从excel表填充值到mysql数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!