C#将CSV文件导入Mysql数据库

2025-11-19 03:23:54

1、//连接MySql数据库

string constr = "server="  ";port=" ";User Id="  ";password="  ";Database=" "";

//servery 主机名 port 端口号 User Id 用户名 password 密码 Database 数据库

//constr 数据库连接语句

MySqlConnection mycon = new MySqlConnection(constr);

2、//读取CSV文件

MySqlBulkLoader bulkLoader = new MySqlBulkLoader(mycon);

if (mycon != null && mycon.State != ConnectionState.Open)

   mycon.open();

bulkLoader.TableName = TableName; //插入的表的名字

bulkLoader.FileName = Environment.CurrentDirectory+"\\"+TableName; //文件的完整路径;先要把数据写入到csv中

//Environment.CurrentDirectory获取当前项目的路径,小编把CSV文件放那的

bulkLoader.FieldTerminator = ",";//这个地方字段间的间隔方式,为逗号

int count = bulkLoader.Load();//这个地方是执行

3、最后别忘记关闭数据库

mycon.Close();

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢