forked from bledeggalih/techie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnection.cs
47 lines (43 loc) · 1.32 KB
/
Connection.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
namespace Techie
{
public class Connection
{
SqlConnection DBConnection;
String ConnectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\MainData.mdf;Integrated Security=True";
public Connection()
{
DBConnection = new SqlConnection(ConnectionString);
}
public DataTable ExecuteQuery(string query)
{
if (DBConnection.State == ConnectionState.Open)
{
DBConnection.Close();
}
DBConnection.Open();
SqlCommand command = new SqlCommand(query, DBConnection);
SqlDataReader reader = command.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(reader);
DBConnection.Close();
return dt;
}
public void ExecuteNQuery(string query)
{
if (DBConnection.State == ConnectionState.Open)
{
DBConnection.Close();
}
DBConnection.Open();
SqlCommand command = new SqlCommand(query, DBConnection);
command.ExecuteNonQuery();
DBConnection.Close();
}
}
}