• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

c#操作Access(下)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

 

  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。

  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.更新一行的数据(与插入类似)

 


    鲜花

    握手

    雷人

    路过

    鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
在C#中,(int),Int32.Parse()和Convert.toInt32()三种方法的区别发布时间:2022-07-10
下一篇:
C#发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap