public static void DataAdapter_FillSchema_Sample() { Console.WriteLine(String.Format("{0} DataAdapter_FillSchema_Sample", databaseProductType.ToString())); Console.WriteLine("Start"); CdbcConnection con = UtilCommon.getConnection(databaseProductType); string queryStr = "SELECT * FROM USER_TABLES"; CdbcCommand cmd = new CdbcCommand(queryStr, con); CdbcDataAdapter adap = new CdbcDataAdapter(cmd); DataTable dt1 = new DataTable(); Console.WriteLine("--------------------------"); Console.WriteLine("Sql : " + queryStr); Console.WriteLine("--------------------------"); Console.WriteLine("Result:"); try { DataTable dt2 = adap.FillSchema(dt1, SchemaType.Mapped); for (int iColumn = 0; iColumn < dt1.Columns.Count; iColumn++) { Console.WriteLine(String.Format("ColumnName:{0} DataType:{1} MaxLength:{2}", dt1.Columns[iColumn].ColumnName, dt1.Columns[iColumn].DataType, dt1.Columns[iColumn].MaxLength)); } for (int iColumn = 0; iColumn < dt2.Columns.Count; iColumn++) { Console.WriteLine(String.Format("ColumnName:{0} DataType:{1} MaxLength:{2}", dt2.Columns[iColumn].ColumnName, dt2.Columns[iColumn].DataType, dt2.Columns[iColumn].MaxLength)); } } catch (Exception ex) { Console.WriteLine("Exception:"); Console.WriteLine(ex.Message); } Console.WriteLine("End"); }
public static void DataAdapter_Update_Sample() { Console.WriteLine(String.Format("{0} DataAdapter_Update_Sample", databaseProductType.ToString())); Console.WriteLine("Start"); Console.WriteLine("This sample will show you how to use CdbcDataAdapter.update insert ,update and delete data."); string guid = Guid.NewGuid().ToString(); CdbcConnection con = UtilCommon.getConnection(databaseProductType); try { //Sample 1 :Add new row (insert) Console.WriteLine("--------------------------"); Console.WriteLine("Sample 1 : Insert a record into table CODE_SAMPLE"); Console.WriteLine("--------------------------"); string queryStrInsert = @"SELECT VAR1,NUM1,DATE1,CLOB1,BLOB1 FROM CODE_SAMPLE WHERE VAR1=:VAR1"; CdbcCommand cmdInsert = new CdbcCommand(queryStrInsert, con); CdbcDataAdapter adapInsert = new CdbcDataAdapter(cmdInsert); CdbcCommandBuilder cb = new CdbcCommandBuilder(adapInsert); DataTable dt1 = new DataTable(); adapInsert.FillSchema(dt1, SchemaType.Mapped); DataRow dr1 = dt1.NewRow(); dr1["VAR1"] = guid; dr1["NUM1"] = DateTime.Now.Second; dr1["DATE1"] = DateTime.Now; dr1["CLOB1"] = "testclob"; // dr1["BLOB1"] = System.IO.File.ReadAllBytes(@"your file path"); dt1.Rows.Add(dr1); int i = adapInsert.Update(dt1); Console.WriteLine(String.Format("{0} Line(s) inserted.", i)); //Sample 2 :Edit row data (update) Console.WriteLine("--------------------------"); Console.WriteLine("Sample 2 : update the record we inserted before."); Console.WriteLine("--------------------------"); string queryStrUpdate = @"SELECT VAR1,NUM1,DATE1,CLOB1,BLOB1 FROM CODE_SAMPLE WHERE VAR1=:VAR1"; CdbcCommand cmdUpdate = new CdbcCommand(queryStrUpdate, con); CdbcParameter para = new CdbcParameter(":VAR1", CdbcOracleDbType.Varchar2); para.Value = guid; cmdUpdate.Parameters.Add(para); CdbcDataAdapter adapUpdate = new CdbcDataAdapter(cmdUpdate); CdbcCommandBuilder cbUpdate = new CdbcCommandBuilder(adapUpdate); DataSet dsUpdate = new DataSet(); adapUpdate.Fill(dsUpdate); dsUpdate.Tables[0].Rows[0]["NUM1"] = DateTime.Now.Second; dsUpdate.Tables[0].Rows[0]["DATE1"] = DateTime.Now; i = adapUpdate.Update(dsUpdate, dsUpdate.Tables[0].TableName); Console.WriteLine(String.Format("{0} Line(s) updated.", i)); //Sample 3 :Delete row (delete) Console.WriteLine("--------------------------"); Console.WriteLine("Sample 3 : Delete the record we inserted before."); Console.WriteLine("--------------------------"); DataSet dsDelete = new DataSet(); adapUpdate.Fill(dsDelete); dsDelete.Tables[0].Rows[0].Delete(); i = adapUpdate.Update(dsDelete.Tables[0]); //The following code will have same result. //i = adapUpdate.Update(dsDelete, dsDelete.Tables[0].TableName); Console.WriteLine(String.Format("{0} Line(s) Deleted.", i)); } catch (Exception ex) { Console.WriteLine("Exception:"); Console.WriteLine(ex.Message); } Console.WriteLine("end"); }