CDataService.cs for generic database access with a testing client program - class1.cs. A short explanation of the attached codes is included.
A Generic Database Access Class
Using a generic database access component in data-aware applications can simply development and separate the data tier from the business tier. The following C# component - CDataService.cs has the four basic database manipulating functions to select, insert, update, and delete data in the SQL database. Client code can pass either a SQL statement or a stored procedure name with parameters to complete the database access. For database other than SQL 7.0/2000, the namespace - "System.Data.SqlClient" should be replaced with "System.Data.OleDb". All "Sql" prefix in the code should be replaced with "OleDb", for instance, "SqlDataAdapter" should be changed to "OleDbDataAdapter".
A WebService for generic database access can be developed easily based on the CDataService.cs. However, all public data access web methods may need a parameter for connection string since they are stateless unless an application or a session object is used to store the connection string.
Database Access Component
The public method - OpenDataSorce( ) returns an object of System.Data.SqlClient.SqlConnection in case a client code needs a connection for some special database access, such as debugging. However, in general, a business object should avoid calling this method directly to access a database connection. Thus, this method may be changed to private for the real world.
CDataService.cs code
- using System;
- using System.Data;
- using System.IO;
- using System.Data.SqlClient;
- namespace DataService_ {
-
-
-
-
- public class CDataService {
-
-
-
- private string m_ConnectionString;
-
-
-
- private SqlConnection m_Connection;
-
-
-
-
- public string ConnectionString {
- get { return m_ConnectionString; }
- set { m_ConnectionString = value; }
- }
-
-
-
- public CDataService() {}
-
-
-
-
- public CDataService(string ConnectionString) {
- this.m_ConnectionString = ConnectionString;
- }
-
-
-
- ~CDataService() {
- this.CloseDataSource();
- }
-
-
-
-
- public SqlConnection OpenDataSource() {
-
- if (m_Connection == null) {
-
- if (m_ConnectionString == null || m_ConnectionString.Length == 0) {
- try {
- m_ConnectionString = ReadConnectionString(@ "DataService_.cfg");
- } catch (System.Exception e) {
- throw e;
- }
- }
- try {
- m_Connection = new SqlConnection(m_ConnectionString);
- m_Connection.Open();
- } catch (System.Exception e) {
- throw e;
- }
- } else if (m_Connection.State != ConnectionState.Open) {
- m_Connection.ConnectionString = m_ConnectionString;
- try {
- m_Connection.Open();
- } catch (Exception e) {
- throw e;
- }
- }
- return m_Connection;
- }
-
-
-
-
-
- private string ReadConnectionString(string FilePath) {
- string s = null;
- try {
- StreamReader sr = File.OpenText(FilePath);
- s = sr.ReadToEnd();
- } catch (System.Exception e) {
- throw e;
- }
- return (s);
- }
-
-
-
- public void CloseDataSource() {
- if (m_Connection != null) {
- if (m_Connection.State == ConnectionState.Open)
- m_Connection.Close();
- m_Connection = null;
- }
- }
-
-
-
-
-
-
- public DataSet SelectSqlData(string QueryString, string TableName, bool ustClose) {
- DataSet ds = new DataSet();
- SqlDataAdapter ad = new SqlDataAdapter();
- try {
- ad.SelectCommand = new SqlCommand(QueryString, this.OpenDataSource());
- if (TableName.Trim().Length > 0) ad.Fill(ds, TableName);
- else ad.Fill(ds);
- } catch (System.Exception e) {
- throw e;
- } finally {
- if (MustClose == true) this.CloseDataSource();
- }
- return ds;
- }
-
-
-
-
-
- public DataSet SelectSqlData(string QueryString, string TableName) {
- return SelectSqlData(QueryString, TableName, false);
- }
-
-
-
-
-
- public DataSet SelectSqlData(string QueryString) {
- return SelectSqlData(QueryString, "", false);
- }
-
-
-
-
- public void InsertSqlData(string InsertString, bool MustClose) {
- SqlDataAdapter da = new SqlDataAdapter();
- try {
- da.InsertCommand = new SqlCommand(InsertString, this.OpenDataSource());
- da.InsertCommand.ExecuteNonQuery();
- } catch (Exception e) {
- throw e;
- } finally {
- if (MustClose == true) this.CloseDataSource();
- }
- }
-
-
-
-
-
- public void InsertSqlData(string InsertString) {
- InsertSqlData(InsertString, false);
- }
-
-
-
-
- public void DeleteSQLData(string DeleteString, bool MustClose) {
- SqlDataAdapter da = new SqlDataAdapter();
- try {
- da.DeleteCommand = new SqlCommand(DeleteString, this.OpenDataSource());
- da.DeleteCommand.ExecuteNonQuery();
- } catch (Exception e) {
- throw e;
- } finally {
- if (MustClose == true) this.CloseDataSource();
- }
- }
- public void DeleteSQLData(string DeleteString) {
- DeleteSQLData(DeleteString, false);
- }
-
-
-
-
-
- public void UpdateSQLData(string UpdateString, bool MustClose) {
- SqlDataAdapter da = new SqlDataAdapter();
- try {
- da.UpdateCommand = new SqlCommand(UpdateString, this.OpenDataSource());
- da.UpdateCommand.ExecuteNonQuery();
- } catch (Exception e) {
- throw e;
- } finally {
- if (MustClose == true) this.CloseDataSource();
- }
- }
-
-
-
-
- public void UpdateSQLData(string UpdateString) {
- UpdateSQLData(UpdateString, false);
- }
- }
- }
class1.cs code
- using System;
- using System.Data;
- using DataService_;
- namespace TryDataService {
- class Class1 {
- private
- const string ConnString = "server=(local);database=northwind;user id=sa;password=mypassword;";
- static void Main(string[] args) {
- string sSQL;
- DataSet ds = new DataSet();
- CDataService objData = new CDataService(ConnString);
- Console.WriteLine("Fetch data by SQL statement...");
- sSQL = "select * from orders where customerid = " + SQLString("WELLI");
- try {
- ds = objData.SelectSqlData(sSQL, "orders", true);
- } catch (Exception e) {
- Console.WriteLine(e.ToString());
- }
- Console.WriteLine("Fetch data via stored procedure...");
- sSQL = "sp_GetOrders " + SQLString("WELLI");
- try {
- ds = objData.SelectSqlData(sSQL, "orders", true);
- } catch (Exception e) {
- Console.WriteLine(e.ToString());
- }
- Console.WriteLine("Insert, update and delete data...");
- sSQL = "insert into Customers (CustomerId, CompanyName ) Values ( ";
- sSQL += SQLString("AAAAA") + ", " + SQLString("A Company") + ")";
- try {
- objData.InsertSqlData(sSQL);
- sSQL = "update customers set ContactName = " + SQLString("A Name");
- sSQL += "where CustomerId = " + SQLString("AAAAA");
- objData.UpdateSQLData(sSQL);
- sSQL = "sp_DeleteCustomer " + SQLString("AAAAA");
- objData.DeleteSQLData(sSQL);
- } catch (Exception e) {
- Console.WriteLine(e.ToString());
- } finally {
- objData.CloseDataSource();
- }
- }
- private static string SQLString(string s) {
- return ("'" + s.Replace("'", "''") + "'");
- }
- }
- }