본문 바로가기

C Sharp

C# MSSQL DataBase연동.

반응형

C#에서 DataBase 연동하기(MSSQL 기준)


//DataBase 연결

public string DBConnectionString = "";

System.Data.SqlClient.SqlConnection myConnection = null;

DBConnectionString = "server=(local); Database=사용할DB명; PWD=비밀번호; UID=계정;";

myConnection = new System.Data.SqlClient.SqlConnection(DBConnectionString);

myConnection.Open();


//Select Query 수행

string sQuery = "쿼리문";

System.Data.SqlClient.SqlDataAdapter myAdapter = 

new System.Data.SqlClient.SqlDataAdapter(sQuery, myConnection);

System.Data.DataSet ds = new System.Data.DataSet();

myAdapter.Fill(ds, "사용할데이터셋이름");

MessageBox.Show(ds.Tables[0].Rows[0][0].ToString());


//Excute Query 수행

string sQuery = "쿼리문";

System.Data.SqlClient.SqlDataAdapter myAdapter = 

new System.Data.SqlClient.SqlDataAdapter(sQuery, myConnection);

int ret = myAdapter.SelectCommand.ExecuteNonQuery();


//DataBase 연결 종료

if (myConnection.State == System.Data.ConnectionState.Open) myConnection.Close();


반응형