블로그 이미지
Magic_kit
study 관련자료를 한곳으로 자기 개발 목적으로 재태크 재무 관리 목적으로 일상생활의 팁을 공유 하기 위하여 블로그를 개설 하였습니다.

calendar

1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28

Category

Recent Post

Recent Comment

Archive

2009. 9. 25. 09:38 .Net Project/ADO.NET 3.5
반응형

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class FrmADONET : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            DisplayDatees();
        }
    }


    //데이터 불러온다
   private void DisplayDatees()
    {
        SqlConnection conn = new SqlConnection("Data Source = .;Initial 
                              Catalog=Market;User ID=Market;Password=1234;");
        conn.Open();

        SqlCommand cmdd = new SqlCommand
                                     ("Select *From Categories", conn);
        cmdd.CommandType = CommandType.Text;

        //ExecuteReader()메서드의 결과값을 DataReader개체에 담기
        SqlDataReader dr = cmdd.ExecuteReader();

        //GridView바인딩
        this.ctrlGridView.DataSource = dr; //datareader, dataSet, List<T>
        this.ctrlGridView.DataBind(); //그렇지 않으면 알아서 모양 만들어 출력

        dr.Close(); //데이터리더도 반드시 Close()해야 함...

        conn.Close();
    }


    protected void btnCatory_Click(object sender, EventArgs e)
    {
        //[0]변수선언부
        string strCategoryName = txtInput.Text.Trim();
        //[1]커넥션
        SqlConnection con = new SqlConnection();
        con.ConnectionString = 
       "server=.;database=Market;uid=Market;pwd=1234;";
        //lblDisplay.Text = "연결되었습니다";
      
        con.Open();

        //[2]커맨드
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = String.Format
               ("Insert Categories(CategoryName)
                 Values('{0}')", strCategoryName);
        cmd.CommandType = CommandType.Text;

        //[3]실행 
        // 모든 명령어 실행 <-> Selecteh 가능 -> Reader사용 
        int rows = cmd.ExecuteNonQuery();    
        this.lblDisplay.Text = rows.ToString() + "개 입력되었습니다";

        con.Close();

    }


    //새로고침
    protected void BtnRefresh_Click(object sender, EventArgs e)
    {
        DisplayDatees();
        //ctrlGridView.DataBind();
    }


}


반응형
posted by Magic_kit