public void Extensions_T004_IDataReader_ToDelimitedFile() { const long RowsToRead = 50; string CmdText = "select top " + RowsToRead.ToString() + " * from [Northwind].[dbo].[Customers]"; const int BufferSize = 1024 * 4; const string TestFile = "c:\\temp\\test.csv"; //var Provider = DBHelper.Providers.OleDB; // 12 sec var Provider = DBHelper.Providers.SqlServer; // 08 sec //var Provider = DBHelper.Providers.ODBC; // 20 sec string Cnstring = string.Empty; switch (Provider) { case DBHelper.Providers.SqlServer: //Cnstring = SQL_PROD_CONNECTIONSTRING; Cnstring = SQL_SQLSERVER_LOCALDB_CONNECTIONSTRING; break; case DBHelper.Providers.OleDB: Cnstring = SQL_OLEDB_LOCALDB_CONNECTIONSTRING; break; case DBHelper.Providers.ODBC: Cnstring = SQL_ODBC_LOCALDB_CONNECTIONSTRING; break; case DBHelper.Providers.Oracle: break; default: break; } long FileRows = 0; // create a connection object using (var dbh = new DBHelper(false)) { dbh.CreateDBObjects(Cnstring, Provider, null); DbDataReader rdr = (DbDataReader)Utility.RetryMethod(new Func <string, CommandType, ConnectionState, DbDataReader>(dbh.ExecuteReader), 3, 3, CmdText, CommandType.Text, ConnectionState.Open); FileRows = rdr.ToDelimitedFile(TestFile, false, Encoding.Default, BufferSize, true, textQualifier: "\"", columnDelimiter: "|"); } var r = File.Exists(TestFile); Assert.IsInstanceOfType(r, typeof(bool)); Assert.AreEqual(true, r); Assert.AreEqual(RowsToRead, FileRows); }
public void Utility_T004_RetryMethod() { const long RowsToRead = 90; string CmdTextOK = "select top " + RowsToRead.ToString() + " * from [Northwind].[dbo].[Customers]"; string CmdTextWRONG = "select top " + RowsToRead.ToString() + " * [Northwind].[dbo].[XXXX]"; //var Provider = DBHelper.Providers.OleDB; // 12 sec var Provider = DBHelper.Providers.SqlServer; // 08 sec //var Provider = DBHelper.Providers.ODBC; // 20 sec string Cnstring = string.Empty; switch (Provider) { case DBHelper.Providers.SqlServer: Cnstring = SQL_SQLSERVER_LOCALDB_CONNECTIONSTRING; break; case DBHelper.Providers.OleDB: Cnstring = SQL_OLEDB_LOCALDB_CONNECTIONSTRING; break; case DBHelper.Providers.ODBC: Cnstring = SQL_ODBC_LOCALDB_CONNECTIONSTRING; break; case DBHelper.Providers.Oracle: break; default: break; } // create a connection object using (var dbh = new DBHelper(false)) { dbh.CreateDBObjects(Cnstring, Provider, null); // Example without retries. //var rdr = dbh.ExecuteReader(CmdText, CommandType.Text, ConnectionState.Open); // Example with retries defining the delegate before and invoking in the Utility.RetryMethod note that the return type 'DataSet' is declared at the end, and the parameters types before. var newfuction = new Func <string, CommandType, ConnectionState, DataSet>(dbh.GetDataSet); // You can define the delegate before or inside of the RetryMethod. DataSet rdr = (DataSet)Utility.RetryMethod(newfuction, 3, 3, CmdTextOK, CommandType.Text, ConnectionState.Open); //rdr = (DataSet)Utility.RetryMethod(newfuction, 3, 3, CmdTextWRONG, CommandType.Text, ConnectionState.Open); // Example with retries defining the delegate inside and imvoking the Utility.RetryMethod note that the return type 'DbDataReader' is declared at the end and the parameters types before. // DbDataReader rdr = (DbDataReader)Utility.RetryMethod(new Func<string, CommandType, ConnectionState, DbDataReader>(dbh.ExecuteReader), 3, 3, CmdTextOK, CommandType.Text, ConnectionState.Open); Assert.IsInstanceOfType(rdr, typeof(DataSet)); Assert.AreEqual(RowsToRead, rdr.Tables[0].Rows.Count); } }