Tuesday, September 10, 2013

How to connect to mysql using asp c#

You need things as below
- mysql database access user
- mysql connector dll file: MySql.Data.dll
- visual studio 9.0 2008

1. You can add MySql.Data.dll reference from solution explorer, if there is not you can download here
 if download is not working notify me pls



2. add connection string on web.config file


<connectionStrings>
    <add name="constr_mysql" connectionString="server=192.168.0.10;uid=root; pwd=pass;database=db_name; Allow Zero Datetime=true" providerName="MySql.Data.MySqlClient"/>
</connectionStrings>

3. Using connection string.

add using reference on c# code  ========================================



using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MySql.Data.MySqlClient;
using MySql.Data.Types;

Declare mysql connection string on c# code =============================


string strConnectionString = ConfigurationManager.ConnectionStrings["constr_mysql"].ConnectionString;



click event on some button  ============================================


MySqlConnection conn = new MySqlConnection(strConnectionString);
MySqlCommand command = conn.CreateCommand();
command.CommandText = "select * from tbl_name;";
conn.Open();
Repeater1.DataSource = command.ExecuteReader();
Repeater1.DataBind();
conn.Close();


That's it :)







No comments:

Post a Comment