Running Database scripts in C#

我怕爱的太早我们不能终老 提交于 2020-01-06 01:51:06

问题


How to run database scripts(MySql db scripts) in c#.

Thanks, sirni.


回答1:


Fork a child process and execute the mysql command-line client, using the script as input.

It's tempting to try to read the script as a file and execute the SQL commands within it one by one, but there are numerous statements in an SQL script that can't be executed that way.

So you'd have to duplicate the code of the mysql client anyway in order to run an SQL script. Do yourself a favor and use the program that is already designed for that purpose.




回答2:


You need to open a connection to the database then create a command object and use the ExecuteNonQuery method (if you don't expect any result) or one of the other Execute... commands. Set the CommandText of the command object to the content of the script to execute.

One example here: http://database-programming.suite101.com/article.cfm/how_to_add_records_to_mysql_with_c




回答3:


You can connect to mysql with c# via odbc:

http://dev.mysql.com/doc/refman/5.0/fr/odbc-net-op-c-sharp-cp.html




回答4:


MySQL Connector for .NET:

http://dev.mysql.com/downloads/connector/net/



来源:https://stackoverflow.com/questions/1270476/running-database-scripts-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!