-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLManager.cs
More file actions
67 lines (62 loc) · 2.19 KB
/
SQLManager.cs
File metadata and controls
67 lines (62 loc) · 2.19 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Threading;
using System.Collections;
using System.IO;
using System.Runtime.InteropServices;
namespace KXTX.IT.BICenter
{
class SQLManager
{
public static DataTable ExecuteQuery(string connectionName, string queryName, params SqlParameter[] parameters)
{
return ExcuteQuery(connectionName, queryName, CommandType.Text, parameters);
}
public static DataTable ExecuteProc(string connectionName, string queryName, params SqlParameter[] parameters)
{
return ExcuteQuery(connectionName, queryName, CommandType.StoredProcedure, parameters);
}
private static DataTable ExcuteQuery(string connectionName, string queryName, CommandType commandType, params SqlParameter[] parameters)
{
SqlCommand cmd = new SqlCommand();
cmd.CommandType = commandType;
cmd.CommandText = queryName;
SqlConnection con = new SqlConnection(connectionName);
DataTable table = new DataTable();
AddParameters(cmd, parameters);
try
{
cmd.Connection = con;
//con.Open();
//n = cmd.ExecuteScalar();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(table);
}
catch (Exception ex)
{
LogManager.AppendLog(DateTime.Now.ToString() + " Execute Query Failed");
LogManager.AppendLog("Error Message: Execute Query Failed, " + ex.Message);
Console.WriteLine(ex.Message);
throw new Exception("Execute Query Failed", ex);
}
finally
{
con.Close();
}
return table;
}
private static void AddParameters(SqlCommand cmd, SqlParameter[] parameters)
{
for (int i = 0; i < parameters.Length; i++)
{
cmd.Parameters.Add(parameters[i]);
}
}
}
}