2012.09.12 13:33

Web.config connectionStrings(MySQL & M/S SQL)

Web.config 의 connectionStrings 영역에 커넥션 문구 지정후 페이지에서 불러오는 내용의 예제입니다.

MySql Connection 과 M/S SQL Connection 입니다.



# web.config


    <connectionStrings>


       <!--MySql Connection-->
        <add name="MYDB" connectionString="SERVER=10.0.0.100;DATABASE=MYDB;user id=mYuser;PASSWORD=password"/>


      <!--M/S SQL Connection-->
    <add name="MSSQL" connectionString="server=10.0.0.200;database=MSSQL;user id=mSuser;password=password;Network Library=DBMSSOCN" />


    </connectionStrings>
 




# MYSQL


 using (MySqlConnection con = new MySqlConnection(ConfigurationManager.ConnectionStrings["MYDB"].ToString()))
        {
            MySqlCommand cmd = new MySqlCommand();
            cmd.Connection = con;


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

            cmd.CommandText = "쿼리";

            MySqlDataReader DbReader = cmd.ExecuteReader();


            while (W_DbReader.Read())
            {        
                Label1.Text += "<hr>" + "seq : " + W_DbReader.GetString(0) + "<br>";
                Label1.Text += "Url : " + W_DbReader.GetString(1) + "<br>";
                Label1.Text += "CompanyId: " + W_DbReader.GetString(2) + "<br>";
                Label1.Text += "StatusCode : " + W_DbReader.GetString(3) + "<p>";
                Label1.Text += "Result : " + W_DbReader.GetString(4) + "<p>";
                Label1.Text += "TimeGenerated : " + W_DbReader.GetString(5) + "<p>";

            }

            DbReader.Close();
            con.Close();

        }





# M/S SQL


        using(SqlConnection wcon=new SqlConnection(ConfigurationManager.ConnectionStrings["MSSQL"].ToString()))
        {
            SqlCommand wcmd = new SqlCommand();
            wcmd.Connection = wcon;

            if (wcon.State == ConnectionState.Closed)
            {
                wcon.Open();
            }

            wcmd.CommandText = "쿼리";

            SqlDataReader W_DbReader = wcmd.ExecuteReader();

          
            while (W_DbReader.Read())
            {
                Label1.Text += "<hr>" + "seq : " + W_DbReader[0].ToString() + "<br>";
                Label1.Text += "Url : " + W_DbReader[1].ToString() + "<br>";
                Label1.Text += "CompanyId: " + W_DbReader[2].ToString() + "<br>";
                Label1.Text += "StatusCode : " + W_DbReader[3].ToString() + "<p>";
                Label1.Text += "Result : " + W_DbReader[4].ToString() + "<p>";
                Label1.Text += "TimeGenerated : " + W_DbReader[5].ToString() + "<p>";

            }
            W_DbReader.Close();
            wcon.Close();
        }

Trackback 0 Comment 0