写在前面:
常用数据库:
SQLserver:https://www.cnblogs.com/mexihq/p/11636785.html
Oracle:https://www.cnblogs.com/mexihq/p/11700741.html
MySQL:https://www.cnblogs.com/mexihq/p/12463423.html
Access:https://www.cnblogs.com/mexihq/p/12466970.html
在日常的工作中,通常一个项目会大量用的数据库的各种基本操作,因此小编几个常见的数据库的操作封装成了一个dll方便后续的开发使用。本文则主要是记录了C#对Access的增、删、改、查的基本操作,如有什么问题还请各位大佬指教。后续也将对其他几个常用的数据库进行相应的整理。话不多说,直接开始码代码。
引用:
using System.Data; //DataSet引用 using System.Data.OleDb; //Access引用
Access的增删改:
/// <summary>
/// Access insert,delete,update
/// </summary>
/// <param name="sql">insert,delete,update statement</param>
/// <param name="link">link statement</param>
/// <returns>Success:success + Number of affected rows; Fail:reason</returns>
public string Access_Mdb_Insdelupd(string sql, string link)
{
try
{
using (OleDbConnection oleDbConnection = new OleDbConnection(link))
{
DataSet dataSet = new DataSet();
oleDbConnection.Open();
OleDbCommand oleDbCommand = new OleDbCommand(sql, oleDbConnection);
int num = oleDbCommand.ExecuteNonQuery();
oleDbConnection.Close();
return "success" + num;
}
}
catch (Exception ex)
{
return ex.Message.ToString();
}
}
Access的查:
/// <summary>
/// Access select
/// </summary>
/// <param name="sql">select statement</param>
/// <param name="link">link statement</param>
/// <param name="record">Success:success; Fail:reason</param>
/// <returns>select result</returns>
public DataSet Access_Mdb_Select(string sql, string link, out string record)
{
try
{
DataSet dataSet = new DataSet();
using (OleDbConnection oleDbConnection = new OleDbConnection(link))
{
oleDbConnection.Open();
OleDbDataAdapter oleDbDataAdapter = new OleDbDataAdapter(sql, oleDbConnection);
oleDbDataAdapter.Fill(dataSet, "sample");
oleDbDataAdapter.Dispose();
oleDbConnection.Close();
record = "success";
return dataSet;
}
}
catch (Exception ex)
{
DataSet dataSet = new DataSet();
record = ex.Message.ToString();
return dataSet;
}
}