-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseHelper.cs
More file actions
45 lines (40 loc) · 1.53 KB
/
Copy pathDatabaseHelper.cs
File metadata and controls
45 lines (40 loc) · 1.53 KB
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
using System;
using System.Data;
using System.Data.SqlClient;
namespace H1
{
public static class DatabaseHelper
{
// Replace with your actual connection string
private static string connectionString = @"Data Source=ABDULSABOOR190\SQLEXPRESS;Initial Catalog=sab;Integrated Security=True;Encrypt=False";
public static DataTable ExecuteQuery(string query, SqlParameter[] parameters = null)
{
DataTable dataTable = new DataTable();
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand command = new SqlCommand(query, connection))
{
if (parameters != null)
command.Parameters.AddRange(parameters);
connection.Open();
SqlDataAdapter adapter = new SqlDataAdapter(command);
adapter.Fill(dataTable);
}
}
return dataTable;
}
public static int ExecuteNonQuery(string query, SqlParameter[] parameters = null)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand command = new SqlCommand(query, connection))
{
if (parameters != null)
command.Parameters.AddRange(parameters);
connection.Open();
return command.ExecuteNonQuery();
}
}
}
}
}