2018년 10월 3일 수요일

C#에서 데이터 베이스에 연결하고, 쿼리를 실행하는 방법 (Mssql, .net)

개념은 잘 알고 있어도 막상 필요할때 코드를 작성할려면 매서드와 순서가 잘 기억나지 않는 애들이죠 ^^ 저도 기억이 나질 않을때를 대비할겸 이번엔 C#에서 디비 연동과 쿼리 실행 부분에 대해서 알아보겠습니다. (DB는 MSSQL을 기준으로 작성합니다만 윈도우 인증 부분을 제외하면 똑같으니 상관 없겠죠) 1. DB 연동 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 29 30 31 32 33 34 35 using System.Data.SqlClient; //DB connection SqlConnection sqlConnection = null; //IP string strDBIP = "127.0.0.1"; //ID string strDBID = "user"; //PW string strDBPW = "12345"; //DB string strDBName = "dbName"; //DB 인스턴스 생성 public SqlConnection connSql() {    sqlConnection = new SqlConnection("server =" + strDBIP + "; uid =" + strDBID + "; pwd =" + strDBPW + "; database =" + strDBName);        //윈도우 인증 계정일 경우엔    sqlConnection = new SqlConnection("Server= localhost; Database= dbName Integrated Security = True;");        //DB 오픈    if (sqlConnection != null && sqlConnection.State == ConnectionState.Closed)    {        sqlConnection.Open();    }        return sqlConnection;    } } //DB 연동 sqlConnection = connSql(); 공통...
Share: