Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
hello,

I want to create a mysql database (contains 1 table) if it not exists in the server by using a csharp application, please help
Posted

The obvious way to do this is to simply make an empty DB a resource in your app and write it out to the file system. Executing scripts to create a DB and a table are, however, also trivial tasks.
 
Share this answer
 
You will need to download and install the MySQL Connector/Net ADO.NET library. Once you’ve added the MySql.Data.dll assembly as a reference to your project or placed it in your /bin folder, you are ready to start writing code to access MySQL. Here is an example of connecting to MySQL and retrieving all of the rows from the verse table into a DataSet.
// Connection string for a typical local MySQL installation
string cnnString = "Server=localhost;Port=3306;Database=versedb;Uid=root;Pwd=MySecretPassword";

// Create a connection object and data adapter
MySqlConnection con = new MySqlConnection(cnnString);
MySqlDataAdapter adapter = new MySqlDataAdapter();

// Create a SQL command object
MySqlCommand cmd = new MySqlCommand("SELECT * FROM product", con);

// Create a fill a Dataset
DataSet ds = new DataSet();
adapter.SelectCommand = cmd;
adapter.Fill(ds);

// Bind the DataSet
// ... Place your databinding code here ...
 
Share this answer
 
v2
hi
There is mySql connector. Using that you can connect to mySql

http://bitdaddys.com/MySQL-ConnectorNet.html[^]

issue your database creating query using the sql command

To check if database exists before creating use

CREATE DATABASE IF NOT EXIST your_database_name

for more reference refer

http://dev.mysql.com/doc/refman/5.0/en/create-database.html[^]
 
Share this answer
 
Comments
JF2015 18-Feb-11 0:56am    
Why answer a question that is almost a year old?
hari19113 7-Jul-12 23:31pm    
hope he has got the answer...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900