2008-08-07 9 views
12

MS Office를 기반으로하는 응용 프로그램을 작성하려면 ADO.NET을 배워야합니다. MSDN Library에서 ADO.NET에 관해 많은 기사를 읽었지만 모든 것이 다소 혼란스러워 보입니다.ADO.NET을 배우는 방법

ADO.NET을 사용할 때 알아야 할 기본 사항은 무엇입니까? 나는 내 학습을 정리하기 위해 몇 가지 핵심 단어로 충분할 것이라고 생각한다. 당신이 뭔가를 사용하는 경우

  • SQLConnection
  • SqlCommand
  • SqlDataReader (과 Sql 교체 :

답변

5

(UR 사용하여 SQL 서버를 가정) 세 가지 주요 구성 요소가 있습니다 '무엇인가', 예 : MySqlConnection, OracleCommand)

다른 모든 것은 그 위에 구축되었습니다.

예 1 :

using (SqlConnection connection = new SqlConnection("CONNECTION STRING")) 
using (SqlCommand command = new SqlCommand()) 
{ 
    command.commandText = "SELECT Name FROM Users WHERE Status = @OnlineStatus"; 
    command.Connection = connection; 
    command.Parameters.Add("@OnlineStatus", SqlDbType.Int).Value = 1; //replace with enum 
    connection.Open(); 

    using (SqlDataReader dr = command.ExecuteReader)) 
    { 
     List<string> onlineUsers = new List<string>(); 

     while (dr.Read()) 
     { 
     onlineUsers.Add(dr.GetString(0)); 
     } 
    } 
} 

예 2 :

using (SqlConnection connection = new SqlConnection("CONNECTION STRING")) 
using (SqlCommand command = new SqlCommand()) 
{ 
    command.commandText = "DELETE FROM Users where Email = @Email"; 
    command.Connection = connection; 
    command.Parameters.Add("@Email", SqlDbType.VarChar, 100).Value = "[email protected]"; 
    connection.Open(); 
    command.ExecuteNonQuery(); 
} 
0

connection.CreateCommand()를 호출하는 명령 객체를 얻는 또 다른 방법입니다.

그런 식으로 명령 개체에 Connection 속성을 설정하지 않아도됩니다.

관련 문제