using System; using System.Collections.Generic; using System.Text; using System.Data.Common; using System.Data.SqlClient; using System.Data; namespace SQLMgtConsole { class DBFactory { // Provider Independent Code for .NET Framework Data Providers public DataTable fnExecDBCommonQuery(string strDBProvider, DbConnection dbcConnection, string strtQueryString, ref Exception exErrHandle) { DataTable dtResult = new DataTable(); try { if (dbcConnection != null && !strDBProvider.Trim().Equals("")) { dbcConnection.Open(); // You can specify the provider here. Because it is a string, // it could also be a variable that comes from a config file: DbProviderFactory dataFactory = DbProviderFactories.GetFactory(strDBProvider); DbDataAdapter da = dataFactory.CreateDataAdapter(); da.SelectCommand = dbcConnection.CreateCommand(); da.SelectCommand.CommandText = strtQueryString; // Fill the DataTable and bind the DataGrid da.Fill(dtResult); exErrHandle = null; } } catch (SqlException sqlEx) { Console.WriteLine("sqlEx : " + sqlEx.Message + Environment.NewLine + sqlEx.StackTrace); exErrHandle = (Exception)sqlEx; dtResult = null; } catch (Exception ex) { Console.WriteLine("Ex : " + ex.Message + Environment.NewLine + ex.StackTrace); exErrHandle = ex; dtResult = null; } finally { if (dbcConnection != null) dbcConnection.Close(); } return dtResult; } } }
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.
This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)