-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDBAccess.cs
144 lines (116 loc) · 3.71 KB
/
DBAccess.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
namespace CafeCodeApp
{
public 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;
}
}
}
}