c#操作Access[下]

来源:互联网 发布:linux 内核编译安装 编辑:程序博客网 时间:2024/06/14 21:38

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

  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.         private string nodeValue;
  11.         public string NodeValue//具体的值
  12.         {
  13.             set { nodeValue = value; }
  14.             get { return nodeValue; }
  15.         }
  16.     }
  17.     //照片节点
  18.     public struct PictureNode
  19.     {
  20.         private string nodeType;
  21.         public string NodeType//照片的列名
  22.         {
  23.             set { nodeType = value; }
  24.             get { return nodeType; }
  25.         }
  26.         private byte[] nodeValue;
  27.         public byte[] NodeValue//照片的值,注意类型
  28.         {
  29.             set { nodeValue = value; }
  30.             get { return nodeValue; }
  31.         }
  32.     }

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

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 
  10. Password=haoren";
  11.                 OleDbConnection odcConnection = new OleDbConnection(strConn);
  12.                 //2、打开连接
  13.                 odcConnection.Open();
  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.                 int size_row = insertArray.Count;
  24.                 string str_row = "";
  25.                 for (int i = 0; i < size_row; i++)
  26.                 {
  27.                     Node vipNode = new Node();
  28.                     vipNode = (Node)insertArray[i];
  29.                     string v = vipNode.NodeValue.ToString();
  30.                     v = DealString(v);
  31.                     if (v == "")
  32.                     {
  33.                         str_row += "null" + ',';
  34.                     }
  35.                     else
  36.                     {
  37.                         str_row += "'" + v + "'" + ',';
  38.                     }
  39.                 }
  40.                 str_row = str_row.TrimEnd(',');
  41.                 if (pictureNode != null && pictureNode.NodeValue != null)
  42.                 {
  43.                     str_col += ',' + pictureNode.NodeType;
  44.                     str_row += ",@Image";
  45.                 }
  46.                 string sql = "insert into " + tableName + @" (" + str_col + ") values" + @"(" + str_row + ")";
  47.                 OleDbCommand odCommand = new OleDbCommand(sql, odcConnection);
  48.                 if (pictureNode != null && pictureNode.NodeValue != null)
  49.                 {
  50.                     odCommand.Parameters.Add("@Image", OleDbType.VarBinary, 
  51. pictureNode.NodeValue.Length).Value = pictureNode.NodeValue;
  52.                 }
  53.                 odCommand.ExecuteNonQuery();
  54.                 odcConnection.Close();
  55.                 return true;
  56.             }
  57.             catch (Exception err)
  58.             {
  59.                 errinfo =  err.Message;
  60.                 return false;
  61.             }
  62.         }

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

 

  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 
  10. Password=haoren";
  11.                 OleDbConnection odcConnection = new OleDbConnection(strConn);
  12.                 //2、打开连接
  13.                 odcConnection.Open();
  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, 
  45. pictureNode.NodeValue.Length).Value = pictureNode.NodeValue;
  46.                 }
  47.                 odCommand.ExecuteNonQuery();
  48.                 odcConnection.Close();
  49.                 return true;
  50.             }
  51.             catch (Exception err)
  52.             {
  53.                 errinfo = err.Message;
  54.                 return false;
  55.             }
  56.         }

 

 

3.  插入图片数据.
  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 
  10. Password=haoren";
  11.                 OleDbConnection odcConnection = new OleDbConnection(strConn);
  12.                 //2、打开连接
  13.                 odcConnection.Open();
  14.                 string sql = @"update " + tableName + " set " + pictureNode.NodeType + "="
  15.                     + "@Image where " + keyNode.NodeType + "=" + "'"+keyNode.NodeValue+"'";
  16.                 OleDbCommand comm = new OleDbCommand(sql, odcConnection);
  17.                 byte[] pic = pictureNode.NodeValue;
  18.                 comm.Parameters.Add("@Image", OleDbType.VarBinary, pic.Length).Value = pic;
  19.                 comm.ExecuteNonQuery();
  20.                 odcConnection.Close();
  21.                 return true;
  22.             }
  23.             catch (Exception err)
  24.             {
  25.                 errinfo = err.Message;
  26.                 return false;
  27.             }
  28.         }

4.修改mdb的一条数据.

  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 
  10. Password=haoren";
  11.                 OleDbConnection odcConnection = new OleDbConnection(strConn);
  12.                 //2、打开连接
  13.                 odcConnection.Open();
  14.                 
  15.                 string sql = @"update "+ tableName+" set " + saveNode.NodeType + " = '" + saveNode.NodeValue+
  16.                     "' where " + keyNode.NodeType + " = " + "'" + keyNode.NodeValue + "'";
  17.                 OleDbCommand comm = new OleDbCommand(sql, odcConnection);
  18.                 comm.ExecuteNonQuery();
  19.                 odcConnection.Close();
  20.                 return true;
  21.             }
  22.             catch (Exception err)
  23.             {
  24.                 errinfo = err.Message;
  25.                 return false;
  26.             }
  27.         }

5.从mdb中获得照片

  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 
  11. Password=haoren";
  12.                 OleDbConnection odcConnection = new OleDbConnection(strConn);
  13.                 //2、打开连接
  14.                 odcConnection.Open();
  15.                 OleDbCommand comm = new OleDbCommand(sql, odcConnection);
  16.                 OleDbDataReader sdr = comm.ExecuteReader();
  17.                 sdr.Read();
  18.                 byte[] pic = (byte[])sdr[0];
  19.                 sdr.Close();
  20.                 odcConnection.Close();
  21.                 return pic;
  22.             }
  23.             catch 
  24.             {
  25.                 return null;
  26.             }
  27.         }

http://blog.csdn.net/gisfarmer/

原创粉丝点击