-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConnectionString.cs
167 lines (153 loc) · 5.69 KB
/
ConnectionString.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Collections;
namespace KITEPlacement
{
public class ConnectionString
{
private static SqlConnection sqlCon;
#region Connection
public static SqlConnection DBConnection()
{
SqlConnection ObjCon = new SqlConnection(ConfigurationManager.ConnectionStrings["dbConnection"].ToString());
return ObjCon;
}
#endregion
#region Method to Fetch Data using Query
public static DataSet SqlExecProc(string qry, out string errMsg)
{
string strException = "";
DataSet ds = new DataSet();
try
{
sqlCon = ConnectionString.DBConnection();
sqlCon.Open();
SqlDataAdapter SqlDA = new SqlDataAdapter(qry, sqlCon);
SqlDA.SelectCommand.CommandTimeout = 3600;
SqlDA.Fill(ds);
strException = "Successful";
}
catch (Exception ex)
{
strException = ex.ToString();
}
finally
{
sqlCon.Close();
sqlCon.Dispose();
}
errMsg = strException;
return ds;
}
#endregion
#region Method to Fetch Records in DataSet with Input Parameters
public static DataSet SqlExecFetchDataSet(string procName, ArrayList arrySPParam, ArrayList arrySPValue, out string errMsg)
{
string strException = "";
DataSet dsData = new DataSet();
try
{
sqlCon = ConnectionString.DBConnection();
sqlCon.Open();
SqlDataAdapter sqlda = new SqlDataAdapter(procName, sqlCon);
sqlda.SelectCommand.CommandType = CommandType.StoredProcedure;
sqlda.SelectCommand.CommandTimeout = 3600;
for (int i = 0; i < arrySPParam.Count; i++)
{
if (arrySPValue[i].ToString().Trim() == "")
sqlda.SelectCommand.Parameters.AddWithValue(arrySPParam[i].ToString(), DBNull.Value);
else
sqlda.SelectCommand.Parameters.AddWithValue(arrySPParam[i].ToString(), arrySPValue[i].ToString());
}
sqlda.Fill(dsData);
sqlCon.Close();
}
catch (Exception ex)
{
strException = ex.ToString();
}
finally
{
sqlCon.Close();
sqlCon.Dispose();
}
strException = "Successful";
errMsg = strException;
return dsData;
}
#endregion
#region Method to Insert Data
public static void SqlExecInsProc(string procName, ArrayList arrySPParam, ArrayList arrySPValue, out string errMsg)
{
string strException = "";
SqlTransaction sqlTran = null;
try
{
sqlCon = ConnectionString.DBConnection();
sqlCon.Open();
sqlTran = sqlCon.BeginTransaction("SqlTran1");
SqlCommand sqlCmd = new SqlCommand(procName, sqlCon, sqlTran);
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.CommandTimeout = 3600;
for (int i = 0; i < arrySPParam.Count; i++)
{
if (arrySPValue[i].ToString().Trim() == "")
sqlCmd.Parameters.AddWithValue(arrySPParam[i].ToString(), DBNull.Value);
else
sqlCmd.Parameters.AddWithValue(arrySPParam[i].ToString(), arrySPValue[i].ToString());
}
sqlCmd.ExecuteNonQuery();
strException = "Successful";
sqlTran.Commit();
}
catch(Exception ex)
{
strException = ex.ToString();
}
finally
{
if (sqlTran != null)
sqlTran.Dispose();
sqlCon.Close();
sqlCon.Dispose();
}
errMsg = strException;
}
#endregion
#region Bulk Insert
public static void FnSqlBulkInsert(string tableName, DataTable dataTable, out string errMsg)
{
string strException = "";
using (var connection = ConnectionString.DBConnection())
{
connection.Open();
using (SqlTransaction sqlTran = connection.BeginTransaction())
{
using (var bulkCopy = new SqlBulkCopy(connection, SqlBulkCopyOptions.Default, sqlTran))
{
bulkCopy.DestinationTableName = tableName;
try
{
bulkCopy.WriteToServer(dataTable);
sqlTran.Commit();
strException = "Successful";
}
catch (Exception ex)
{
sqlTran.Rollback();
strException = ex.ToString();
}
}
}
}
errMsg = strException;
}
#endregion
}
}