源代码如下:
using System; using System.Data; using System.Data.OleDb; using System.IO; using System.Windows.Forms; namespace Test { class Database { public static bool DataBase_Exists() { if (!File.Exists(Application.StartupPath + "\\DataBase.mdb")) { return false; } else { return true; } } public OleDbConnection Get_Conn() { try { string Conn_String = ""; Conn_String = "Data Source='DataBase.mdb'; Provider=Microsoft.ACE.OLEDB.12.0;"; OleDbConnection Db_Conn = new OleDbConnection(Conn_String); Db_Conn.Open(); return Db_Conn; } catch (Exception e) { Error.Database_Error_Conn = e.Message.ToString(); return null; } } public OleDbDataReader Select(string Select_String) { try { if (Get_Conn() == null) { return null; } OleDbCommand Db_Cmd = new OleDbCommand(Select_String, Get_Conn()); OleDbDataReader Db_Reader = Db_Cmd.ExecuteReader(); Db_Reader.Read(); return Db_Reader; } catch (Exception e) { Error.Database_Error_Select = e.Message.ToString(); return null; } } public int Action_The_SQL(string SQL_String) { try { if (Get_Conn() == null) { return 0; } OleDbCommand Db_Cmd = new OleDbCommand(SQL_String, Get_Conn()); int Line = Db_Cmd.ExecuteNonQuery(); return Line; } catch (Exception e) { Error.Database_Error_Action_The_SQL = e.Message.ToString(); return 0; } } public DataSet GetDataSet(string SQL_String, string tableName) { try { if (Get_Conn() == null) { return null; } OleDbDataAdapter Adapter = new OleDbDataAdapter(SQL_String, Get_Conn()); DataSet DS = new DataSet(); Adapter.Fill(DS, tableName); return DS; } catch (Exception j) { Error.Database_Error_GetDataSet = j.Message.ToString(); return null; } } public string GetData(string SQL_String) { try { if (Get_Conn() == null) { return null; } OleDbCommand Db_Cmd = new OleDbCommand(SQL_String, Get_Conn()); string data = Convert.ToString(Db_Cmd.ExecuteScalar()); return data; } catch (Exception e) { Error.Database_Error_Action_The_SQL = e.Message.ToString(); return null; } } } }
其引用的错误(Error)类源代码如下:
namespace Test{ class Error { public static string Database_Error_Select; public static string Database_Error_Conn; public static string Database_Error_Action_The_SQL; public static string Database_Error_GetDataSet; } }
时间: 2024-11-08 20:13:10