locked
Best way to connect to mysql from asp.net? RRS feed

  • Question

  • User660571870 posted

    Hi experts,

    What is the best way to connect to mysql from  asp.net? I am developing a public website and the user count may be huge. Can u provide sample code?How can I handle hundreds of simultaneous requests which demand database operation?

    Thanks a lot for your reply in advance

     

    Wednesday, December 8, 2010 12:35 AM

Answers

  • User-1348520733 posted

    using System.IO;
    using System.Collections;
    using System.Data.Odbc;
    /// <summary>
    /// Summary description for Class1
    /// </summary>
    public class Class1
    {
        OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=letz; User=root;Password=;Option=3;");
        OdbcCommand cmd = new OdbcCommand();
        OdbcDataAdapter da = new OdbcDataAdapter();
        DataSet ds = new DataSet();
        OdbcDataReader rd;


    Hi 

        I hope you are inquiring a similar connection code as below. Please see the below or mention more specifications if you require.

    Here I have created a class file and this class file will be called where ever needed. 


    // add these Namespace

    using System.IO;

    using System.Collections;

    using System.Data.Odbc;

    public class Class1

    {

        OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=localhost or remote;Database=xyz; User=user;Password=pass;Option=3;");

        OdbcCommand cmd = new OdbcCommand();

        OdbcDataAdapter da = new OdbcDataAdapter();

        DataSet ds = new DataSet();

        OdbcDataReader rd;



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 10, 2010 10:32 PM

All replies

  • User1643008609 posted

    Hi Friend,

    The bellow URL will provide you details

    http://www.15seconds.com/issue/050210.htm

    Wednesday, December 8, 2010 1:04 AM
  • User660571870 posted

    Hi

    Thanks for the reply.Actually I am looking for the sample code (C#) to do CRUD operations in the DB.

    Wednesday, December 8, 2010 5:43 AM
  • User1643008609 posted

    Hi Friend,

    It is also present is part 2 of same post.(Link at bottom)

    http://www.15seconds.com/issue/050407.htm    (Part 2)


    Wednesday, December 8, 2010 6:52 AM
  • User660571870 posted

    Thanks again friend.Is this the method followed for high traffic websites similar to orkut,facebook etc?

    Thursday, December 9, 2010 7:06 AM
  • User-1348520733 posted

    using System.IO;
    using System.Collections;
    using System.Data.Odbc;
    /// <summary>
    /// Summary description for Class1
    /// </summary>
    public class Class1
    {
        OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=letz; User=root;Password=;Option=3;");
        OdbcCommand cmd = new OdbcCommand();
        OdbcDataAdapter da = new OdbcDataAdapter();
        DataSet ds = new DataSet();
        OdbcDataReader rd;


    Hi 

        I hope you are inquiring a similar connection code as below. Please see the below or mention more specifications if you require.

    Here I have created a class file and this class file will be called where ever needed. 


    // add these Namespace

    using System.IO;

    using System.Collections;

    using System.Data.Odbc;

    public class Class1

    {

        OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=localhost or remote;Database=xyz; User=user;Password=pass;Option=3;");

        OdbcCommand cmd = new OdbcCommand();

        OdbcDataAdapter da = new OdbcDataAdapter();

        DataSet ds = new DataSet();

        OdbcDataReader rd;



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 10, 2010 10:32 PM
  • User-1348520733 posted

    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.IO;
    using System.Collections;
    using System.Data.Odbc;
    /// <summary>
    /// Summary description for Class1
    /// </summary>
    public class Class1
    {
        OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=letz; User=root;Password=;Option=3;");
        OdbcCommand cmd = new OdbcCommand();
        OdbcDataAdapter da = new OdbcDataAdapter();
        DataSet ds = new DataSet();
        OdbcDataReader rd;
        public void idu(string query)
        {
            cmd.Connection = cn;
            cmd.CommandText = query;
            cn.Open();
            cmd.ExecuteNonQuery();
            cn.Close();
        }
        public DataSet selection(string query)
        {
            cmd.Connection = cn;
            cmd.CommandText = query;
            da.SelectCommand = cmd;
            cn.Open();
            da.Fill(ds);
            cn.Close();
            return ds;
        }
        public Boolean chk(string qu)
        {
            cmd.Connection = cn;
            cmd.CommandText = qu;
            cn.Open();
            rd = cmd.ExecuteReader();
            Boolean st = rd.Read();
            cn.Close();
            return st;
        }
        public Class1()
        {
            //
            // TODO: Add constructor logic here
            //
        }
    }
    HI

    I missed your requirement for CRUD operations. Here is the sample code:

    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.IO;
    using System.Collections;
    using System.Data.Odbc;

    public class Class1
    {
        OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=letz; User=root;Password=;Option=3;");
        OdbcCommand cmd = new OdbcCommand();
        OdbcDataAdapter da = new OdbcDataAdapter();
        DataSet ds = new DataSet();


        OdbcDataReader rd;

        public void insert delete update(string query)
        {
            cmd.Connection = cn;
            cmd.CommandText = query;

            cn.Open();
            cmd.ExecuteNonQuery();
            cn.Close();
        }

        public DataSet selection(string query)
        {
            cmd.Connection = cn;
            cmd.CommandText = query;
            da.SelectCommand = cmd;
            cn.Open();
            da.Fill(ds);
            cn.Close();
            return ds;

        }



        public Boolean check(string qu)
        {
            cmd.Connection = cn;
            cmd.CommandText = qu;
            cn.Open();
            rd = cmd.ExecuteReader();
            Boolean st = rd.Read();
            cn.Close();
            return st;

        }
        public Class1()
        {
            //
            // TODO: Add constructor logic here
            //
        }
    }

    Friday, December 10, 2010 10:39 PM