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();
'C Sharp' 카테고리의 다른 글
C#으로 Excel문서 만들기 예제, 엑셀문서. (46) | 2015.04.21 |
---|---|
C# Exception Error Message (0) | 2015.04.21 |
C# File Read, Write, 파일 입출력 예제 (0) | 2015.04.10 |
C# DateTime 예제 (0) | 2015.04.09 |
C# List 사용예제. C# Collections.Generic - 2. (0) | 2015.04.08 |