CUBRID学习笔记 32 对net的datatable的支持 cubrid教程

时间:2022-05-30 03:14:07
  • 在net的驱动中实现理一下的支持
  • DataTable data populate
  • Built-in commands construct: INSERT , UPDATE, DELETE
  • Columns metadata/properties
  • DataSetDataView inter-connection

还是看例子  会net都会看懂 不解释

//获取列属性:
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() method Debug.Assert(dt.Columns[].ColumnName == "code");
Debug.Assert(dt.Columns[].AllowDBNull == false);
Debug.Assert(dt.Columns[].DefaultValue.ToString() == "");
Debug.Assert(dt.Columns[].Unique == true);
Debug.Assert(dt.Columns[].DataType == typeof(System.String));
Debug.Assert(dt.Columns[].Ordinal == );
Debug.Assert(dt.Columns[].Table == dt);
//插入记录
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);
}
//完整的代码:
using CUBRID.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 = -;
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", "", "");
using (CUBRIDConnection conn = new CUBRIDConnection(sb.GetConnectionString()))
{
conn.Open(); String sql = "select * 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(newRow, );
da.Update(dt);
dt.AcceptChanges(); Debug.Assert(dt.Rows[]["capital"].ToString() == "MyXYZ");
Debug.Assert(newRow.RowState.ToString() != "New");
} Debug.Assert(GetTableRowsCount("nation", conn) == );
//Revert changes
ExecuteSQL("delete from nation where `code` = 'ZZZ'", conn);
Debug.Assert(GetTableRowsCount("nation", conn) == ); conn.Close();
}
}
}
}