C# MSSQL 접속 sample code
프로그래밍2008. 8. 31. 23:25
using System;
using System.Data;
using System.Data.SqlClient;
public class DB_TEST
{
public static void Main(string[] args)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString =
"Server=ip;database=CL_ADMIN;uid=sa;pwd=password;";
try
{
conn.Open();
Console.WriteLine("데이터베이스 연결성공..");
String log = " SELECT * FROM dbo.OB_TEST"; // 쿼리문
SqlCommand cmd = new SqlCommand(log, conn); // 적용
SqlDataReader rd = cmd.ExecuteReader(); // 쿼리 수행
while (rd.Read()) //한줄씩 잃기
{
Console.WriteLine(String.Format("{0}, {1}", rd[0],rd[1]));
//내용 출력하는 부분
}
rd.Close();
}
catch
{
Console.WriteLine("데이터베이스 연결 실패..");
}
finally
{
if (conn != null)
{
conn.Close();
}
Console.WriteLine("데이터베이스 연결해제");
}
}
}
using System.Data;
using System.Data.SqlClient;
public class DB_TEST
{
public static void Main(string[] args)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString =
"Server=ip;database=CL_ADMIN;uid=sa;pwd=password;";
try
{
conn.Open();
Console.WriteLine("데이터베이스 연결성공..");
String log = " SELECT * FROM dbo.OB_TEST"; // 쿼리문
SqlCommand cmd = new SqlCommand(log, conn); // 적용
SqlDataReader rd = cmd.ExecuteReader(); // 쿼리 수행
while (rd.Read()) //한줄씩 잃기
{
Console.WriteLine(String.Format("{0}, {1}", rd[0],rd[1]));
//내용 출력하는 부분
}
rd.Close();
}
catch
{
Console.WriteLine("데이터베이스 연결 실패..");
}
finally
{
if (conn != null)
{
conn.Close();
}
Console.WriteLine("데이터베이스 연결해제");
}
}
}
'프로그래밍' 카테고리의 다른 글
php, mssql 연동이 되지 않을 때. (0) | 2009.04.02 |
---|---|
NAVER OpenAPI 에 대한 잡설. (4) | 2009.02.10 |
IBM Lotus Symphony 무료 오피스(Beta) 사용자 리뷰 컨테스트 (0) | 2007.12.10 |
놀라운 Ajax solitaire 카드 놀이 (0) | 2007.12.07 |
Ping 날리는 소스 (2) | 2007.11.21 |