Skip to content

Instantly share code, notes, and snippets.

@t3knoid
Created February 14, 2020 19:46
Show Gist options
  • Save t3knoid/6538dec59934cef30072a5bf0d4ef1cc to your computer and use it in GitHub Desktop.
Save t3knoid/6538dec59934cef30072a5bf0d4ef1cc to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
namespace DatabaseProject
{
class DBAccess
{
private static SqlConnection connection = new SqlConnection();
private static SqlCommand command = new SqlCommand();
private static SqlDataReader DbReader;
private static SqlDataAdapter adapter = new SqlDataAdapter();
public SqlTransaction DbTran;
private static string strConnString = "Data Source=(local);Initial Catalog=SocialNetwork;Integrated Security=True";
public void createConn()
{
try
{
if (connection.State != ConnectionState.Open)
{
connection.ConnectionString = strConnString;
connection.Open();
}
}
catch (Exception ex)
{
throw ex;
}
}
public void closeConn()
{
connection.Close();
}
public int executeDataAdapter(DataTable tblName, string strSelectSql)
{
try
{
if (connection.State == 0)
{
createConn();
}
adapter.SelectCommand.CommandText = strSelectSql;
adapter.SelectCommand.CommandType = CommandType.Text;
SqlCommandBuilder DbCommandBuilder = new SqlCommandBuilder(adapter);
string insert = DbCommandBuilder.GetInsertCommand().CommandText.ToString();
string update = DbCommandBuilder.GetUpdateCommand().CommandText.ToString();
string delete = DbCommandBuilder.GetDeleteCommand().CommandText.ToString();
return adapter.Update(tblName);
}
catch (Exception ex)
{
throw ex;
}
}
public void readDatathroughAdapter(string query, DataTable tblName)
{
try
{
if (connection.State == ConnectionState.Closed)
{
createConn();
}
command.Connection = connection;
command.CommandText = query;
command.CommandType = CommandType.Text;
adapter = new SqlDataAdapter(command);
adapter.Fill(tblName);
}
catch (Exception ex)
{
throw ex;
}
}
public SqlDataReader readDatathroughReader(string query)
{
//DataReader used to sequentially read data from a data source
SqlDataReader reader;
try
{
if (connection.State == ConnectionState.Closed)
{
createConn();
}
command.Connection = connection;
command.CommandText = query;
command.CommandType = CommandType.Text;
reader = command.ExecuteReader();
return reader;
}
catch (Exception ex)
{
throw ex;
}
}
public int executeQuery(SqlCommand dbCommand)
{
try
{
if (connection.State == 0)
{
createConn();
}
dbCommand.Connection = connection;
dbCommand.CommandType = CommandType.Text;
return dbCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment