hytos / DTI_PID / ID2PSN / AppMSSqlDatabase.cs @ f5c168dd
이력 | 보기 | 이력해설 | 다운로드 (6.25 KB)
1 | 23a96301 | humkyung | 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 | 1d46fca7 | LJIYEON | public override List<string> GetCommonTableNames() |
107 | { |
||
108 | List<string> res = new List<string> { }; |
||
109 | { |
||
110 | string query = "SELECT T.name AS table_name, C.name AS column_name" + |
||
111 | "FROM ARS_COMMON.sys.tables AS T INNER JOIN ARS_COMMON.sys.columns AS C ON T.object_id = C.object_id WHERE T.name = 'PSN_REVISION'"; |
||
112 | using (var ds = ExecuteDataSet(GetSqlStringCommand(query))) |
||
113 | { |
||
114 | foreach (DataRow dr in ds.Tables[0].Rows) |
||
115 | { |
||
116 | res.Add(dr["column_name"].ToString()); |
||
117 | } |
||
118 | } |
||
119 | } |
||
120 | |||
121 | return res; |
||
122 | } |
||
123 | |||
124 | 0ff2a9f1 | LJIYEON | public override List<string> GetColumnNames(string TableName) |
125 | { |
||
126 | List<string> res = new List<string> { }; |
||
127 | { |
||
128 | b56d3ccb | LJIYEON | //string query = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '" + TableName + "'"; |
129 | string query = "select name from dbo.syscolumns where id = object_id('" + TableName + "')"; |
||
130 | 0ff2a9f1 | LJIYEON | using (var ds = ExecuteDataSet(GetSqlStringCommand(query))) |
131 | { |
||
132 | foreach (DataRow dr in ds.Tables[0].Rows) |
||
133 | { |
||
134 | b56d3ccb | LJIYEON | res.Add(dr["name"].ToString()); |
135 | 0ff2a9f1 | LJIYEON | } |
136 | } |
||
137 | } |
||
138 | |||
139 | return res; |
||
140 | } |
||
141 | |||
142 | 23a96301 | humkyung | /// <summary> |
143 | /// get column list of table |
||
144 | /// </summary> |
||
145 | /// <author>humkyung</author> |
||
146 | /// <date>2012.07.17</date> |
||
147 | /// <param name="oColumnList"></param> |
||
148 | /// <param name="sTableName"></param> |
||
149 | /// <returns></returns> |
||
150 | public List<ColInfo> GetColumnInfoListOf(string sTableName) |
||
151 | { |
||
152 | List<ColInfo> res = new List<ColInfo> { }; |
||
153 | |||
154 | string sSql = string.Format( |
||
155 | @"SELECT c.name 'Column Name', t.Name 'Data type' |
||
156 | FROM sys.columns c |
||
157 | INNER JOIN |
||
158 | sys.types t ON c.user_type_id = t.user_type_id |
||
159 | LEFT OUTER JOIN |
||
160 | sys.index_columns ic ON ic.object_id = c.object_id AND ic.column_id = c.column_id |
||
161 | LEFT OUTER JOIN |
||
162 | sys.indexes i ON ic.object_id = i.object_id AND ic.index_id = i.index_id |
||
163 | WHERE |
||
164 | c.object_id = OBJECT_ID('{0}')", sTableName); |
||
165 | |||
166 | using (DbDataReader reader = ExecuteReader(GetSqlStringCommand(sSql))) |
||
167 | { |
||
168 | while (reader.Read()) |
||
169 | { |
||
170 | res.Add(new ColInfo() { Name = reader.GetString(0), DataType = reader.GetString(1) }); |
||
171 | } |
||
172 | } |
||
173 | |||
174 | return res; |
||
175 | } |
||
176 | |||
177 | /// <summary> |
||
178 | /// execute non-query |
||
179 | /// </summary> |
||
180 | /// <author>humkyung</author> |
||
181 | /// <date>2014.09.29</date> |
||
182 | /// <param name="sSql"></param> |
||
183 | /// <returns>int</returns> |
||
184 | public new int ExecuteNonQuery(string sSql) |
||
185 | { |
||
186 | DbCommand cmd = GetSqlStringCommand(sSql); |
||
187 | return cmd.ExecuteNonQuery(); |
||
188 | } |
||
189 | } |
||
190 | } |