介绍之前先介绍一个结构体。因为以下函数都要用到这个结构体。
- //普通的节点
- public struct Node
- {
- private string nodeType;
- public string NodeType//表的字段名
- {
- set { nodeType = value; }
- get { return nodeType; }
- }
-
- private string nodeValue;
- public string NodeValue//具体的值
- {
- set { nodeValue = value; }
- get { return nodeValue; }
- }
- }
-
- //照片节点
- public struct PictureNode
- {
- private string nodeType;
- public string NodeType//照片的列名
- {
- set { nodeType = value; }
- get { return nodeType; }
- }
-
- private byte[] nodeValue;
- public byte[] NodeValue//照片的值,注意类型
- {
- set { nodeValue = value; }
- get { return nodeValue; }
- }
- }
具体就用不着多加描述了吧!继续看问题点。
1.向table中插入数据(按行插入,如果需要插入多条请自己组织这个函数就ok了),其中的 insertArray存储的是一系列Node,pictureNode是PictureNode。
- //插入数据
- public static bool InsertRow( string mdbPath, string tableName, ArrayList insertArray,
- PictureNode pictureNode, ref string errinfo)
- {
- try
- {
- //1、建立连接
- string strConn
- = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mdbPath + ";Jet OLEDB:Database Password=haoren";
- OleDbConnection odcConnection = new OleDbConnection(strConn);
- //2、打开连接
- odcConnection.Open();
-
- string str_col = "";
- int size_col = insertArray.Count;
- for (int i = 0; i < size_col; i++)
- {
- Node vipNode = new Node();
- vipNode = (Node)insertArray[i];
- str_col += vipNode.NodeType + ",";
- }
- str_col = str_col.TrimEnd(',');
-
-
- int size_row = insertArray.Count;
- string str_row = "";
- for (int i = 0; i < size_row; i++)
- {
- Node vipNode = new Node();
- vipNode = (Node)insertArray[i];
- string v = vipNode.NodeValue.ToString();
- v = DealString(v);
- if (v == "")
- {
- str_row += "null" + ',';
- }
- else
- {
- str_row += "'" + v + "'" + ',';
- }
- }
- str_row = str_row.TrimEnd(',');
- if (pictureNode != null && pictureNode.NodeValue != null)
- {
- str_col += ',' + pictureNode.NodeType;
- str_row += ",@Image";
- }
- string sql = "insert into " + tableName + @" (" + str_col + ") values" + @"(" + str_row + ")";
- OleDbCommand odCommand = new OleDbCommand(sql, odcConnection);
- if (pictureNode != null && pictureNode.NodeValue != null)
- {
- odCommand.Parameters.Add("@Image", OleDbType.VarBinary, pictureNode.NodeValue.Length).Value = pictureNode.NodeValue;
- }
- odCommand.ExecuteNonQuery();
- odcConnection.Close();
- return true;
- }
- catch (Exception err)
- {
- errinfo = err.Message;
- return false;
- }
- }
2.更新一行的数据(与插入类似)
|
请发表评论