Дата публикации: 03.06.2013
Выполнение запроса «INSERT RETURNING» (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | //////////////////////////////////////////////////////////////////////////////// //Samples for LCPI ADO.NET Data provider for OLEDB. // 28.05.2013 using System; using System.Data; using lcpi.data.oledb; namespace Sample_0005 { class Program { private const string c_cn_str = "provider=LCPI.IBProvider.3;" + "location=localhost:d:\\database\\ibp_test_fb25_d3.gdb;" + "user id=gamer;" + "password=vermut;" ; //---------------------------------------------------------------------- private static char GenChar( int i) { return ( char )( 'a' +(i%26)); } //GenChar //---------------------------------------------------------------------- static int Main( string [] args) { int resultCode=0; try { const string c_test_str= ".NET Provider Sample #0005" ; var cn= new OleDbConnection(c_cn_str); cn.Open(); var tr=cn.BeginTransaction(IsolationLevel.RepeatableRead); var cmd= new OleDbCommand( "" ,cn,tr); //--------------------------------------INSERT cmd.CommandText= "insert into TBL_CS__ASCII (VARCHAR__32) values(:x)\n" + "returning TEST_ID" ; //define INPUT parameter cmd.Parameters.Add( "x" ,OleDbType.VarWChar,0,ParameterDirection.Input).Value=c_test_str; //define OUTPUT parameter cmd.Parameters.Add( "TEST_ID" ,OleDbType.Variant,0,ParameterDirection.Output); var RowsAffected=cmd.ExecuteNonQuery(); var rec_id=cmd.Parameters[ "TEST_ID" ].Value; Console.WriteLine( "RowsAffected: {0}, TEST_ID={1}" , RowsAffected, rec_id); //--------------------------------------SELECT cmd.CommandText= "select VARCHAR__32 from TBL_CS__ASCII where TEST_ID=:test_id" ; //Remove previous set of parameters cmd.Parameters.Clear(); cmd.Parameters.Add( "test_id" ,OleDbType.Variant,0,ParameterDirection.Input).Value=rec_id; using ( var reader=cmd.ExecuteReader()) { if (!reader.Read()) throw new ApplicationException( "Test record not found!" ); if (reader.GetString( /*VARCHAR__32*/ 0)!=c_test_str) throw new ApplicationException( "Select wrong column data!" ); Console.WriteLine( "OK. We select the correct field value." ); if (reader.Read()) throw new ApplicationException( "Select more than one row!" ); } //using reader //Commit transaction tr.Commit(); //----- Console.WriteLine( "All is OK." ); } catch (Exception e) { resultCode=1; Console.WriteLine( "" ); Console.WriteLine( "ERROR: {0} - {1}" ,e.Source,e.Message); } //catch return resultCode; } //Main } //class Program } //Sample_0005 |