Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySql ConnectionString without Database name to create a database in c#

I have got a situation where i do need to create a database into MYSQL by using the connection string needed to get into mysql server.Till Now i have used connectionstring with database names .So in this situation what will be the connectionstring structure to execute my create database queries into mysql server.

I need the Connectionstring for localhost ..

Please help me ..

like image 500
user3617097 Avatar asked May 15 '14 11:05

user3617097


2 Answers

You can optionally omit the database parameter in connection string. Doing so, you get a connection to the database server but you are not connected to any specific database.

Part of Example from MySQL documentation:

MySql.Data.MySqlClient.MySqlConnection conn;
string myConnectionString;

//myConnectionString = "server=localhost;uid=root;pwd=12345;database=test;";
myConnectionString = "server=localhost;uid=root;pwd=12345;";

try
{
    conn = new MySql.Data.MySqlClient.MySqlConnection();
    conn.ConnectionString = myConnectionString;
    conn.Open();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
    MessageBox.Show(ex.Message);
}

But you have to use database name to qualify the table or other object names within your queries.

Example:

select * from so.tbl_so_q23676633;

In the above example, 'so' is the database qualifier for table 'tbl_so_q23676633'.

like image 109
Ravinder Reddy Avatar answered Oct 13 '22 00:10

Ravinder Reddy


Using OleDb you can achieve this. // OleDb

    using System.Data.OleDb;
    OleDbConnection conn = new OleDbConnection(); 
    conn.ConnectionString = "Provider=MySqlProv; Data Source=ServerName; User id=UserName; Password=Secret";
    conn.Open(); 

For more info MySQL Connection string without database Another Useful link Types of MySQL connection strings

like image 25
Elanchezhian Narayanasamy Avatar answered Oct 13 '22 00:10

Elanchezhian Narayanasamy