📜  使用C#的基本数据库操作(1)

📅  最后修改于: 2023-12-03 15:36:35.519000             🧑  作者: Mango

使用C#的基本数据库操作

C# 是一种强类型的面向对象编程语言,它提供了许多操作数据库的工具和框架。本文将介绍 C# 中使用基本数据库操作的方法,包括连接数据库、增删改查等。

连接数据库

连接数据库是进行数据库操作的第一步。在 C# 中,连接数据库需要创建 SqlConnection 对象并指定连接字符串。连接字符串是指连接数据库所需的参数信息,包括服务器名称、数据库名称、用户名和密码等信息。

以下是连接数据库的示例代码:

using System.Data.SqlClient;

string connectionString = "Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;";
SqlConnection connection = new SqlConnection(connectionString);

其中,myServerAddress表示服务器地址,myDataBase表示数据库名称,myUsernamemyPassword分别表示用户名和密码。

增加数据

添加数据是操作数据库的一种重要方式。在 C# 中,可以使用 SqlCommand 对象进行数据添加操作。通过设置 SqlCommand 对象的 CommandText 属性,可以指定要执行的 SQL 语句。然后通过调用 SqlCommand 对象的 ExecuteNonQuery 方法来执行 SQL 语句。

以下是添加数据的示例代码:

using System.Data.SqlClient;

SqlConnection connection = new SqlConnection(connectionString);
string queryString = "INSERT INTO Customers (CustomerName, ContactName, Country) VALUES (@CustomerName, @ContactName, @Country)";
SqlCommand command = new SqlCommand(queryString, connection);
command.Parameters.AddWithValue("@CustomerName", "Alfreds Futterkiste");
command.Parameters.AddWithValue("@ContactName", "Maria Anders");
command.Parameters.AddWithValue("@Country", "Germany");

connection.Open();
command.ExecuteNonQuery();
connection.Close();

其中,Customers是要添加数据的表名,CustomerNameContactNameCountry是该表的列名。@CustomerName@ContactName@Country是参数名,可以通过 command.Parameters.AddWithValue() 方法来设置参数的值。在执行 SQL 语句之前,需要通过 connection.Open() 方法打开数据库连接,并在执行完成后使用 connection.Close() 方法关闭数据库连接。

删除数据

删除数据是操作数据库的另一种重要方式。在 C# 中,可以使用 SqlCommand 对象进行数据删除操作。同样通过设置 SqlCommand 对象的 CommandText 属性,可以指定要执行的 SQL 语句。然后通过调用 SqlCommand 对象的 ExecuteNonQuery 方法来执行 SQL 语句。

以下是删除数据的示例代码:

using System.Data.SqlClient;

SqlConnection connection = new SqlConnection(connectionString);
string queryString = "DELETE FROM Customers WHERE CustomerID = @CustomerID";
SqlCommand command = new SqlCommand(queryString, connection);
command.Parameters.AddWithValue("@CustomerID", "ALFKI");

connection.Open();
command.ExecuteNonQuery();
connection.Close();

其中,Customers是要删除数据的表名,CustomerID是该表的列名。@CustomerID是参数名,可以通过 command.Parameters.AddWithValue() 方法来设置参数的值。

更新数据

更新数据是操作数据库的另一种常用方式。在 C# 中,可以使用 SqlCommand 对象进行数据更新操作。同样通过设置 SqlCommand 对象的 CommandText 属性,可以指定要执行的 SQL 语句。然后通过调用 SqlCommand 对象的 ExecuteNonQuery 方法来执行 SQL 语句。

以下是更新数据的示例代码:

using System.Data.SqlClient;

SqlConnection connection = new SqlConnection(connectionString);
string queryString = "UPDATE Customers SET ContactName = @ContactName WHERE CustomerID = @CustomerID";
SqlCommand command = new SqlCommand(queryString, connection);
command.Parameters.AddWithValue("@ContactName", "Maria Paula");
command.Parameters.AddWithValue("@CustomerID", "ALFKI");

connection.Open();
command.ExecuteNonQuery();
connection.Close();

其中,Customers是要更新数据的表名,ContactNameCustomerID是该表的列名。@ContactName@CustomerID是参数名,可以通过 command.Parameters.AddWithValue() 方法来设置参数的值。

查询数据

查询数据是常用的操作,可以使用 SqlCommand 和 SqlDataReader 对象进行数据查询。同样通过设置 SqlCommand 对象的 CommandText 属性,可以指定要执行的 SQL 语句。然后通过调用 SqlCommand 对象的 ExecuteReader 方法来返回 SqlDataReader 对象。在 SqlDataReader 对象中,可以通过调用 Read 方法逐行读取查询结果。

以下是查询数据的示例代码:

using System.Data.SqlClient;

SqlConnection connection = new SqlConnection(connectionString);
string queryString = "SELECT CustomerName, ContactName, Country FROM Customers WHERE CustomerID = @CustomerID";
SqlCommand command = new SqlCommand(queryString, connection);
command.Parameters.AddWithValue("@CustomerID", "ALFKI");

connection.Open();
SqlDataReader reader = command.ExecuteReader();

while (reader.Read())
{
    Console.WriteLine(String.Format("{0}, {1}, {2}",
        reader["CustomerName"], reader["ContactName"], reader["Country"]));
}

reader.Close();
connection.Close();

其中,Customers是要查询数据的表名,CustomerID是该表的列名。@CustomerID是参数名,可以通过 command.Parameters.AddWithValue() 方法来设置参数的值。

在 SqlDataReader 对象中,可以通过 reader["列名"] 来获取查询结果的某一列。在使用完 SqlDataReader 对象之后,需要通过 reader.Close() 方法关闭 SqlDataReader 对象。同样,在执行完成 SQL 语句之后,需要通过 connection.Close() 方法关闭数据库连接。

以上就是 C# 中使用基本数据库操作的方法,包括连接数据库、增删改查等。总之,学会了这些操作,就可以编写出更多实用的数据库应用程序。