hytos / DTI_PID / ID2PSN / AppMSSqlDatabase.cs @ 48870200
이력 | 보기 | 이력해설 | 다운로드 (5.45 KB)
1 |
using System; |
---|---|
2 |
using System.Collections.Generic; |
3 |
using System.Linq; |
4 |
using System.Web; |
5 |
using System.Data; |
6 |
using System.Data.Common; |
7 |
using System.Data.SqlClient; |
8 |
|
9 |
/*------------------------------------------------------------------------------- |
10 |
* |
11 |
* ***************** Version 1 ***************** |
12 |
* author: HumKyung Baek Date: 2014.09.29 - access MSSql |
13 |
--------------------------------------------------------------------------------*/ |
14 |
|
15 |
namespace ID2PSN |
16 |
{ |
17 |
public class AppMSSqlDatabase : AbstractDatabase<SqlConnection, SqlCommand, SqlDataAdapter> |
18 |
{ |
19 |
private const string oConnString = "server={0};uid={1};pwd={2};database={3}"; |
20 |
|
21 |
/// <summary> |
22 |
/// 호스트 |
23 |
/// </summary> |
24 |
public string Host { get; set; } |
25 |
|
26 |
/// <summary> |
27 |
/// 아이디 |
28 |
/// </summary> |
29 |
public string Id { get; set; } |
30 |
|
31 |
/// <summary> |
32 |
/// 암호 |
33 |
/// </summary> |
34 |
public string Password { get; set; } |
35 |
|
36 |
/// <summary> |
37 |
/// 데이타베이스 |
38 |
/// </summary> |
39 |
public string Database { get; set; } |
40 |
|
41 |
/// <summary> |
42 |
/// connection string |
43 |
/// </summary> |
44 |
public string ConnectionString { get; set; } |
45 |
|
46 |
/// 15.06.23 added by soohyun |
47 |
public const int WINDOWS_Authen = 0; |
48 |
public const int SERVER_Authen = 1; |
49 |
private static int SqlAuthentication_; |
50 |
|
51 |
public int SqlAuthentication |
52 |
{ |
53 |
set { SqlAuthentication_ = value; } |
54 |
} |
55 |
// up to here |
56 |
|
57 |
/// <summary> |
58 |
/// return connect string to MSSql |
59 |
/// </summary> |
60 |
/// <author>humkyung</author> |
61 |
/// <date>2014.09.29</date> |
62 |
/// <returns>Connection string</returns> |
63 |
protected override string GetConnectionString() |
64 |
{ |
65 |
string connString = this.ConnectionString; |
66 |
|
67 |
if (string.IsNullOrEmpty(this.ConnectionString)) |
68 |
{ |
69 |
if (SERVER_Authen == SqlAuthentication_) |
70 |
{ |
71 |
connString = string.Format(oConnString, Host, Id, Password, Database); |
72 |
} |
73 |
else if (WINDOWS_Authen == SqlAuthentication_) |
74 |
{ |
75 |
connString = string.Format("Data Source={0};Database={1};Integrated Security=SSPI", Host, Database); |
76 |
} |
77 |
} |
78 |
|
79 |
return connString; |
80 |
} |
81 |
|
82 |
/// <summary> |
83 |
/// get table name list |
84 |
/// </summary> |
85 |
/// <author>humkyung</author> |
86 |
/// <date>2014.09.29</date> |
87 |
/// <param name="sTableName"></param> |
88 |
/// <returns></returns> |
89 |
public override List<string> GetTableNames() |
90 |
{ |
91 |
List<string> res = new List<string> { }; |
92 |
{ |
93 |
string query = "SELECT NAME FROM SYS.TABLES"; |
94 |
using (var ds = ExecuteDataSet(GetSqlStringCommand(query))) |
95 |
{ |
96 |
foreach (DataRow dr in ds.Tables[0].Rows) |
97 |
{ |
98 |
res.Add(dr["NAME"].ToString()); |
99 |
} |
100 |
} |
101 |
} |
102 |
|
103 |
return res; |
104 |
} |
105 |
|
106 |
public override List<string> GetColumnNames(string TableName) |
107 |
{ |
108 |
List<string> res = new List<string> { }; |
109 |
{ |
110 |
string query = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '" + TableName + "'"; |
111 |
using (var ds = ExecuteDataSet(GetSqlStringCommand(query))) |
112 |
{ |
113 |
foreach (DataRow dr in ds.Tables[0].Rows) |
114 |
{ |
115 |
res.Add(dr["COLUMN_NAME"].ToString()); |
116 |
} |
117 |
} |
118 |
} |
119 |
|
120 |
return res; |
121 |
} |
122 |
|
123 |
/// <summary> |
124 |
/// get column list of table |
125 |
/// </summary> |
126 |
/// <author>humkyung</author> |
127 |
/// <date>2012.07.17</date> |
128 |
/// <param name="oColumnList"></param> |
129 |
/// <param name="sTableName"></param> |
130 |
/// <returns></returns> |
131 |
public List<ColInfo> GetColumnInfoListOf(string sTableName) |
132 |
{ |
133 |
List<ColInfo> res = new List<ColInfo> { }; |
134 |
|
135 |
string sSql = string.Format( |
136 |
@"SELECT c.name 'Column Name', t.Name 'Data type' |
137 |
FROM sys.columns c |
138 |
INNER JOIN |
139 |
sys.types t ON c.user_type_id = t.user_type_id |
140 |
LEFT OUTER JOIN |
141 |
sys.index_columns ic ON ic.object_id = c.object_id AND ic.column_id = c.column_id |
142 |
LEFT OUTER JOIN |
143 |
sys.indexes i ON ic.object_id = i.object_id AND ic.index_id = i.index_id |
144 |
WHERE |
145 |
c.object_id = OBJECT_ID('{0}')", sTableName); |
146 |
|
147 |
using (DbDataReader reader = ExecuteReader(GetSqlStringCommand(sSql))) |
148 |
{ |
149 |
while (reader.Read()) |
150 |
{ |
151 |
res.Add(new ColInfo() { Name = reader.GetString(0), DataType = reader.GetString(1) }); |
152 |
} |
153 |
} |
154 |
|
155 |
return res; |
156 |
} |
157 |
|
158 |
/// <summary> |
159 |
/// execute non-query |
160 |
/// </summary> |
161 |
/// <author>humkyung</author> |
162 |
/// <date>2014.09.29</date> |
163 |
/// <param name="sSql"></param> |
164 |
/// <returns>int</returns> |
165 |
public new int ExecuteNonQuery(string sSql) |
166 |
{ |
167 |
DbCommand cmd = GetSqlStringCommand(sSql); |
168 |
return cmd.ExecuteNonQuery(); |
169 |
} |
170 |
} |
171 |
} |