gpt4 book ai didi

ado.net - 如何学习 ADO.NET

转载 作者:行者123 更新时间:2023-12-02 09:08:11 29 4
gpt4 key购买 nike

我需要学习 ADO.NET 来构建基于 MS Office 的应用程序。我在 MSDN 库中阅读了大量有关 ADO.NET 的内容,但对我来说,一切都显得相当困惑。

使用 ADO.NET 时必须了解哪些基础知识?我想几个关键词就足以让我组织我的学习了。

最佳答案

共有三个关键组件(假设您使用 SQL Server):

  • SQLConnection
  • SqlCommand
  • SqlDataReader

(如果您使用其他内容,请将 Sql 替换为“Something”,例如 MySqlConnectionOracleCommand)

其他一切都建立在其之上。

示例 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();
}

关于ado.net - 如何学习 ADO.NET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4170/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com