Re: Embed MySQL into c# .Net application
Posted by: asif desai
Date: June 08, 2009 05:07AM

Hi all this is the code for connection through C# to my mySql use this and connect u r application



/********************** button click*********************/

if (conn != null)
conn.Close();

//string connStr = String.Format("server={0};user id={1}; password={2}; database=mysql; pooling=false",
// server.Text, userid.Text, password.Text );
string connStr = "server=localhost; user id=root; password=root; database=mysql;";
//String.Format("server={0};user id={1}; password={2}; database=mysql; pooling=false",

try
{
conn = new MySqlConnection( connStr );
conn.Open();

GetDatabases();
MessageBox.Show("ConnectionString" + conn.ConnectionString);
MessageBox.Show("ConnectionTimeout" + conn.ConnectionTimeout);
MessageBox.Show("Container" + conn.Container);
MessageBox.Show("Database" + conn.Database);
MessageBox.Show("DataSource" + conn.DataSource);
MessageBox.Show("ServerThread" + conn.ServerThread);
MessageBox.Show("ServerVersion" + conn.ServerVersion);
MessageBox.Show("Site" + conn.Site);
MessageBox.Show("State" + conn.State);
MessageBox.Show("UseCompression" + conn.UseCompression);


MySqlCommand cmd = new MySqlCommand();
cmd.Connection = conn;


}
catch (MySqlException ex)
{
MessageBox.Show( "Error connecting to the server: " + ex.Message );
}
/*************************************************************/


this is function
/*********************** function to browse database and schema***************/

private void GetDatabases()
{
MySqlDataReader reader = null;

MySqlCommand cmd = new MySqlCommand("SHOW DATABASES", conn);
try
{
reader = cmd.ExecuteReader();
databaseList.Items.Clear();
while (reader.Read())
{
databaseList.Items.Add( reader.GetString(0) );
}
}
catch (MySqlException ex)
{
MessageBox.Show("Failed to populate database list: " + ex.Message );
}
finally
{
if (reader != null) reader.Close();
}
}

private void databaseList_SelectedIndexChanged(object sender, System.EventArgs e)
{
MySqlDataReader reader = null;

conn.ChangeDatabase( databaseList.SelectedItem.ToString() );

MySqlCommand cmd = new MySqlCommand("SHOW TABLES", conn);
try
{
reader = cmd.ExecuteReader();
tables.Items.Clear();
while (reader.Read())
{
tables.Items.Add( reader.GetString(0) );
}
}
catch (MySqlException ex)
{
MessageBox.Show("Failed to populate table list: " + ex.Message );
}
finally
{
if (reader != null) reader.Close();
}
}

Options: ReplyQuote


Subject
Written By
Posted
Re: Embed MySQL into c# .Net application
June 08, 2009 05:07AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.