public static long GetLastRowId(GFXDClientConnection conn, string tableName, string identityName) { long id = 0; try { GFXDCommand cmd = conn.CreateCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText = String.Format( "SELECT {0} FROM {1} ORDER BY {2} DESC FETCH FIRST 1 ROWS ONLY", identityName, tableName, identityName); GFXDDataReader rdr = cmd.ExecuteReader(); while (rdr.Read()) { id = long.Parse(rdr.GetString(0)); } } catch (Exception e) { throw new Exception(e.Message, e.InnerException); } return(id); }
protected void ParseDataReader(GFXDDataReader reader) { while (reader.Read()) { StringBuilder row = new StringBuilder(); for (int i = 0; i < reader.FieldCount; i++) { row.AppendFormat("{0}, ", reader.GetString(i)); } Log(row.ToString()); } }
protected void VerifyInserts(GFXDConnection conn, string cmdText, int start, int end, string addrPrefix) { // check the inserts GFXDCommand cmd = new GFXDCommand(cmdText, conn); Dictionary <int, string> result = new Dictionary <int, string>(end - start + 1); int id; string addr; GFXDDataReader reader = cmd.ExecuteReader(); for (int i = start; i <= end; i++) { Assert.IsTrue(reader.Read(), "failed in read for i=" + i); id = reader.GetInt32(0); addr = reader.GetString(1); Assert.IsFalse(result.ContainsKey(id), "unexpected duplicate for id=" + id); Assert.AreEqual(addrPrefix + id, addr); result.Add(id, addr); } Assert.IsFalse(reader.Read()); }
public void DataReaderWithPositionalParameters() { // Open a new connection to the network server running on localhost string host = "localhost"; int port = s_clientPort; string connStr = string.Format("server={0}:{1}", host, port); using (GFXDClientConnection conn = new GFXDClientConnection(connStr)) { conn.Open(); // create a table GFXDCommand cmd = new GFXDCommand("create table t1 (id int primary" + " key, addr varchar(20))", conn); cmd.ExecuteNonQuery(); try { // insert into the table using positional parameters cmd = new GFXDCommand("insert into t1 (id, addr) values (?, ?)", conn); cmd.Prepare(); for (int i = 1; i <= s_numInserts; i++) { cmd.Parameters.Clear(); cmd.Parameters.Add(i); cmd.Parameters.Add("addr" + i); cmd.ExecuteNonQuery(); } // now query the table using a DataReader cmd.Parameters.Clear(); cmd.CommandText = "select * from t1"; GFXDDataReader reader = cmd.ExecuteReader(); int[] ids = new int[s_numInserts]; int numResults = 0; while (reader.Read()) { int id = reader.GetInt32(0); string addr = reader.GetString(1); if (ids[id - 1] != 0) { throw new Exception("Duplicate value for ID=" + id + " addr=" + addr); } ids[id - 1] = id; numResults++; } reader.Close(); if (numResults != s_numInserts) { throw new Exception("unexpected number of results " + numResults); } } finally { // drop the table cmd = new GFXDCommand("drop table t1", conn); cmd.ExecuteNonQuery(); conn.Close(); } } }
protected void ParseDataReader(GFXDDataReader reader) { while (reader.Read()) { StringBuilder row = new StringBuilder(); for (int i = 0; i < reader.FieldCount; i++) row.AppendFormat("{0}, ", reader.GetString(i)); Log(row.ToString()); } }
public override void Run(object context) { String tableName = null; try { tableName = DbRandom.BuildRandomTable(10); if (DbHelper.TableExists(tableName)) { Log(String.Format("Table {0} has been created", tableName)); } else { Log(String.Format("Failed to create table {0}", tableName)); } //GFXDClientConnection conn = Helper.OpenNewConnection(); GFXDCommand cmd = Connection.CreateCommand(); cmd.CommandText = String.Format("SELECT * FROM {0}", tableName); GFXDDataReader rdr = cmd.ExecuteReader(); int colCount = DbHelper.GetTableColumnCount(tableName); while (rdr.Read()) { StringBuilder row = new StringBuilder(); for (int i = 0; i < colCount; i++) { try { String data = rdr.GetString(i); if (data == null && data == String.Empty) { Log("GetString() failed to retrieve column data"); } row.Append(data); row.Append(", "); } catch (Exception e) { Log(e.Message); } } Log(row.ToString()); } } catch (Exception e) { Fail(e); } finally { try { DbHelper.DropTable(tableName); } catch (Exception e) { Fail(e); } base.Run(context); } }