This article is a mirror article of machine translation, please click here to jump to the original article.

View: 21413|Reply: 3

[Source] c# operation access database DBHelp class

[Copy link]
Posted on 9/23/2015 9:41:24 AM | | | |
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data.OleDb;
  6. using System.Data;

  7. namespace Model
  8. {
  9.     public class AccessDBHelper
  10.     {
  11.         /// <summary>
  12.         /// 操作结果消息
  13.         /// </summary>
  14.         public static string errorMessage = string.Empty;
  15.         public const string connStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=.\aaa.mdb; Jet OLEDB:Database Password=111";
  16.         /// <summary>
  17.         /// 执行SQL语句.返回执行结果对象
  18.         /// 【Select:返回DataTable 】【Insert/Update/Delete:返回Int型受印象行数】【 CreateTable/DropTable:返回True||False】
  19.         /// </summary>
  20.         /// <param name="sqlStr">要执行的SQL语句</param>
  21.         /// <param name="connStr">数据库连接字符串</param>
  22.         /// <param name="opt">要执行的操作</param>
  23.         /// <returns>
  24.         /// 返回执行结果对象【Select:返回DataTable 】【Insert/Update/Delete:返回Int型受印象行数】【 CreateTable/DropTable:返回True||False】
  25.         /// </returns>
  26.         /// <Author> frd 2011-9-9</Author>
  27.         public static object ExecSql(string sqlStr, OperateType opt)
  28.         {
  29.             object returnObj = new object();
  30.             OleDbConnection odbConn = new OleDbConnection(connStr);
  31.             odbConn.Open();
  32.             switch (opt)
  33.             {
  34.                 case OperateType.Select:
  35.                     {
  36.                         DataTable selectResult = new DataTable();
  37.                         try
  38.                         {
  39.                             OleDbDataAdapter dataAdapter = new OleDbDataAdapter(sqlStr, connStr);
  40.                             DataSet dataSet = new DataSet();
  41.                             dataAdapter.Fill(dataSet, "Select");
  42.                             returnObj = dataSet.Tables[0];
  43.                         }
  44.                         catch (Exception ex)
  45.                         {
  46.                             returnObj = -1;
  47.                             errorMessage = "数据查询失败:" + ex.ToString();
  48.                         }
  49.                         finally
  50.                         {
  51.                             odbConn.Close();
  52.                         }
  53.                     }
  54.                     break;
  55.                 case OperateType.Insert:
  56.                 case OperateType.Update:
  57.                 case OperateType.Delete:
  58.                     {
  59.                         try
  60.                         {
  61.                             OleDbCommand odbc = new OleDbCommand(sqlStr, odbConn);
  62.                             returnObj = odbc.ExecuteNonQuery();
  63.                         }
  64.                         catch (Exception ex)
  65.                         {
  66.                             returnObj = -1;
  67.                             errorMessage = "数据操作失败:" + ex.ToString();
  68.                         }
  69.                         finally
  70.                         {
  71.                             odbConn.Close();
  72.                         }
  73.                     }
  74.                     break;
  75.                 case OperateType.CreateTable:
  76.                 case OperateType.DropTable:
  77.                     {
  78.                         try
  79.                         {
  80.                             OleDbCommand odbc = new OleDbCommand(sqlStr, odbConn);
  81.                             odbc.ExecuteNonQuery();
  82.                             returnObj = true;
  83.                         }
  84.                         catch (Exception ex)
  85.                         {
  86.                             returnObj = false;
  87.                             errorMessage = "数据操作失败:" + ex.ToString();
  88.                         }
  89.                         finally
  90.                         {
  91.                             odbConn.Close();
  92.                         }
  93.                     }
  94.                     break;
  95.                 default:
  96.                     { odbConn.Close(); }
  97.                     break;
  98.             }

  99.             return returnObj;
  100.         }

  101.         /// <summary>
  102.         /// 数据库操作类型
  103.         /// </summary>
  104.         public enum OperateType
  105.         {
  106.             /// <summary>
  107.             /// 查询记录
  108.             /// </summary>
  109.             Select,
  110.             /// <summary>
  111.             /// 插入记录
  112.             /// </summary>
  113.             Insert,
  114.             /// <summary>
  115.             /// 更新记录
  116.             /// </summary>
  117.             Update,
  118.             /// <summary>
  119.             /// 删除记录
  120.             /// </summary>
  121.             Delete,
  122.             /// <summary>
  123.             /// 创建表
  124.             /// </summary>
  125.             CreateTable,
  126.             /// <summary>
  127.             /// 删除表
  128.             /// </summary>
  129.             DropTable

  130.         }
  131.     }
  132. }
Copy code
The "Microsoft. ACE.oledb.12.0" provides a workaround
http://www.itsvse.com/thread-2293-1-1.html
(Source: Wuhan Software Engineering Vocational College)
This error may be reported.





Previous:The "Microsoft. ACE.oledb.12.0" provides a workaround
Next:Teach you to manually crack any Internet café charge, any charge! Never go out of style!
Posted on 9/23/2015 6:25:44 PM |

 Landlord| Posted on 2/26/2016 6:27:10 PM |
The method on the 1st floor is easy to hang up in the case of concurrency, and it will open and close for a while. Not recommended, use my method below!


 Landlord| Posted on 3/30/2016 4:07:28 PM |
Xiao Zhaozha Posted on 2016-2-26 18:27
The method on the 1st floor is easy to hang up in the case of concurrency, and it will open and close for a while. Not recommended, use my method below!

Model.AccessDB db = new Model.AccessDB(@".\AlipayDB.mdb; Jet OLEDB:Database Password=123");
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com