[ASP.NET] ADONET : ms-sql 데이터베이스 입력 (insert)
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebADONET
{
public partial class FrmSqlCommand : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_SqlCommand_Click(object sender, EventArgs e)
{
//[1] sqlonnection 클래스의 인스턴스 생성
SqlConnection con = new SqlConnection();
//[2] ConnectionString 속성지정
con.ConnectionString = "server=SUPERMAN\\MSQL;database=study;uid=study;pwd=XXXXX";
//[3] Open 메소드로 데이터베이스 연결
con.Open();
//[!] 입력예시문 실행
//sqlCommand의 인스턴스 생성
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.Connection = con;
// CommandText 속성에 모든 명령어 지정
sqlCmd.CommandText = "insert Memos Values('홍길동', 'abc@abc.com', '홍길동 다녀갑니다.', GetDate(), '127.0.0.1')";
//CommandType 속서어 : Text, SP
sqlCmd.CommandType = CommandType.Text;
// 실행
sqlCmd.ExecuteNonQuery();
lbl_Display.Text = "데이터베이스 입력됨";
//[4] Close 메소드로 데이터베이스 연결 종료
con.Close();
}
}
}
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebADONET
{
public partial class FrmSqlCommand : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_SqlCommand_Click(object sender, EventArgs e)
{
//[1] sqlonnection 클래스의 인스턴스 생성
SqlConnection con = new SqlConnection();
//[2] ConnectionString 속성지정
con.ConnectionString = "server=SUPERMAN\\MSQL;database=study;uid=study;pwd=XXXXX";
//[3] Open 메소드로 데이터베이스 연결
con.Open();
//[!] 입력예시문 실행
//sqlCommand의 인스턴스 생성
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.Connection = con;
// CommandText 속성에 모든 명령어 지정
sqlCmd.CommandText = "insert Memos Values('홍길동', 'abc@abc.com', '홍길동 다녀갑니다.', GetDate(), '127.0.0.1')";
//CommandType 속서어 : Text, SP
sqlCmd.CommandType = CommandType.Text;
// 실행
sqlCmd.ExecuteNonQuery();
lbl_Display.Text = "데이터베이스 입력됨";
//[4] Close 메소드로 데이터베이스 연결 종료
con.Close();
}
}
}