c#操作Access[下]

来源:互联网 发布:淘宝如何设置店铺首页 编辑:程序博客网 时间:2024/05/21 10:08

介绍之前先介绍一个结构体。因为以下函数都要用到这个结构体。

view plain
  1. //普通的节点   
  2. public struct Node  
  3. {  
  4.     private string nodeType;  
  5.     public string NodeType//表的字段名   
  6.     {  
  7.         set { nodeType = value; }  
  8.         get { return nodeType; }  
  9.     }  
  10.   
  11.     private string nodeValue;  
  12.     public string NodeValue//具体的值   
  13.     {  
  14.         set { nodeValue = value; }  
  15.         get { return nodeValue; }  
  16.     }  
  17. }  
  18.   
  19. //照片节点   
  20. public struct PictureNode  
  21. {  
  22.     private string nodeType;  
  23.     public string NodeType//照片的列名   
  24.     {  
  25.         set { nodeType = value; }  
  26.         get { return nodeType; }  
  27.     }  
  28.   
  29.     private byte[] nodeValue;  
  30.     public byte[] NodeValue//照片的值,注意类型   
  31.     {  
  32.         set { nodeValue = value; }  
  33.         get { return nodeValue; }  
  34.     }  
  35. }  

具体就用不着多加描述了吧!继续看问题点。

1.向table中插入数据(按行插入,如果需要插入多条请自己组织这个函数就ok了),其中的 insertArray存储的是一系列Node,pictureNode是PictureNode。

view plain
  1. //插入数据   
  2.   public static bool InsertRow( string mdbPath, string tableName, ArrayList insertArray,   
  3.        PictureNode pictureNode, ref string errinfo)  
  4.   {  
  5.       try  
  6.       {  
  7.           //1、建立连接   
  8.           string strConn  
  9.               = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mdbPath + ";Jet OLEDB:Database Password=haoren";  
  10.           OleDbConnection odcConnection = new OleDbConnection(strConn);  
  11.           //2、打开连接   
  12.           odcConnection.Open();  
  13.   
  14.           string str_col = "";  
  15.           int size_col = insertArray.Count;  
  16.           for (int i = 0; i < size_col; i++)  
  17.           {  
  18.               Node vipNode = new Node();  
  19.               vipNode = (Node)insertArray[i];  
  20.               str_col += vipNode.NodeType + ",";  
  21.           }  
  22.           str_col = str_col.TrimEnd(',');  
  23.   
  24.   
  25.           int size_row = insertArray.Count;  
  26.           string str_row = "";  
  27.           for (int i = 0; i < size_row; i++)  
  28.           {  
  29.               Node vipNode = new Node();  
  30.               vipNode = (Node)insertArray[i];  
  31.               string v = vipNode.NodeValue.ToString();  
  32.               v = DealString(v);  
  33.               if (v == "")  
  34.               {  
  35.                   str_row += "null" + ',';  
  36.               }  
  37.               else  
  38.               {  
  39.                   str_row += "'" + v + "'" + ',';  
  40.               }  
  41.           }  
  42.           str_row = str_row.TrimEnd(',');  
  43.           if (pictureNode != null && pictureNode.NodeValue != null)  
  44.           {  
  45.               str_col += ',' + pictureNode.NodeType;  
  46.               str_row += ",@Image";  
  47.           }  
  48.           string sql = "insert into " + tableName + @" (" + str_col + ") values" + @"(" + str_row + ")";  
  49.           OleDbCommand odCommand = new OleDbCommand(sql, odcConnection);  
  50.           if (pictureNode != null && pictureNode.NodeValue != null)  
  51.           {  
  52.               odCommand.Parameters.Add("@Image", OleDbType.VarBinary, pictureNode.NodeValue.Length).Value = pictureNode.NodeValue;  
  53.           }  
  54.           odCommand.ExecuteNonQuery();  
  55.           odcConnection.Close();  
  56.           return true;  
  57.       }  
  58.       catch (Exception err)  
  59.       {  
  60.           errinfo =  err.Message;  
  61.           return false;  
  62.       }  
  63.   }  

2.更新一行的数据(与插入类似)

view plain
  1. //更新一行数据   
  2.  public static bool UpdateRow(string mdbPath, string tableName,   
  3.      Node keyNode,ArrayList insertArray,PictureNode pictureNode, ref string errinfo)  
  4.  {  
  5.      try  
  6.      {  
  7.          //1、建立连接   
  8.          string strConn  
  9.              = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mdbPath + ";Jet OLEDB:Database Password=haoren";  
  10.          OleDbConnection odcConnection = new OleDbConnection(strConn);  
  11.          //2、打开连接   
  12.          odcConnection.Open();  
  13.   
  14.          int size = insertArray.Count;  
  15.          string str = "";  
  16.          for (int i = 0; i < size; i++)  
  17.          {  
  18.              Node node = new Node();  
  19.              node = (Node)insertArray[i];  
  20.              string v = node.NodeValue.ToString();  
  21.              v = DealString(v);  
  22.              str += node.NodeType + " = ";  
  23.              if (v == "")  
  24.              {  
  25.                  str += "null" + ',';  
  26.              }  
  27.              else  
  28.              {  
  29.                  str += "'" + v + "'" + ',';  
  30.              }  
  31.                
  32.          }  
  33.          str = str.TrimEnd(',');  
  34.          if (pictureNode.NodeValue != null)  
  35.          {  
  36.              str += ',' + pictureNode.NodeType;  
  37.              str += " = @Image";  
  38.          }  
  39.          string sql = "update " + tableName + " set " + str +  
  40.              " where " + keyNode.NodeType + " = " + "'" + keyNode.NodeValue + "'";  
  41.          OleDbCommand odCommand = new OleDbCommand(sql, odcConnection);  
  42.          if (pictureNode.NodeValue != null)  
  43.          {  
  44.              odCommand.Parameters.Add("@Image", OleDbType.VarBinary, pictureNode.NodeValue.Length).Value = pictureNode.NodeValue;  
  45.          }  
  46.          odCommand.ExecuteNonQuery();  
  47.          odcConnection.Close();  
  48.          return true;  
  49.      }  
  50.      catch (Exception err)  
  51.      {  
  52.          errinfo = err.Message;  
  53.          return false;  
  54.      }  
  55.  }  

3.  插入图片数据.

view plain
  1. //插入图片数据   
  2.  public static bool InsertPictureToMDB(string mdbPath,  string tableName,Node keyNode,  
  3.      PictureNode pictureNode,ref string errinfo)  
  4.  {  
  5.      try  
  6.      {  
  7.          //1、建立连接   
  8.          string strConn  
  9.              = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mdbPath + ";Jet OLEDB:Database Password=haoren";  
  10.          OleDbConnection odcConnection = new OleDbConnection(strConn);  
  11.          //2、打开连接   
  12.          odcConnection.Open();  
  13.          string sql = @"update " + tableName + " set " + pictureNode.NodeType + "="  
  14.              + "@Image where " + keyNode.NodeType + "=" + "'"+keyNode.NodeValue+"'";  
  15.          OleDbCommand comm = new OleDbCommand(sql, odcConnection);  
  16.          byte[] pic = pictureNode.NodeValue;  
  17.          comm.Parameters.Add("@Image", OleDbType.VarBinary, pic.Length).Value = pic;  
  18.          comm.ExecuteNonQuery();  
  19.          odcConnection.Close();  
  20.          return true;  
  21.      }  
  22.      catch (Exception err)  
  23.      {  
  24.          errinfo = err.Message;  
  25.          return false;  
  26.      }  
  27.  }  

4.修改mdb的一条数据.

view plain
  1. //修改mdb的一条数据   
  2. public static bool UpdateMDBNode( string tableName, Node keyNode,  
  3.     Node saveNode, ref string errinfo)  
  4. {  
  5.     try  
  6.     {  
  7.         //1、建立连接   
  8.         string strConn  
  9.             = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mdbPath + ";Jet OLEDB:Database Password=haoren";  
  10.         OleDbConnection odcConnection = new OleDbConnection(strConn);  
  11.         //2、打开连接   
  12.         odcConnection.Open();  
  13.           
  14.         string sql = @"update "+ tableName+" set " + saveNode.NodeType + " = '" + saveNode.NodeValue+  
  15.             "' where " + keyNode.NodeType + " = " + "'" + keyNode.NodeValue + "'";  
  16.         OleDbCommand comm = new OleDbCommand(sql, odcConnection);  
  17.         comm.ExecuteNonQuery();  
  18.         odcConnection.Close();  
  19.         return true;  
  20.     }  
  21.     catch (Exception err)  
  22.     {  
  23.         errinfo = err.Message;  
  24.         return false;  
  25.     }  
  26. }  

5.从mdb中获得照片

view plain
  1. //从mdb中获得照片   
  2.     public static byte[] GetImageFromMDB( string tableName, Node keyNode)  
  3.     {  
  4.         try  
  5.         {  
  6.             string sql = "Select 照片 From " + tableName +  
  7.                 " member Where " + keyNode.NodeType + " = "  
  8.                 + "'" + keyNode.NodeValue + "'";  
  9.             string strConn  
  10.                 = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mdbPath + ";Jet OLEDB:Database Password=haoren";  
  11.             OleDbConnection odcConnection = new OleDbConnection(strConn);  
  12.             //2、打开连接   
  13.             odcConnection.Open();  
  14.             OleDbCommand comm = new OleDbCommand(sql, odcConnection);  
  15.             OleDbDataReader sdr = comm.ExecuteReader();  
  16.             sdr.Read();  
  17.   
  18.             byte[] pic = (byte[])sdr[0];  
  19.             sdr.Close();  
  20.             odcConnection.Close();  
  21.             return pic;  
  22.         }  
  23.         catch   
  24.         {  
  25.             return null;  
  26.         }  
  27.     }  

到此以上描述问题,全部解决。这些程序代码都在net2005下测试通过。希望能给大家带来一些帮助。

原创粉丝点击