문제
MS Office 기반 애플리케이션을 구축하려면 ADO.NET을 배워야 합니다.MSDN Library에서 ADO.NET에 대한 많은 내용을 읽었지만 모든 것이 다소 지저분해 보입니다.
ADO.NET을 사용할 때 알아야 할 기본 사항은 무엇입니까?나는 나의 학습을 체계화하는 데 몇 가지 핵심 단어만으로 충분할 것이라고 생각합니다.
해결책
세 가지 주요 구성 요소가 있습니다(SQL 서버를 사용한다고 가정).
SQLConnection
SqlCommand
SqlDataReader
(다른 것을 사용하는 경우 교체하십시오. 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 = "user@host.com";
connection.Open();
command.ExecuteNonQuery();
}
다른 팁
명령 객체를 얻는 또 다른 방법은 다음을 호출하는 것입니다. connection.CreateCommand()
.
그렇게하면 설정하지 않아도됩니다. Connection
명령 개체의 속성입니다.
제휴하지 않습니다 StackOverflow