none
How to set connection string and connect database RRS feed

  • Question

  • Web.Config

    <connectionStrings> <add name="NorthwindConnectionString" connectionString="Data Source=serverName;Initial Catalog=Northwind;Persist Security Info=True;User ID=userName;Password=password" providerName="System.Data.SqlClient" /> </connectionStrings>

    ASP

    SqlConnection con = new SqlConnection();
    protected string con_string = WebConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;

    protected DataSet GetData()
        {
           string sql = "Your SQL";
            con.ConnectionString = con_string;
            SqlCommand cmd = new SqlCommand(sql, con);

            try
            {
                if (con.State == ConnectionState.Open)
                {
                    con.Close();
                }
                con.Open();

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds, "Data");
                return ds;
            }
            catch (Exception ex)
            {
                return null;
            }
            finally { con.Close(); }
        }

    Tuesday, March 10, 2015 8:06 AM

Answers

  • You can using this:

    using System.Data.SqlClient; SqlConnection conn = new SqlConnection(); conn.ConnectionString = "Data Source=ServerName;" + "Initial Catalog=DataBaseName;" + "User id=UserName;" + "Password=Secret;"; conn.Open();

    Tuesday, March 10, 2015 8:08 AM
  • Hello,

    You could check all connecting string on this site:

    http://www.connectionstrings.com/

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, March 11, 2015 2:30 AM
    Moderator

All replies

  • You can using this:

    using System.Data.SqlClient; SqlConnection conn = new SqlConnection(); conn.ConnectionString = "Data Source=ServerName;" + "Initial Catalog=DataBaseName;" + "User id=UserName;" + "Password=Secret;"; conn.Open();

    Tuesday, March 10, 2015 8:08 AM
  • Or can Use simply Visual Studio Sql connection Wizard :)
    Tuesday, March 10, 2015 8:09 AM
  • Hello,

    You could check all connecting string on this site:

    http://www.connectionstrings.com/

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, March 11, 2015 2:30 AM
    Moderator