CUBRID study notes 32 support for net datatable cubrid tutorial, cubriddatatable

Source: Internet
Author: User

CUBRID study notes 32 support for net datatable cubrid tutorial, cubriddatatable

  • Implement reasonable support in the net driver
  • DataTable data populate
  • Built-in commands construct: INSERT, UPDATE, DELETE
  • Columns metadata/properties
  • DataSet, DataView inter-connection

If you look at the example, the net will understand it and will not explain it.

 

// Obtain the column attributes: String SQL = "select * from nation"; CUBRIDDataAdapter da = new CUBRIDDataAdapter (); da. selectCommand = new CUBRIDCommand (SQL, conn); DataTable dt = new DataTable ("nation"); da. fillSchema (dt, SchemaType. source); // To retrieve all the column properties you have to use the FillSchema () methodDebug. assert (dt. columns [0]. columnName = "code"); Debug. assert (dt. columns [0]. allowDBNull = false); Debug. as Sert (dt. columns [0]. defaultValue. toString () = ""); Debug. assert (dt. columns [0]. unique = true); Debug. assert (dt. columns [0]. dataType = typeof (System. string); Debug. assert (dt. columns [0]. ordinal = 0); Debug. assert (dt. columns [0]. table = dt); // insert record String SQL = "select * from nation order by 'code' asc"; using (CUBRIDDataAdapter da = new CUBRIDDataAdapter (SQL, conn )) {using (CUBRIDDataAdapter daCmd = new CUBRIDDataAdapter (SQL, conn) {CUBRIDCommandBuilder cmdBuilder = new CUBRIDCommandBuilder (daCmd); da. insertCommand = cmdBuilder. getInsertCommand ();} DataTable dt = newDataTable ("nation"); da. fill (dt); DataRow newRow = dt. newRow (); newRow ["code"] = "ZZZ"; newRow ["name"] = "ABCDEF"; newRow ["capital"] = "MyXYZ "; newRow ["continent"] = "QWERTY"; dt. rows. add (newRow); da. update (dt);} // complete code: using CUBRI D. data. CUBRIDClient; using System. diagnostics; using System. data; using System; namespace DataTableExample {class Program {private static void ExecuteSQL (string SQL, CUBRIDConnection conn) {using (CUBRIDCommand cmd = new CUBRIDCommand (SQL, conn) {cmd. executeNonQuery () ;}} private static int GetTableRowsCount (string tableName, CUBRIDConnection conn) {int count =-1; string SQL = "select count (*) From '"+ tableName +"' "; using (CUBRIDCommand cmd = new CUBRIDCommand (SQL, conn) {count = (int) cmd. executeScalar ();} return count;} static void Main (string [] args) {CUBRIDConnectionStringBuilder sb = new CUBRIDConnectionStringBuilder ("localhost", "demodb", "public ","", "33000"); using (CUBRIDConnection conn = new CUBRIDConnection (sb. getConnectionString () {conn. open (); String SQL = "sele Ct * from nation order by 'code' desc limit 10 "; using (CUBRIDDataAdapter da = new CUBRIDDataAdapter (SQL, conn )) {// Initialize the command object that will be used as the UpdateCommand for the DataAdapter. CUBRIDCommand daInsert = new CUBRIDCommand ("insert into nation values (?,?,?,?) ", Conn); daInsert. CommandType = CommandType. Text; // Parameter: code daInsert. Parameters. Add (new CUBRIDParameter ("? P1 ", DbType. String); daInsert. Parameters ["? P1 "]. SourceVersion = DataRowVersion. Current; daInsert. Parameters ["? P1 "]. SourceColumn =" code "; daInsert. Parameters ["? P1 "]. SourceColumnNullMapping = false; // Parameter: name daInsert. Parameters. Add (new CUBRIDParameter ("? P2 ", DbType. String); daInsert. Parameters ["? P2 "]. SourceVersion = DataRowVersion. Original; daInsert. Parameters ["? P2 "]. SourceColumn =" name "; daInsert. Parameters ["? P2 "]. SourceColumnNullMapping = false; // Parameter: continent daInsert. Parameters. Add (new CUBRIDParameter ("? P3 ", DbType. String); daInsert. Parameters ["? P3 "]. SourceVersion = DataRowVersion. Current; daInsert. Parameters ["? P3 "]. SourceColumn =" continent "; daInsert. Parameters ["? P3 "]. SourceColumnNullMapping = false; // Parameter: capital daInsert. Parameters. Add (new CUBRIDParameter ("? P4 ", DbType. String); daInsert. Parameters ["? P4 "]. SourceVersion = DataRowVersion. Original; daInsert. Parameters ["? P4 "]. SourceColumn =" capital "; daInsert. Parameters ["? P4 "]. sourceColumnNullMapping = false; daInsert. updatedRowSource = UpdateRowSource. none; // Assign the command to the InsertCommand property of the DataAdapter. da. insertCommand = daInsert; DataTable dt = new DataTable ("nation"); da. fill (dt); DataRow newRow = dt. newRow (); newRow ["code"] = "ZZZ"; newRow ["name"] = "ABCDEF"; newRow ["capital"] = "MyXYZ "; newRow ["continent"] = "QWERTY"; dt. rows. insertAt (n EwRow, 0); da. update (dt); dt. acceptChanges (); Debug. assert (dt. rows [0] ["capital"]. toString () = "MyXYZ"); Debug. assert (newRow. rowState. toString ()! = "New");} Debug. assert (GetTableRowsCount ("nation", conn) = 216); // Revert changes ExecuteSQL ("delete from nation where 'code' = 'zzz'", conn); Debug. assert (GetTableRowsCount ("nation", conn) = 215); conn. close ();}}}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.