Download Here: MySQL_C#WPF_Connection
While installing MySQL packages you have set username and password. Well my username and password is “root”. Now open a project in MS visual studio c#. I have chosen WPF application. And created a button whose content is set to “Connect”. I called the click event of the button when I click on it I may connect to MySQL database which I created. I created an object of MySQLConnection “con”. And con.ConnectionString = “ServerName;DataBaseName;Username;Password;”;
Hello guys, today i’m gonna teach you how to connect MYSQL
with MS visual studio c#. This tutorial is about MySQL workbench 5.2 and MySQL
server 5.5, make sure both are installed in your PC.so let’s get started first
of all open MySQL workbench 5.2 then click on New Server Instance. And follow
the steps to configure your server. When you are done make a database let’s say
I made a database name “Text” and Server name is Localhost.
Create table according to your need. I have created a table employee which has Idemployee and EmployeeName two attributes. Idemployee is primary key and ofcourse not null able. EmployeeName has size 45 characters. I have added two instances in the employee table:
Create table according to your need. I have created a table employee which has Idemployee and EmployeeName two attributes. Idemployee is primary key and ofcourse not null able. EmployeeName has size 45 characters. I have added two instances in the employee table:
Idemployee
|
EmployeeName
|
1
|
Umair
|
2
|
Baig
|
While installing MySQL packages you have set username and password. Well my username and password is “root”. Now open a project in MS visual studio c#. I have chosen WPF application. And created a button whose content is set to “Connect”. I called the click event of the button when I click on it I may connect to MySQL database which I created. I created an object of MySQLConnection “con”. And con.ConnectionString = “ServerName;DataBaseName;Username;Password;”;
This is the outline of connection string. Here my server name
is localhost, database name is text username and password is root. So my
connection string will be like this.
MySqlConnection con = new
MySqlConnection();
con.ConnectionString = @"Server
= localhost; Database = text; Uid = root ; Pwd = root;";
and I made try and catch block. It will avoid malfunctioning
of my application. If the exceptions arises the catch block will be executed. Here
is the code:
private void
button1_Click(object sender, RoutedEventArgs e)
{
MySqlConnection con = new
MySqlConnection();
con.ConnectionString = @"Server
= localhost; Database = text; Uid = root ; Pwd = root;";
try
{
con.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
if you want to check what is name of your server and database just write
con.DataSource.ToString() // ServerName
con.Database //DataBase Name
XAML Code:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Content="Connect" Height="23" HorizontalAlignment="Left" Margin="184,85,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
</Window>
C# Code:
private void button1_Click(object sender, RoutedEventArgs e)
{
MySqlConnection con = new MySqlConnection();
con.ConnectionString = @"Server = localhost; Database = text; Uid = root ; Pwd = root;";
try
{
con.Open();
MessageBox.Show(con.DataSource.ToString()+"\n"+con.Database+"\n");
MySqlCommand com = new MySqlCommand();
com.Connection = con;
com.CommandText = "Select * from employee";
MySqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
MessageBox.Show(reader[1].ToString());
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
No comments:
Post a Comment