在C#中使用OLEDB可以實現對數據的增刪改查操作,以下是一個簡單的例子:
using System.Data.OleDb;
string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\mydatabase.mdb";
OleDbConnection connection = new OleDbConnection(connectionString);
connection.Open();
// 插入數據
string insertQuery = "INSERT INTO myTable (column1, column2) VALUES ('value1', 'value2')";
OleDbCommand insertCommand = new OleDbCommand(insertQuery, connection);
insertCommand.ExecuteNonQuery();
// 更新數據
string updateQuery = "UPDATE myTable SET column1 = 'new value' WHERE column2 = 'value2'";
OleDbCommand updateCommand = new OleDbCommand(updateQuery, connection);
updateCommand.ExecuteNonQuery();
// 刪除數據
string deleteQuery = "DELETE FROM myTable WHERE column1 = 'value1'";
OleDbCommand deleteCommand = new OleDbCommand(deleteQuery, connection);
deleteCommand.ExecuteNonQuery();
// 查詢數據
string selectQuery = "SELECT * FROM myTable";
OleDbCommand selectCommand = new OleDbCommand(selectQuery, connection);
OleDbDataReader reader = selectCommand.ExecuteReader();
while(reader.Read())
{
// 讀取數據
}
connection.Close();
以上是一個簡單的示例,實際應用中需要根據具體情況來編寫更復雜的增刪改查操作。