블로그 이미지
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. 29. 15:12 .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 FrmSqlParamter : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DisplayData();

    }

    private void DisplayData()
    {
        SqlConnection con = new SqlConnection
               (ConfigurationManager.ConnectionStrings
                 ["ConnectionString"].ConnectionString);
        con.Open();

        SqlCommand cmd = new SqlCommand
                     ("Select *From Categories", con);

        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds, "Categories");

        this.ctlCategoryList.DataSource = ds.Tables[0];
        this.ctlCategoryList.DataBind();

        con.Close();
       
    }
    //추가
    protected void btnAddConnection_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = ConfigurationManager.ConnectionStrings
                     ["ConnectionString"].ConnectionString;

        con.Open();

        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = 
     "Insert Into Categories(CategoryName) Values(@CategoryName)";
        cmd.CommandType = CommandType.Text;
             

        //[1] 파라미터 추가하는 첫번째 방법
        //cmd.Parameters.AddWithValue

               ("@CategoryName", txtCategoryName.Text);

        //[2] 파라미터 추가하는 두번째 방법
        cmd.Parameters.Add("@CategoryName", SqlDbType.VarChar, 50);
        cmd.Parameters["@CategoryName"].Value = txtCategoryName.Text;

        //[3] SqlParamter 클래스 사용 : 
        //Sp의 Output 매개변수 처리시 무조건 사용 하여야 한다.
        //SqlParameter ParamterCategoryName =
        //new SqlParameter("@CategoryName", SqlDbType.VarChar, 50);

        //ParamterCategoryName.Direction = ParameterDirection.Input; 
        //ParamterCategoryName.Value = txtCategoryName.Text;
        //cmd.Parameters.Add(ParamterCategoryName);

       
        cmd.ExecuteNonQuery(); //실행
       
        con.Close();
        DisplayData();
    }
}




반응형
posted by Magic_kit