这些仅供参考哈。是之前学习红书皮所编写的,还没完善,希望阅读的人可以给些意见。嘿嘿!以下是源码:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;
namespace AccessHandle
{
/// <summary>
/// 项目名:Access数据库
/// 程序员:Vseen
/// 日期/时间:2008.12.18
/// 联系QQ:56222777
/// Mail:XinSet@vip.qq.com
/// </summary>
public class AccessHandle
{
#region 「767工作室」
/// <summary>
/// 「767工作室」。
/// </summary>
public static string Programmer = "「767工作室」";
/// <summary>
/// 错误消息框。
/// </summary>
/// <param name="error">错误消息。</param>
public static void ErrorBox(string error)
{
MessageBox.Show("发生错误,请将以下错误信息截图,并记录发生错误的步骤,最后及时联系软件工作室成员。错误如下:\n\n" + error , Programmer , MessageBoxButtons.OK , MessageBoxIcon.Error);
}
#endregion
#region 实例一个Access数据库。
/// <summary>
/// 实例一个Access数据库。
/// </summary>
/// <param name="dataFile">数据库完整路径。</param>
/// <param name="userID">数据库账号。</param>
/// <param name="password">数据库密码。</param>
public AccessHandle(string dataFile , string userID , string password)
{
myConn = new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0;Jet OLEDB:" + //数据库引擎名称以及版本号
"Database Password=" + password + //数据库密码
";User ID=" + userID + //数据库账户
";Data Source=" + dataFile); //数据库路径。
}
#endregion
/// <summary>
/// 连接对象(数据源),与数据库相连接。
/// </summary>
OleDbConnection myConn = null;
/// <summary>
/// 数据集。
/// </summary>
public DataSet mySet = new DataSet();
#region 保存指定表
/// <summary>
/// 保存指定表。
/// </summary>
/// <param name="tableName">表名。</param>
public void Update(string tableName)
{
try
{
myConn.Open();
OleDbCommand myCommand = new OleDbCommand("select * from " + tableName , myConn);
myConn.Close();
OleDbDataAdapter MyAdapter = new OleDbDataAdapter(myCommand);
OleDbCommandBuilder ol = new OleDbCommandBuilder(MyAdapter);
MyAdapter.Update(mySet.Tables[tableName]);
}
catch (Exception ex)
{
ErrorBox("保存失败!!\n\n" + ex.Message);
}
finally
{
if (myConn.State != ConnectionState.Closed) myConn.Close();
}
}
#endregion
#region 创建表
/// <summary>
/// 创建、删除、修改一张表。
/// </summary>
/// <param name="sqlText">SQL语句。</param>
/// <returns></returns>
public bool TableHandle(string sqlText)
{
try
{
OleDbCommand myCommand = new OleDbCommand(sqlText , myConn);
myConn.Open();
myCommand.ExecuteNonQuery();
myConn.Close();
string[] strs = sqlText.Trim().Split(' ');
if (strs[0].ToLower() == "drop") mySet.Tables.Remove(strs[2]);
return true;
}
catch (Exception ex)
{
ErrorBox(ex.Message);
}
finally
{
if (myConn.State != ConnectionState.Closed) myConn.Close();
}
return false;
}
#endregion
#region 返回指定数据表
/// <summary>
/// 返回指定数据表。
/// </summary>
/// <param name="tableName">数据表名。</param>
/// <returns></returns>
public DataTable GetTable(string tableName)
{
if (!mySet.Tables.Contains(tableName))
{
OleDbDataAdapter myAdapter = new OleDbDataAdapter("select * from " + tableName , myConn);
myAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
try
{
myConn.Open();
myAdapter.Fill(mySet , tableName);
myConn.Close();
}
catch (Exception ex)
{
ErrorBox("找不到表?\n\n" + ex.Message);
return null;
}
finally
{
if (myConn.State != ConnectionState.Closed) myConn.Close();
}
}
return mySet.Tables[tableName];
}
#endregion
#region 返回所有表名
/// <summary>
/// 返回所有表的名称。
/// </summary>
/// <returns></returns>
public string[] GetTablesName()
{
List<string> names = new List<string>();
try
{
OleDbCommand myCommand = new OleDbCommand("SELECT * FROM MSysObjects WHERE Flags=0 AND Type=1" , myConn);
myConn.Open();
OleDbDataReader myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
names.Add(myReader["Name"].ToString());
}
myConn.Close();
}
catch (Exception ex)
{
ErrorBox(ex.Message);
return null;
}
finally
{
if (myConn.State != ConnectionState.Closed) myConn.Close();
}
return names.ToArray();
}
#endregion
#region 返回指定数据表的结构
/// <summary>
/// 返回指定数据表的结构。
/// </summary>
/// <param name="tableName">数据表名。</param>
/// <returns></returns>
public DataTable GetSchemaTable(string tableName)
{
OleDbCommand myCommand = new OleDbCommand("select * from " + tableName , myConn);
DataTable table = null;
try
{
myConn.Open();
table = myCommand.ExecuteReader(CommandBehavior.KeyInfo).GetSchemaTable();//CommandBehavior.KeyInfo<--重要!
myConn.Close();
}
catch (Exception ex)
{
ErrorBox(ex.Message);
}
finally
{
if (myConn.State != ConnectionState.Closed) myConn.Close();
}
return table;
}
/// <summary>
/// 返回最后一张数据表的结构。
/// </summary>
/// <returns></returns>
public DataTable GetSchemaTable()
{
if (mySet.Tables.Count == 0)
{
string[] str = GetTablesName();
return GetSchemaTable(str[str.Length - 1]);
}
return GetSchemaTable(mySet.Tables[mySet.Tables.Count - 1].TableName);
}
#endregion
#region 返回第一行、第一列的值
/// <summary>
/// 返回第一行、第一列的值。
/// </summary>
/// <param name="sqlText">查询的SQL。</param>
/// <returns></returns>
public object GetExecuteScalar(string sqlText)
{
OleDbCommand myCommand = new OleDbCommand(sqlText , myConn);
object obj = null;
try
{
myConn.Open();
obj = myCommand.ExecuteScalar();
myConn.Close();
}
catch (Exception ex)
{
ErrorBox(ex.Message);
}
finally
{
if (myConn.State != ConnectionState.Closed) myConn.Close();
}
return obj;
}
#endregion
#region XML数据库
#endregion
#region 临时视图
/// <summary>
/// 返回指定表级的临时视图。
/// </summary>
/// <param name="baseTable">主表。</param>
/// <param name="sonTables">子表。</param>
/// <returns></returns>
public DataTable MergeTables(string baseTable , params string[] sonTables)
{
if (!mySet.Tables.Contains(baseTable))
GetTable(baseTable);
foreach (string table in sonTables)
if (!mySet.Tables.Contains(table))
GetTable(table);
try
{
foreach (string table in sonTables)
mySet.Tables[baseTable].Merge(mySet.Tables[table] , true , MissingSchemaAction.AddWithKey);
}
catch (Exception ex)
{
ErrorBox(ex.Message);
return null;
}
return mySet.Tables[baseTable];
}
#endregion
#region 回滚到最后一次正确保存
/// <summary>
/// 回滚到最后一次正确保存。
/// </summary>
/// <param name="tableName">表名</param>
public void Reject(string tableName)
{
mySet.Tables[tableName].RejectChanges();
}
#endregion
}
}