自己寫的AccessDBHelper

最近,一个项目需要用到MDB文件,所以就自己写了一个小类库实现一些用到的功能,这不是一个万能的MDB操作功能类库,功能比我这强百倍的,网上一搜大把.但毕竟不是出自自己之手,用起来是方便,以后维护就是怕会头脑空白一片.

由于是完整代码,有点长.先介绍基本功能,以免误了各位看官的时间,要是这些功能没有值得一看的,路过路过了!

1.MDB的创建;

2.在MDB中创建表;

3.MDB表内容的增删改查;

4.文件的辅助操作类;

5.测试WEB.

 

项目的结构如下图:

image

 

测试内容如下:

image

 

详细介绍:

文件的辅助操作类

因为要用到建立文件和读取,从开始就应该想好,要有一个专门的类来处理一些与文件有关的功能,例如以下用到的功能都写入FilePathTools.cs中,这个类的主要作用有

1.给出一个文件的完整路径,得到扩展名;

我为了建立一个MDB文件,一定会输入一个完整的文件路径来创建文件,但是输入的文件是不是MDB文件,就要取出扩展名来判断了,所以要有一个取扩展名的方法.

2.取出文件名简名和全名;

有时候免不了要用到文件名,单单是文件名,不包含路径,这里就有取出带扩展名和不带扩展名的两个方法.

3.取出文件的所在路径;

这里也用到取出文件的路径的方法.

4.判断文件是否存在;

要创建文件,先要看看你想创建的文件是否已存在了.

5.判断文件所在的路径是否存在;

你想建立文件,必需先有路径,所以要判断你的目标路径是否存在.

6.创建路径;

目标路径不存在的时候,你要先创建路径.

以下是类文件的C#代码

FilePathTools.cs

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Text;
   5:  using System.IO;
   6:   
   7:  namespace Core
   8:  {
   9:      public static class FilePathTools
  10:      {
  11:          /// <summary>
  12:          /// 取出文件擴展名
  13:          /// </summary>
  14:          /// <param name="FileName">文件名</param>
  15:          /// <returns></returns>
  16:          public static string GetExtFileTypeName(string FileName)
  17:          {
  18:              string sFile = FileName;
  19:              sFile = sFile.Substring(sFile.LastIndexOf("\\") + 1);
  20:              sFile = sFile.Substring(sFile.LastIndexOf(".")).ToLower();
  21:              return sFile;
  22:          }
  23:   
  24:          /// <summary>
  25:          /// 取出文件名,包括擴展名的完整名稱
  26:          /// </summary>
  27:          /// <param name="FileName">文件名</param>
  28:          /// <returns></returns>
  29:          public static string GetFileFullName(string FileName)
  30:          {
  31:              string sFile = FileName;
  32:              sFile = sFile.Substring(sFile.LastIndexOf("\\") + 1);
  33:              //sFile = sFile.Substring(sFile.LastIndexOf(".")).ToLower();
  34:              return sFile;
  35:          }
  36:   
  37:          /// <summary>
  38:          /// 取出文件名,不包括擴展名
  39:          /// </summary>
  40:          /// <param name="FileName">文件名</param>
  41:          /// <returns></returns>
  42:          public static string GetFileName(string FileName)
  43:          {
  44:              string sFile = FileName;
  45:              sFile = sFile.Substring(sFile.LastIndexOf("\\") + 1);
  46:              sFile = sFile.Substring(0,sFile.LastIndexOf(".")).ToLower();
  47:              return sFile;
  48:          }
  49:   
  50:          /// <summary>
  51:          /// 取出文件所在的路徑
  52:          /// </summary>
  53:          /// <param name="FileName">文件名</param>
  54:          /// <returns></returns>
  55:          public static string GetFilePath(string FileName)
  56:          {
  57:              string sFile = FileName;
  58:              sFile = sFile.Substring(0,sFile.LastIndexOf("\\") );            
  59:              return sFile;
  60:          }
  61:   
  62:          /// <summary>
  63:          /// 文件所在的路徑是否存在
  64:          /// </summary>
  65:          /// <param name="FileName">文件名</param>
  66:          /// <returns></returns>
  67:          public static bool HasFilePath(string FileName)
  68:          {
  69:              return Directory.Exists(GetFilePath(FileName));
  70:          }
  71:   
  72:          /// <summary>
  73:          /// 路徑創建
  74:          /// </summary>
  75:          /// <param name="FileName">文件名</param>
  76:          /// <returns></returns>
  77:          public static bool CreatePath(string path)
  78:          {
  79:              try
  80:              {
  81:                  if (!HasFilePath(GetFilePath(path)))
  82:                  {
  83:                      CreatePath(GetFilePath(path));
  84:                  }
  85:                  Directory.CreateDirectory(path);
  86:                  return true;
  87:              }
  88:              catch(Exception ee)
  89:              {
  90:                  throw new Exception(ee.Message);
  91:              }
  92:          }
  93:   
  94:          /// <summary>
  95:          /// 文件是否存在
  96:          /// </summary>
  97:          /// <param name="FileName">文件名</param>
  98:          /// <returns></returns>
  99:          public static bool HasFile(string FileName)
 100:          {
 101:              return File.Exists(FileName);
 102:          }
 103:      }
 104:  }

 

 

创建MDB文件

创建MDB文件只有一个方法,但是我还是用一个单独的类来写,谁又知道那天会不会要加一些功能呢?

给一个完整的文件路径,做一些判断,你要建立MDB文件,那就要看输入的是不是MDB文件.你还要看目标文件路径是不是已存在,要是没有,那就要先建立路径,再创建文件.

要创建MDB文件,你用ADO.NET是做不来的,需要用到ADOX,引用ADOX的方法是在项目中加入引用,选COM,再找到

Microsoft ADO Ext.x.x for DDL and Security,通常使用2.7或2.8版本,但是现在已有6.0的了,我也是使用6.0版本.用法请参考代码清单.

image

创建MDB代码清单如下:

CreateMDB.cs

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Text;
   5:  using ADOX;
   6:  using System.IO;
   7:   
   8:  namespace Core.AccessDBHelper
   9:  {
  10:     public  class CreateMDB:ICreateDB
  11:      {
  12:          #region ICreateDB Members
  13:   
  14:         public  bool CreateAccessFile(string filename)
  15:         {
  16:             try
  17:             {
  18:                 if (FilePathTools.GetExtFileTypeName(filename).ToLower() != ".mdb")
  19:                 {
  20:                     throw new Exception("創建MDB文件時,需要一個.mdb擴展名的文件名稱.");
  21:                 }
  22:                 if (!FilePathTools.HasFilePath(FilePathTools.GetFilePath(filename)))
  23:                 {
  24:                     if (FilePathTools.CreatePath(FilePathTools.GetFilePath(filename)))
  25:                     {
  26:                         throw new Exception("創建MDB文件時,需要一個有效的路徑.");
  27:                     }
  28:                 }
  29:                 ADOX.CatalogClass cat = new CatalogClass();
  30:                 string str = "provider=Microsoft.Jet.OleDb.4.0;Data Source=" +  filename + ";";
  31:                 cat.Create(str);
  32:                 cat = null;
  33:                 return true;
  34:             }
  35:             catch (Exception ee)
  36:             {
  37:                 throw new Exception(ee.Message);
  38:             }
  39:         }
  40:   
  41:          #endregion
  42:      }
  43:  }

 

 

在MDB文件中创建表

创建表的功能也使用单独的类来处理,直接参看以下代码:

CreateTable.cs

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Text;
   5:  using System.Data;
   6:  using System.Data.OleDb;
   7:   
   8:  namespace Core.AccessDBHelper
   9:  {
  10:      public static class CreateTable
  11:      {
  12:          public static bool Create(string mdbFile, string strCreateTableSql)
  13:          {
  14:              OleDbConnection DBConnection;
  15:              OleDbCommand DBCommand = new OleDbCommand();   //   数据库命令 
  16:              #region   打开数据库
  17:              string strConnect = @"provider=Microsoft.Jet.OleDb.4.0;Data Source=" + mdbFile + "; ";
  18:              DBConnection = new OleDbConnection(strConnect);
  19:              DBCommand.Connection = DBConnection;
  20:              try
  21:              {
  22:                  if (DBCommand.Connection.State != ConnectionState.Open)
  23:                  {
  24:                      DBCommand.Connection.Open();//打开数据库连接 
  25:                  }
  26:              }
  27:              catch (Exception ex)
  28:              {
  29:                  throw new Exception(ex.Message);
  30:              }
  31:              #endregion
  32:              #region   建表
  33:              //string strSql = "CREATE TABLE "+tableName+"(ID     varchar(2),   name     varchar(50)   NOT   NULL) "; 
  34:              DBCommand.CommandText = strCreateTableSql;
  35:   
  36:              try
  37:              {
  38:                  DBCommand.ExecuteNonQuery();
  39:              }
  40:              catch (Exception ex)
  41:              {
  42:                  throw new Exception(ex.Message);
  43:              }
  44:              #endregion
  45:              DBCommand.Connection.Close();//   关闭数据库连接 
  46:              return true;
  47:          }
  48:      }
  49:  }

 

数据的增删改查

功能与SQLHelper功能相似,不多说.以下是代码,有注释

MDBHelper.cs

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Text;
   5:  using System.Data;
   6:  using System.Configuration;
   7:  using System.Data.OleDb;
   8:  using System.Collections;
   9:   
  10:  namespace Core.AccessDBHelper
  11:  {
  12:      public static class MDBHelper
  13:      {
  14:          //数据库连接字符串       
  15:          public static string GetMDBConnectionString(string mdbFile)
  16:          {
  17:              return @"provider=Microsoft.Jet.OleDb.4.0;Data Source=" + mdbFile + "; ";
  18:          }
  19:          //数据库连接字符串       
  20:          public static OleDbConnection GetMDBConnection(string mdbFile)
  21:          {
  22:              return new OleDbConnection(GetMDBConnectionString(mdbFile));
  23:          }
  24:   
  25:          // 用于缓存参数的HASH表
  26:          private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());
  27:          /// <summary>
  28:          /// 给定连接的数据库用假设参数执行一个sql命令(不返回数据集)
  29:          /// </summary>
  30:          /// <param name="connectionString">一个有效的连接字符串</param>
  31:          /// <param name="commandText">存储过程名称或者sql命令语句</param>
  32:          /// <param name="commandParameters">执行命令所用参数的集合</param>
  33:          /// <returns>执行命令所影响的行数</returns>
  34:          public static int ExecuteNonQuery(string connectionString, string cmdText, params OleDbParameter[] commandParameters)
  35:          {
  36:   
  37:              OleDbCommand cmd = new OleDbCommand();
  38:              using (OleDbConnection conn = new OleDbConnection(connectionString))
  39:              {
  40:                  try
  41:                  {
  42:                      PrepareCommand(cmd, conn, null, cmdText, commandParameters);
  43:                      int val = cmd.ExecuteNonQuery();
  44:                      cmd.Parameters.Clear();
  45:                      return val;
  46:                  }
  47:                  catch (Exception ee)
  48:                  {
  49:                      throw ee;
  50:                  }
  51:                  finally
  52:                  {
  53:                      if (conn.State != ConnectionState.Closed)
  54:                      {
  55:                          conn.Close();
  56:                      }
  57:                  }
  58:              }
  59:   
  60:   
  61:          }
  62:          /// <summary>
  63:          /// 用现有的数据库连接执行一个sql命令(不返回数据集)
  64:          /// </summary>
  65:          /// <remarks>
  66:          ///举例: 
  67:          /// int result = ExecuteNonQuery(connString, "PublishOrders", new OleDbParameter("@prodid", 24));
  68:          /// </remarks>
  69:          /// <param name="conn">一个现有的数据库连接</param>
  70:          /// <param name="commandText">存储过程名称或者sql命令语句</param>
  71:          /// <param name="commandParameters">执行命令所用参数的集合</param>
  72:          /// <returns>执行命令所影响的行数</returns>
  73:          public static int ExecuteNonQuery(OleDbConnection conn, string cmdText, params OleDbParameter[] commandParameters)
  74:          {
  75:              try
  76:              {
  77:                  OleDbCommand cmd = new OleDbCommand();
  78:                  PrepareCommand(cmd, conn, null, cmdText, commandParameters);
  79:                  int val = cmd.ExecuteNonQuery();
  80:                  cmd.Parameters.Clear();
  81:                  return val;
  82:              }
  83:              catch (Exception ee)
  84:              {
  85:                  throw ee;
  86:              }
  87:              finally
  88:              {
  89:                  if (conn.State != ConnectionState.Closed)
  90:                  {
  91:                      conn.Close();
  92:                  }
  93:              }
  94:          }
  95:          /// <summary>
  96:          ///使用现有的SQL事务执行一个sql命令(不返回数据集)
  97:          /// </summary>
  98:          /// <remarks>
  99:          ///举例: 
 100:          /// int result = ExecuteNonQuery(trans, "PublishOrders", new OleDbParameter("@prodid", 24));
 101:          /// </remarks>
 102:          /// <param name="trans">一个现有的事务</param>
 103:          /// <param name="commandText">存储过程名称或者sql命令语句</param>
 104:          /// <param name="commandParameters">执行命令所用参数的集合</param>
 105:          /// <returns>执行命令所影响的行数</returns>
 106:          public static int ExecuteNonQuery(OleDbTransaction trans, string cmdText, params OleDbParameter[] commandParameters)
 107:          {
 108:              try
 109:              {
 110:                  OleDbCommand cmd = new OleDbCommand();
 111:                  PrepareCommand(cmd, trans.Connection, trans, cmdText, commandParameters);
 112:                  int val = cmd.ExecuteNonQuery();
 113:                  cmd.Parameters.Clear();
 114:                  return val;
 115:              }
 116:              catch (Exception ee)
 117:              {
 118:                  throw ee;
 119:              }
 120:   
 121:          }
 122:          /// <summary>
 123:          /// 用执行的数据库连接执行一个返回数据集的sql命令
 124:          /// </summary>
 125:          /// <remarks>
 126:          /// 举例: 
 127:          /// OleDbDataReader r = ExecuteReader(connString, "PublishOrders", new OleDbParameter("@prodid", 24));
 128:          /// </remarks>
 129:          /// <param name="connectionString">一个有效的连接字符串</param>
 130:          /// <param name="commandText">存储过程名称或者sql命令语句</param>
 131:          /// <param name="commandParameters">执行命令所用参数的集合</param>
 132:          /// <returns>包含结果的读取器</returns>
 133:          public static OleDbDataReader ExecuteReader(string connectionString, string cmdText, params OleDbParameter[] commandParameters)
 134:          {
 135:              //创建一个SqlCommand对象
 136:              OleDbCommand cmd = new OleDbCommand();
 137:              //创建一个SqlConnection对象
 138:              OleDbConnection conn = new OleDbConnection(connectionString);
 139:              //在这里我们用一个try/catch结构执行sql文本命令/存储过程,因为如果这个方法产生一个异常我们要关闭连接,因为没有读取器存在,
 140:              //因此commandBehaviour.CloseConnection 就不会执行
 141:              try
 142:              {
 143:                  //调用 PrepareCommand 方法,对 SqlCommand 对象设置参数
 144:                  PrepareCommand(cmd, conn, null, cmdText, commandParameters);
 145:                  //调用 SqlCommand 的 ExecuteReader 方法
 146:                  OleDbDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
 147:                  //清除参数
 148:                  cmd.Parameters.Clear();
 149:                  return reader;
 150:              }
 151:              catch (Exception ee)
 152:              {
 153:                  throw ee;
 154:              }
 155:              finally
 156:              {
 157:                  if (conn.State != ConnectionState.Closed)
 158:                  {
 159:                      conn.Close();
 160:                  }
 161:              }
 162:          }
 163:          /// <summary>
 164:          /// 返回一个DataSet数据集
 165:          /// </summary>
 166:          /// <param name="connectionString">一个有效的连接字符串</param>
 167:          /// <param name="cmdText">存储过程名称或者sql命令语句</param>
 168:          /// <param name="commandParameters">执行命令所用参数的集合</param>
 169:          /// <returns>包含结果的数据集</returns>
 170:          public static DataSet ExecuteDataSet(string connectionString, string cmdText, params OleDbParameter[] commandParameters)
 171:          {
 172:              //创建一个SqlCommand对象,并对其进行初始化
 173:              OleDbCommand cmd = new OleDbCommand();
 174:              using (OleDbConnection conn = new OleDbConnection(connectionString))
 175:              {
 176:                  PrepareCommand(cmd, conn, null, cmdText, commandParameters);
 177:                  //创建SqlDataAdapter对象以及DataSet
 178:                  OleDbDataAdapter da = new OleDbDataAdapter(cmd);
 179:                  DataSet ds = new DataSet();
 180:                  try
 181:                  {
 182:                      //填充ds
 183:                      da.Fill(ds);
 184:                      // 清除cmd的参数集合 
 185:                      cmd.Parameters.Clear();
 186:                      //返回ds
 187:                      return ds;
 188:                  }
 189:                  catch (Exception ee)
 190:                  {
 191:                      throw ee;
 192:                  }
 193:                  finally
 194:                  {
 195:                      if (conn.State != ConnectionState.Closed)
 196:                      {
 197:                          conn.Close();
 198:                      }
 199:                  }
 200:              }
 201:          }
 202:          /// <summary>
 203:          /// 用指定的数据库连接字符串执行一个命令并返回一个数据集的第一列
 204:          /// </summary>
 205:          /// <remarks>
 206:          ///例如: 
 207:          /// Object obj = ExecuteScalar(connString, "PublishOrders", new OleDbParameter("@prodid", 24));
 208:          /// </remarks>
 209:          ///<param name="connectionString">一个有效的连接字符串</param>
 210:          /// <param name="commandText">存储过程名称或者sql命令语句</param>
 211:          /// <param name="commandParameters">执行命令所用参数的集合</param>
 212:          /// <returns>用 Convert.To{Type}把类型转换为想要的 </returns>
 213:          public static object ExecuteScalar(string connectionString, string cmdText, params OleDbParameter[] commandParameters)
 214:          {
 215:              OleDbCommand cmd = new OleDbCommand();
 216:              using (OleDbConnection conn = new OleDbConnection(connectionString))
 217:              {
 218:                  try
 219:                  {
 220:                      PrepareCommand(cmd, conn, null, cmdText, commandParameters);
 221:                      object val = cmd.ExecuteScalar();
 222:                      cmd.Parameters.Clear();
 223:                      return val;
 224:                  }
 225:                  catch (Exception ee)
 226:                  {
 227:                      throw ee;
 228:                  }
 229:                  finally
 230:                  {
 231:                      if (conn.State != ConnectionState.Closed)
 232:                      {
 233:                          conn.Close();
 234:                      }
 235:                  }
 236:              }
 237:          }
 238:          /// <summary>
 239:          /// 用指定的数据库连接执行一个命令并返回一个数据集的第一列
 240:          /// </summary>
 241:          /// <remarks>
 242:          /// 例如: 
 243:          /// Object obj = ExecuteScalar(connString, "PublishOrders", new OleDbParameter("@prodid", 24));
 244:          /// </remarks>
 245:          /// <param name="conn">一个存在的数据库连接</param>
 246:          /// <param name="commandText">存储过程名称或者sql命令语句</param>
 247:          /// <param name="commandParameters">执行命令所用参数的集合</param>
 248:          /// <returns>用 Convert.To{Type}把类型转换为想要的 </returns>
 249:          public static object ExecuteScalar(OleDbConnection conn, string cmdText, params OleDbParameter[] commandParameters)
 250:          {
 251:              try
 252:              {
 253:                  OleDbCommand cmd = new OleDbCommand();
 254:                  PrepareCommand(cmd, conn, null, cmdText, commandParameters);
 255:                  object val = cmd.ExecuteScalar();
 256:                  cmd.Parameters.Clear();
 257:                  return val;
 258:              }
 259:              catch (Exception ee)
 260:              {
 261:                  throw ee;
 262:              }
 263:              finally
 264:              {
 265:                  if (conn.State != ConnectionState.Closed)
 266:                  {
 267:                      conn.Close();
 268:                  }
 269:              }
 270:          }
 271:          /// <summary>
 272:          /// 将参数集合添加到缓存
 273:          /// </summary>
 274:          /// <param name="cacheKey">添加到缓存的变量</param>
 275:          /// <param name="cmdParms">一个将要添加到缓存的sql参数集合</param>
 276:          public static void CacheParameters(string cacheKey, params OleDbParameter[] commandParameters)
 277:          {
 278:              try
 279:              {
 280:                  parmCache[cacheKey] = commandParameters;
 281:              }
 282:              catch (Exception ee)
 283:              {
 284:                  throw ee;
 285:              }
 286:          }
 287:          /// <summary>
 288:          /// 找回缓存参数集合
 289:          /// </summary>
 290:          /// <param name="cacheKey">用于找回参数的关键字</param>
 291:          /// <returns>缓存的参数集合</returns>
 292:          public static OleDbParameter[] GetCachedParameters(string cacheKey)
 293:          {
 294:              try
 295:              {
 296:                  OleDbParameter[] cachedParms = (OleDbParameter[])parmCache[cacheKey];
 297:                  if (cachedParms == null)
 298:                      return null;
 299:                  OleDbParameter[] clonedParms = new OleDbParameter[cachedParms.Length];
 300:                  for (int i = 0, j = cachedParms.Length; i < j; i++)
 301:                      clonedParms = (OleDbParameter[])((ICloneable)cachedParms).Clone();
 302:                  return clonedParms;
 303:              }
 304:              catch (Exception ee)
 305:              {
 306:                  throw ee;
 307:              }
 308:   
 309:          }
 310:          /// <summary>
 311:          /// 准备执行一个命令
 312:          /// </summary>
 313:          /// <param name="cmd">sql命令</param>
 314:          /// <param name="conn">Sql连接</param>
 315:          /// <param name="trans">Sql事务</param>
 316:          /// <param name="cmdText">命令文本,例如:Select * from Products</param>
 317:          /// <param name="cmdParms">执行命令的参数</param>
 318:          private static void PrepareCommand(OleDbCommand cmd, OleDbConnection conn, OleDbTransaction trans, string cmdText, OleDbParameter[] cmdParms)
 319:          {
 320:              try
 321:              {
 322:                  //判断连接的状态。如果是关闭状态,则打开
 323:                  if (conn.State != ConnectionState.Open)
 324:                      conn.Open();
 325:                  //cmd属性赋值
 326:                  cmd.Connection = conn;
 327:                  cmd.CommandText = cmdText;
 328:                  //是否需要用到事务处理
 329:                  if (trans != null)
 330:                      cmd.Transaction = trans;
 331:                  cmd.CommandType = CommandType.Text;
 332:                  //添加cmd需要的存储过程参数
 333:                  if (cmdParms != null)
 334:                  {
 335:                      foreach (OleDbParameter parm in cmdParms)
 336:                          cmd.Parameters.Add(parm);
 337:                  }
 338:              }
 339:              catch (Exception ee)
 340:              {
 341:                  throw ee;
 342:              }
 343:   
 344:          }
 345:      }
 346:  }

 

测试代码

用一个页面来看看功能的工作是否正常,对以上的功能进行测试

测试取扩展名、取文件名、文件路径、检测文件和路径、建立MDB、建立表、写入数据等等功能。

Default.aspx

   1:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   2:   
   3:  <html xmlns="http://www.w3.org/1999/xhtml">
   4:  <head runat="server">
   5:      <title></title>
   6:  </head>
   7:  <body>
   8:      <form id="form1" runat="server">
   9:      <div>
  10:          <asp:TextBox ID="TextBox1" runat="server" Width="383px"></asp:TextBox>
  11:          <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
  12:          <asp:GridView ID="GridView1" runat="server">
  13:          </asp:GridView>
  14:      </div>
  15:      </form>
  16:  </body>
  17:  </html>

 

Default.aspx.cs

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Web;
   5:  using System.Web.UI;
   6:  using System.Web.UI.WebControls;
   7:  using Core;
   8:  using Core.AccessDBHelper;
   9:   
  10:  public partial class _Default : System.Web.UI.Page
  11:  {
  12:      protected void Page_Load(object sender, EventArgs e)
  13:      {
  14:   
  15:      }
  16:      protected void Button1_Click(object sender, EventArgs e)
  17:      {
  18:          try
  19:          {
  20:              Response.Write("擴展名:" + FilePathTools.GetExtFileTypeName(TextBox1.Text));
  21:              Response.Write("<br/>");
  22:          }
  23:          catch (Exception ee)
  24:          {
  25:              Response.Write(ee.Message);
  26:              Response.Write("<br/>");
  27:          }
  28:          try
  29:          {
  30:              Response.Write("文件名:" + FilePathTools.GetFileName(TextBox1.Text));
  31:              Response.Write("<br/>");
  32:          }
  33:          catch (Exception ee)
  34:          {
  35:              Response.Write(ee.Message);
  36:              Response.Write("<br/>");
  37:          }
  38:          try
  39:          {
  40:              Response.Write("文件全名:" + FilePathTools.GetFileFullName(TextBox1.Text));
  41:              Response.Write("<br/>");
  42:          }
  43:          catch (Exception ee)
  44:          {
  45:              Response.Write(ee.Message);
  46:              Response.Write("<br/>");
  47:          }
  48:   
  49:          try
  50:          {
  51:              Response.Write("文件路徑:" + FilePathTools.GetFilePath(TextBox1.Text));
  52:              Response.Write("<br/>");
  53:          }
  54:          catch (Exception ee)
  55:          {
  56:              Response.Write(ee.Message);
  57:              Response.Write("<br/>");
  58:          }
  59:          try
  60:          {
  61:              Response.Write("文件是否存在:" + FilePathTools.HasFile(TextBox1.Text).ToString());
  62:              Response.Write("<br/>");
  63:          }
  64:          catch (Exception ee)
  65:          {
  66:              Response.Write(ee.Message);
  67:              Response.Write("<br/>");
  68:          }
  69:          try
  70:          {
  71:              Response.Write("文件路徑是否存在:" + FilePathTools.HasFilePath(TextBox1.Text).ToString());
  72:              Response.Write("<br/>");
  73:          }
  74:          catch (Exception ee)
  75:          {
  76:              Response.Write(ee.Message);
  77:              Response.Write("<br/>");
  78:          }
  79:          try
  80:          {
  81:              Response.Write("創建文件夾:" + FilePathTools.CreatePath(FilePathTools.GetFilePath(TextBox1.Text)).ToString());
  82:              Response.Write("<br/>");
  83:          }
  84:          catch (Exception ee)
  85:          {
  86:              Response.Write(ee.Message);
  87:              Response.Write("<br/>");
  88:          }
  89:          try
  90:          {
  91:              Response.Write("創建MDB文件:" + new CreateMDB().CreateAccessFile(TextBox1.Text));
  92:              Response.Write("<br/>");
  93:          }
  94:          catch (Exception ee)
  95:          {
  96:              Response.Write(ee.Message);
  97:              Response.Write("<br/>");
  98:          }
  99:          try
 100:          {
 101:              Response.Write("創建表:" + CreateTable.Create(TextBox1.Text, "CREATE TABLE test(ID varchar(2),name nvarchar(50) NOT NULL)").ToString());
 102:              Response.Write("<br/>");
 103:          }
 104:          catch (Exception ee)
 105:          {
 106:              Response.Write(ee.Message);
 107:              Response.Write("<br/>");
 108:          }
 109:          try
 110:          {
 111:              Response.Write("寫入記錄:" + MDBHelper.ExecuteNonQuery(MDBHelper.GetMDBConnection(TextBox1.Text),"insert into test(ID,name) values(1,'謝堂文');",null).ToString());
 112:              Response.Write("<br/>");
 113:          }
 114:          catch (Exception ee)
 115:          {
 116:              Response.Write(ee.Message);
 117:              Response.Write("<br/>");
 118:          }
 119:          try
 120:          {
 121:              GridView1.DataSource = MDBHelper.ExecuteDataSet(MDBHelper.GetMDBConnectionString(TextBox1.Text), "select * from test;", null);
 122:              GridView1.DataBind();
 123:          }
 124:          catch
 125:          {
 126:          }
 127:      }
 128:  }

 

源代码可从这里下载

生成的DLL文件从这里下载

 

本文原创,转载请注明出处.

你可能感兴趣的:(Access)