posted by 네코냥이 2013. 5. 11. 20:00

ADO.NET + ASP.NET DB 연동 간단한 예제  ADO.NET 

2012/03/09 11:47

복사http://cepiloth.blog.me/70133307765


서버는 IIS

DBMS 는 MSSQL

 

 

ASP.NET 웹페이지에서 ADO.NET 를 사용하여

데이타 베이스를 연결하는 간단한 예제

 

 

using System.Data;
using System.Data.SqlClient; //SQL 서버 데이터를 사용할 때

 

 

using System.Data;
using System.Data.OleDB; // OLeDb 데이터를 사용할때

 

 

show page (aspx, asp)

 

 protected void Page_Load(object sender, EventArgs e)
    {
        string source = @"Server=localhost;uid=sa;pwd=1234;database=student";

        //1. db connect
        SqlConnection conn = new SqlConnection(source);
        conn.Open();

        //2. instruction 
        String sql = "SELECT * FROM IMAGES";
        SqlCommand cmd = new SqlCommand(sql, conn);

        //3. instruction exe
        SqlDataReader reader = cmd.ExecuteReader();

        //4. show pages

        Response.Write("<table border=1>");

        while (reader.Read())
        {
            Response.Write("<tr>");

            for (int i = 0; i<reader.FieldCount; i++)
            {
                Response.Write("<td>"+reader[i]+"</td>");
            }
            Response.Write("</tr>");
        }

        Response.Write("</table>");

        // 5. db close
        reader.Close();
        conn.Close();
    }