以下是一个示例代码,用于在 C# 中操作 Access 数据库:
using System.Data.OleDb;
class AccessDB
{
// 数据库连接字符串
private readonly string connectionString;
public AccessDB(string filepath)
{
connectionString = $"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={filepath};Persist Security Info=False;";
}
// 查询语句示例
public void PerformQuery()
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
OleDbCommand command = new OleDbCommand("SELECT * FROM myTable", connection);
connection.Open();
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
// 处理每一行数据
string column1Value = reader.GetString(0);
int column2Value = reader.GetInt32(1);
// ...
}
reader.Close();
}
}
// 插入语句示例
public void PerformInsert()
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
OleDbCommand command = new OleDbCommand("INSERT INTO myTable (column1, column2) VALUES (?, ?)", connection);
command.Parameters.AddWithValue("?", "value1"); // 第一个参数的值
command.Parameters.AddWithValue("?", 123); // 第二个参数的值
connection.Open();
command.ExecuteNonQuery();
}
}
// 更新语句示例
public void PerformUpdate()
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
OleDbCommand command = new OleDbCommand("UPDATE myTable SET column1 = ? WHERE id = ?", connection);
command.Parameters.AddWithValue("?", "new value"); // 要更新的值
command.Parameters.AddWithValue("?", 1); // WHERE 子句中的参数值
connection.Open();
command.ExecuteNonQuery();
}
}
// 删除语句示例
public void PerformDelete()
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
OleDbCommand command = new OleDbCommand("DELETE FROM myTable WHERE id = ?", connection);
command.Parameters.AddWithValue("?", 1); // WHERE 子句中的参数值
connection.Open();
command.ExecuteNonQuery();
}
}
}
在实际应用中,需要根据具体情况修改连接字符串和 SQL 语句,以适应不同的 Access 数据库。
以下是利用C#操作Access数据库的示例代码:
using System.Data.OleDb;
public class AccessHelper
{
private static string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:datadatabase.mdb";
private static OleDbConnection oleDbConnection;
//打开数据库连接
public static void Open()
{
oleDbConnection = new OleDbConnection(connectionString);
if (oleDbConnection.State == System.Data.ConnectionState.Closed)
oleDbConnection.Open();
}
//关闭数据库连接
public static void Close()
{
if (oleDbConnection.State == System.Data.ConnectionState.Open)
oleDbConnection.Close();
}
//执行SQL语句,返回受影响的行数
public static int ExecuteNonQuery(string sql)
{
Open();
OleDbCommand cmd = new OleDbCommand(sql, oleDbConnection);
int affected = cmd.ExecuteNonQuery();
Close();
return affected;
}
//执行SQL语句,返回查询到的第一行第一列的值
public static object ExecuteScalar(string sql)
{
Open();
OleDbCommand cmd = new OleDbCommand(sql, oleDbConnection);
object result = cmd.ExecuteScalar();
Close();
return result;
}
//执行SQL语句,返回查询到的结果集
public static OleDbDataReader ExecuteReader(string sql)
{
Open();
OleDbCommand cmd = new OleDbCommand(sql, oleDbConnection);
OleDbDataReader reader = cmd.ExecuteReader();
return reader;
}
}
其中,connectionString
是连接Access数据库的字符串,根据实际情况修改即可。Open()
和Close()
方法分别用于打开和关闭数据库连接。ExecuteNonQuery(string sql)
方法执行给定的SQL语句并返回受影响的行数。ExecuteScalar(string sql)
方法执行给定的SQL语句并返回查询到的第一行第一列的值。ExecuteReader(string sql)
方法执行给定的SQL语句并返回查询到的结果集。使用方法如下:
string sql = "SELECT * FROM users WHERE username='admin'";
OleDbDataReader reader = AccessHelper.ExecuteReader(sql);
while (reader.Read())
{
string username = reader.GetString(reader.GetOrdinal("username"));
string password = reader.GetString(reader.GetOrdinal("password"));
Console.WriteLine("username: " + username + ", password: " + password);
}
reader.Close();
发布者:luotuoemo,转转请注明出处:https://www.jintuiyun.com/159159.html