private void btnConnect_Click(object sender, EventArgs e) { CConnectionContext cc = new CConnectionContext(txtHost.Text, 20902, txtUser.Text, txtPassword.Text); m_spMysql = new CSocketPool <CMysql>(false); //set event for MySQL/Mariadb database shutdown m_spMysql.SocketPoolEvent += new CSocketPool <CMysql> .DOnSocketPoolEvent(m_spMysql_SocketPoolEvent); if (!m_spMysql.StartSocketPool(cc, 1, 1)) { txtMessage.Text = "No connection to " + txtHost.Text; return; } CMysql mysql = m_spMysql.AsyncHandlers[0]; //set event for tracking all database table update events, delete, update and insert m_spMysql.Sockets[0].Push.OnPublish += new DOnPublish(Push_OnPublish); //create a DB session with default to sample database sakil bool ok = mysql.Open("sakila", null, DB_CONSTS.ENABLE_TABLE_UPDATE_MESSAGES); m_ds = new DataSet("real-time cache"); DataTable dt = null; string errMsg = ""; //query all cached tables into client side for intial cache data ok = mysql.Execute("", (h, ret, err_msg, affected, fail_ok, id) => { //this callback is fired from worker thread from socket pool thread ok = (ret == 0); errMsg = err_msg; }, (h, data) => { //this callback is fired from worker thread from socket pool thread CMysql.AppendRowDataIntoDataTable(data, dt); }, (h) => { //this callback is fired from worker thread from socket pool thread dt = CMysql.MakeDataTable(h.ColumnInfo); string name = h.ColumnInfo[0].DBPath + "." + h.ColumnInfo[0].TablePath; dt.TableName = name; m_ds.Tables.Add(dt); }); ok = mysql.WaitAll(); txtMessage.Text = errMsg; lstTables.Items.Clear(); foreach (DataTable table in m_ds.Tables) { lstTables.Items.Add(table.TableName); } if (m_ds.Tables.Count > 0) { lstTables.SelectedIndex = 0; } btnDisconnect.Enabled = ok; btnConnect.Enabled = !ok; }
static void Main(string[] args) { Console.WriteLine("SocketPro performance test against a remote MySQL backend DB"); Console.WriteLine("Remote host: "); string host = Console.ReadLine(); Console.WriteLine("Database name: "); string dbName = Console.ReadLine(); Console.WriteLine("Table name: "); string tableName = Console.ReadLine(); Console.WriteLine("sql filter: "); string filter = Console.ReadLine(); CConnectionContext cc = new CConnectionContext(host, 20902, "root", "Smash123"); Console.WriteLine("Asynchronous execution (0) or synchronous execution (1) ?"); bool sync = (Console.ReadKey().KeyChar != '0'); using (CSocketPool <CMysql> spMysql = new CSocketPool <CMysql>()) { if (!spMysql.StartSocketPool(cc, 1, 1)) { Console.WriteLine("Failed in connecting to remote helloworld server"); Console.WriteLine("Press any key to close the application ......"); Console.Read(); return; } Console.WriteLine(""); Console.WriteLine("Computing ......"); CMysql mysql = spMysql.Seek(); CAsyncDBHandler.DResult dr = (handler, res, errMsg) => { if (res != 0) { Console.WriteLine("res = {0}, errMsg: {1}", res, errMsg); } }; uint obtained = 0; bool ok = mysql.Open(dbName, dr); #if USE_DATATABLE List <KeyValuePair <CDBColumnInfoArray, DataTable> > ra = new List <KeyValuePair <CDBColumnInfoArray, DataTable> >(); #else List <KeyValuePair <CDBColumnInfoArray, CDBVariantArray> > ra = new List <KeyValuePair <CDBColumnInfoArray, CDBVariantArray> >(); #endif CAsyncDBHandler.DExecuteResult er = (handler, res, errMsg, affected, fail_ok, id) => { if (res != 0) { Console.WriteLine("fails = {0}, oks = {1}, res = {2}, errMsg: {3}", (uint)(fail_ok >> 32), (uint)fail_ok, res, errMsg); } ra.Clear(); ++obtained; }; CAsyncDBHandler.DRows r = (handler, rowData) => { //rowset data come here int last = ra.Count - 1; #if USE_DATATABLE KeyValuePair <CDBColumnInfoArray, DataTable> item = ra[last]; CAsyncDBHandler.AppendRowDataIntoDataTable(rowData, item.Value); #else KeyValuePair <CDBColumnInfoArray, CDBVariantArray> item = ra[last]; item.Value.AddRange(rowData); #endif }; CAsyncDBHandler.DRowsetHeader rh = (handler) => { //rowset header comes here #if USE_DATATABLE DataTable dt = CAsyncDBHandler.MakeDataTable(handler.ColumnInfo); KeyValuePair <CDBColumnInfoArray, DataTable> item = new KeyValuePair <CDBColumnInfoArray, DataTable>(handler.ColumnInfo, dt); #else KeyValuePair <CDBColumnInfoArray, CDBVariantArray> item = new KeyValuePair <CDBColumnInfoArray, CDBVariantArray>(handler.ColumnInfo, new CDBVariantArray()); #endif ra.Add(item); }; obtained = 0; string sql = "select * from " + tableName; if (filter.Length > 0) { sql += " where " + filter; } int count = 10000; DateTime start = DateTime.Now; for (int n = 0; n < count; ++n) { ok = mysql.Execute(sql, er, r, rh); if (sync && ok) { ok = mysql.WaitAll(); } if (!ok) { break; } } if (!sync && ok) { ok = mysql.WaitAll(); } double diff = (DateTime.Now - start).TotalMilliseconds; Console.WriteLine("Time required = {0} milliseconds for {1} query requests", diff, obtained); //you need to compile and run the sample project test_sharp before running the below code ok = mysql.Execute("USE mysqldb;delete from company where id > 3"); string sql_insert_parameter = "INSERT INTO company(ID,NAME,ADDRESS,Income)VALUES(?,?,?,?)"; ok = mysql.Prepare(sql_insert_parameter, dr); ok = mysql.WaitAll(); int index = 0; count = 50000; Console.WriteLine(); Console.WriteLine("Going to insert {0} records into the table mysqldb.company", count); start = DateTime.Now; CDBVariantArray vData = new CDBVariantArray(); ok = mysql.BeginTrans(); for (int n = 0; n < count; ++n) { vData.Add(n + 4); int data = (n % 3); switch (data) { case 0: vData.Add("Google Inc."); vData.Add("1600 Amphitheatre Parkway, Mountain View, CA 94043, USA"); vData.Add(66000000000.12); break; case 1: vData.Add("Microsoft Inc."); vData.Add("700 Bellevue Way NE- 22nd Floor, Bellevue, WA 98804, USA"); vData.Add(93600000001.24); break; default: vData.Add("Apple Inc."); vData.Add("1 Infinite Loop, Cupertino, CA 95014, USA"); vData.Add(234000000002.17); break; } ++index; //send 2000 sets of parameter data onto server for processing in batch if (2000 == index) { ok = mysql.Execute(vData, er); ok = mysql.EndTrans(); vData.Clear(); Console.WriteLine("Commit {0} records into the table mysqldb.company", index); ok = mysql.BeginTrans(); index = 0; } } if (vData.Count > 0) { ok = mysql.Execute(vData, er); Console.WriteLine("Commit {0} records into the table mysqldb.company", index); } ok = mysql.EndTrans(); ok = mysql.WaitAll(); diff = (DateTime.Now - start).TotalMilliseconds; Console.WriteLine("Time required = {0} milliseconds for {1} insert requests", diff, count); Console.WriteLine(); Console.WriteLine("Press any key to close the application ......"); Console.ReadLine(); } }
static void Main(string[] args) { Console.WriteLine("Remote host: "); string host = Console.ReadLine(); #if FOR_MIDDLE_SERVER CConnectionContext cc = new CConnectionContext(host, 20901, "root", "Smash123"); #else CConnectionContext cc = new CConnectionContext(host, 20902, "root", "Smash123"); #endif using (CSocketPool <CMysql> spMysql = new CSocketPool <CMysql>(true, 600000)) { if (!spMysql.StartSocketPool(cc, 1, 1)) { Console.WriteLine("Failed in connecting to remote async mysql server"); Console.WriteLine("Press any key to close the application ......"); Console.Read(); return; } CMysql mysql = spMysql.Seek(); #if FOR_MIDDLE_SERVER bool ok = mysql.Open("user=root;pwd=Smash123;db=mysqldb", dr); #else bool ok = mysql.Open("", dr); #endif List <KeyValuePair <CDBColumnInfoArray, CDBVariantArray> > ra = new List <KeyValuePair <CDBColumnInfoArray, CDBVariantArray> >(); CMysql.DRows r = (handler, rowData) => { //rowset data come here int last = ra.Count - 1; KeyValuePair <CDBColumnInfoArray, CDBVariantArray> item = ra[last]; item.Value.AddRange(rowData); }; CMysql.DRowsetHeader rh = (handler) => { //rowset header comes here KeyValuePair <CDBColumnInfoArray, CDBVariantArray> item = new KeyValuePair <CDBColumnInfoArray, CDBVariantArray>(handler.ColumnInfo, new CDBVariantArray()); ra.Add(item); }; TestCreateTables(mysql); ok = mysql.Execute("delete from employee;delete from company", er); TestPreparedStatements(mysql); InsertBLOBByPreparedStatement(mysql); ok = mysql.Execute("SELECT * from company;select * from employee;select curtime()", er, r, rh); CDBVariantArray vPData = TestStoredProcedure(mysql, ra); ok = mysql.WaitAll(); Console.WriteLine(); Console.WriteLine("There are {0} output data returned", mysql.Outputs * 2); CDBVariantArray vData = TestBatch(mysql, ra); ok = mysql.WaitAll(); Console.WriteLine(); Console.WriteLine("There are {0} output data returned", mysql.Outputs * 3); int index = 0; Console.WriteLine(); Console.WriteLine("+++++ Start rowsets +++"); foreach (KeyValuePair <CDBColumnInfoArray, CDBVariantArray> it in ra) { Console.Write("Statement index = {0}", index); if (it.Key.Count > 0) { Console.WriteLine(", rowset with columns = {0}, records = {1}.", it.Key.Count, it.Value.Count / it.Key.Count); } else { Console.WriteLine(", no rowset received."); } ++index; } Console.WriteLine("+++++ End rowsets +++"); Console.WriteLine(); Console.WriteLine("Press any key to close the application ......"); Console.Read(); } }