.
A struct is introduced before introduction. This struct is required for all the following functions.
// Common node <br/> public struct node <br/>{< br/> private string nodetype; <br/> Public String nodetype // field name of the table <br/>{< br/> set {nodetype = value ;}< br/> get {return nodetype ;} <br/>}</P> <p> private string nodevalue; <br/> Public String nodevalue // specific value <br/>{< br/> set {nodevalue = value ;}< br/> get {return nodevalue ;} <br/>}</P> <p> // photo node <br/> public struct picturenode <br/>{< br/> private string nodetype; <br/> Public String nodetype // name of the photo column <br/>{< br/> set {nodetype = value ;}< br/> get {return nodetype ;} <br/>}</P> <p> private byte [] nodevalue; <br/> Public byte [] nodevalue // The photo value, note type <br/>{< br/> set {nodevalue = value ;}< br/> get {return nodevalue ;}< br/>}< br/>}
I don't need to describe more about it! Continue to check the problem.
1. Insert data to the table (insert data by row. If you want to insert multiple rows, organize the function by yourself.) insertarray stores a series of nodes and picturenode is picturenode.
// Insert data <br/> Public static bool insertrow (string mdbpath, string tablename, arraylist insertarray, <br/> picturenode, ref string errinfo) <br/> {<br/> try <br/> {<br/> // 1. Establish a connection <br/> string strconn <br/> = @ "provider = microsoft. jet. oledb.4.0; Data Source = "+ mdbpath +"; Jet oledb: Database Password = haoren "; <br/> oledbconnection odcconnection = new oledbconnection (strconn ); <br/> // 2. Open Connection <br/> odcconnection. open (); </P> <p> string str_col = ""; <br/> int size_col = insertarray. count; <br/> for (INT I = 0; I <size_col; I ++) <br/> {<br/> node vipnode = new node (); <br/> vipnode = (node) insertarray [I]; <br/> str_col + = vipnode. nodetype + ","; <br/>}< br/> str_col = str_col.trimend (','); </P> <p> int size_row = insertarray. count; <br/> string str_row = ""; <br/> for (INT I = 0; I <Size_row; I ++) <br/>{< br/> node vipnode = new node (); <br/> vipnode = (node) insertarray [I]; <br/> string v = vipnode. nodevalue. tostring (); <br/> V = dealstring (V); <br/> If (V = "") <br/>{< br/> str_row + = "null" + ','; <br/>}< br/> else <br/> {<br/> str_row + = "'" + V + "'" + ','; <br/>}< br/> str_row = str_row.trimend (','); <br/> If (picturenode! = NULL & picturenode. nodevalue! = NULL) <br/>{< br/> str_col + = ',' + picturenode. nodetype; <br/> str_row + = ", @ image "; <br/>}< br/> string SQL = "insert into" + tablename + @ "(" + str_col + ") values" + @ "(" + str_row + ") "; <br/> oledbcommand odcommand = new oledbcommand (SQL, odcconnection); <br/> If (picturenode! = NULL & picturenode. nodevalue! = NULL) <br/>{< br/> odcommand. parameters. add ("@ image", oledbtype. varbinary, picturenode. nodevalue. length ). value = picturenode. nodevalue; <br/>}< br/> odcommand. executenonquery (); <br/> odcconnection. close (); <br/> return true; <br/>}< br/> catch (exception ERR) <br/>{< br/> errinfo = err. message; <br/> return false; <br/>}< br/>}
2. Update the data of a row (similar to insert)
// Update a row of Data <br/> Public static bool updaterow (string mdbpath, string tablename, <br/> node keynode, arraylist insertarray, picturenode, ref string errinfo) <br/> {<br/> try <br/> {<br/> // 1. Establish a connection <br/> string strconn <br/> = @ "provider = microsoft. jet. oledb.4.0; Data Source = "+ mdbpath +"; Jet oledb: Database Password = haoren "; <br/> oledbconnection odcconnection = new oledbconnection (strconn ); <Br/> // 2. Open the connection <br/> odcconnection. open (); </P> <p> int size = insertarray. count; <br/> string STR = ""; <br/> for (INT I = 0; I <size; I ++) <br/>{< br/> node = new node (); <br/> node = (node) insertarray [I]; <br/> string v = node. nodevalue. tostring (); <br/> V = dealstring (V); <br/> STR + = node. nodetype + "="; <br/> If (V = ") <br/>{< br/> STR + =" null "+ ','; <br/>}< br/> else <br/> {< Br/> STR + = "'" + V + "'" + ','; <br/>}</P> <p >}< br/> STR = Str. trimend (','); <br/> If (picturenode. nodevalue! = NULL) <br/>{< br/> STR + = ',' + picturenode. nodetype; <br/> STR + = "= @ image "; <br/>}< br/> string SQL = "Update" + tablename + "set" + STR + <br/> "where" + keynode. nodetype + "=" + "'" + keynode. nodevalue + "'"; <br/> oledbcommand odcommand = new oledbcommand (SQL, odcconnection); <br/> If (picturenode. nodevalue! = NULL) <br/>{< br/> odcommand. parameters. add ("@ image", oledbtype. varbinary, picturenode. nodevalue. length ). value = picturenode. nodevalue; <br/>}< br/> odcommand. executenonquery (); <br/> odcconnection. close (); <br/> return true; <br/>}< br/> catch (exception ERR) <br/>{< br/> errinfo = err. message; <br/> return false; <br/>}< br/>}
3. Insert image data.
// Insert image data <br/> Public static bool insertpicturetomdb (string mdbpath, string tablename, node keynode, <br/> picturenode, ref string errinfo) <br/> {<br/> try <br/> {<br/> // 1. Establish a connection <br/> string strconn <br/> = @ "provider = microsoft. jet. oledb.4.0; Data Source = "+ mdbpath +"; Jet oledb: Database Password = haoren "; <br/> oledbconnection odcconnection = new oledbconnection (strconn ); <br/> // 2. Open the connection <br/> odcconnection. open (); <br/> string SQL = @ "Update" + tablename + "set" + picturenode. nodetype + "=" <br/> + "@ image where" + keynode. nodetype + "=" + "'" + keynode. nodevalue + "'"; <br/> oledbcommand comm = new oledbcommand (SQL, odcconnection); <br/> byte [] PIC = picturenode. nodevalue; <br/> comm. parameters. add ("@ image", oledbtype. varbinary, Pic. length ). value = PIC; <br/> comm. executenonquery (); <br/> odcconnection. close (); <br/> return true; <br/>}< br/> catch (exception ERR) <br/>{< br/> errinfo = err. message; <br/> return false; <br/>}< br/>}
4. modify a data entry of MDB.
// Modify a data entry of MDB <br/> Public static bool updatemdbnode (string tablename, node keynode, <br/> node savenode, ref string errinfo) <br/> {<br/> try <br/> {<br/> // 1. Establish a connection <br/> string strconn <br/> = @ "provider = microsoft. jet. oledb.4.0; Data Source = "+ mdbpath +"; Jet oledb: Database Password = haoren "; <br/> oledbconnection odcconnection = new oledbconnection (strconn ); <br/> // 2. Open the connection <br/> odcconnection. open (); </P> <p> string SQL = @ "Update" + tablename + "set" + savenode. nodetype + "= '" + savenode. nodevalue + <br/> "'where" + keynode. nodetype + "=" + "'" + keynode. nodevalue + "'"; <br/> oledbcommand comm = new oledbcommand (SQL, odcconnection); <br/> comm. executenonquery (); <br/> odcconnection. close (); <br/> return true; <br/>}< br/> catch (exception ERR) <br/>{< br/> errinfo = err. message; <br/> return false; <br/>}< br/>}
5. Get photos from MDB
// Obtain the photo from MDB <br/> Public static byte [] getimagefrommdb (string tablename, node keynode) <br/> {<br/> try <br/> {<br/> string SQL = "select photo from" + tablename + <br/> "member where" + keynode. nodetype + "=" <br/> + "'" + keynode. nodevalue + "'"; <br/> string strconn <br/> = @ "provider = Microsoft. jet. oledb.4.0; Data Source = "+ mdbpath +"; Jet oledb: Database Password = haoren "; <br/> oledbconnection odcconnection = new oledbconnection (strconn ); <br/> // 2. Open the connection <br/> odcconnection. open (); <br/> oledbcommand comm = new oledbcommand (SQL, odcconnection); <br/> oledbdatareader SDR = comm. executereader (); <br/> SDR. read (); </P> <p> byte [] PIC = (byte []) SDR [0]; <br/> SDR. close (); <br/> odcconnection. close (); <br/> return PIC; <br/>}< br/> catch <br/>{< br/> return NULL; <br/>}< br/>}
All the problems described above are solved. These program codes have passed the test in net2005. I hope to help you.
To add a reprinted message, please indicate the source. Thank you! Banzhiyan Ajie http://blog.csdn.net/gisfarmer/