-
Notifications
You must be signed in to change notification settings - Fork 0
/
SqlFunctions.cs
435 lines (420 loc) · 12.2 KB
/
SqlFunctions.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.IO;
namespace DBTableMover
{
/// <summary>
/// Summary description for SqlFunctions.
/// </summary>
public class SqlFunctions
{
private string tableScript;
private string valueScript;
private DataSet dsTable = new DataSet();
private SqlConnection conDataConnection = new SqlConnection(frmMain.ConnectionString);
/// <summary>
/// main constructor
/// </summary>
public SqlFunctions()
{
//
// TODO: Add constructor logic here
//
}
/// <summary>
/// creates the actual script for the passed in table
/// </summary>
/// <param name="tableName">table to create a script for</param>
/// <returns>string</returns>
public string CreateTableScript(string tableName)
{
this.tableScript = "";
try
{
// declare variables for specific items
string columnName = "";
int seed = 0;
int inc = 0;
string columnType = "";
int columnLen = 0;
string columnNull = "";
string columnColl = "";
// start building script
dsTable.Tables.Clear();
SqlDataAdapter adap = new SqlDataAdapter();
SqlCommand comm = conDataConnection.CreateCommand();
comm.CommandType = CommandType.Text;
comm.CommandText = "exec sp_help " + tableName;
adap.SelectCommand = comm;
conDataConnection.Open();
adap.Fill(dsTable);
conDataConnection.Close();
// insert drop and create scripts
tableScript = "if exists(select name from sysobjects where name='" + tableName + "' and type='U')\r\nBEGIN\r\ndrop table " + tableName + "\r\nEND\r\nGO\r\n\r\n";
tableScript += "create table [dbo].[" + tableName + "]\r(\n";
// grab primary key column
string[] keyName = CheckForIdentity();
if(keyName!=null)
{
seed = Convert.ToInt32(dsTable.Tables[2].Rows[0]["Seed"].ToString());
inc = Convert.ToInt32(dsTable.Tables[2].Rows[0]["Increment"].ToString());
}
// grab column values
int cols = 0;
foreach(DataRow row in dsTable.Tables[1].Rows)
{
string columnOnly = "";
// there's more columns past the first then add this
if(cols >= 1)
columnOnly += ",\n";
columnName = row["Column_name"].ToString();
columnType = row["Type"].ToString();
columnLen = Convert.ToInt32(row["Length"].ToString());
if(row["Nullable"].ToString().ToUpper() == "YES")
columnNull = "NULL";
else
columnNull = "NOT NULL";
if(row["Collation"].ToString() == "NULL")
columnColl = "";
else
columnColl = row["Collation"].ToString();
// create the script for column
columnOnly += "[" + columnName + "] ";
columnOnly += "[" + columnType + "]";
switch(columnType)
{
case "varchar":
case "nchar":
case "nvarchar":
case "char":
case "binary":
case "varbinary":
{
if(columnLen==-1)
columnOnly += "(MAX)";
else
columnOnly += "(" + columnLen + ")";
break;
}
case "text":
case "ntext":
case "datetime":
case "int":
case "double":
case "single":
case "float":
case "money":
case "smallint":
case "tinyint":
case "uniqueidentifier":
case "bit":
case "xml":
case "bigint":
case "image":
case "numeric":
case "real":
case "smalldatetime":
case "smallmoney":
case "timestamp":
{
break;
}
}
// if it's the identity set it as one
if(CheckForKeyRow(keyName,columnName))
columnOnly += " IDENTITY(" + seed + "," + inc + ")";
if (columnColl == "")
columnOnly += " " + columnNull;
else
columnOnly += " COLLATE " + columnColl + " " + columnNull;
// add one to the number of columns
cols++;
// add column text to script
tableScript += columnOnly;
}
// now add the primary key if there is one
string PrimaryKeyClause = CheckForPrimaryKey(tableName);
if(PrimaryKeyClause.Length > 0)
tableScript += PrimaryKeyClause;
// now close the script for the table
tableScript += ") ON [PRIMARY]\r\nGO\r\n";
}
catch(Exception x)
{
WriteLog(x.Message);
}
return tableScript;
}
/// <summary>
/// creates insert scripts for each row in the passed in table
/// </summary>
/// <param name="tableName">name of the table to create scripts for</param>
/// <returns>string</returns>
public string CreateValueScript(string tableName)
{
this.valueScript = "";
try
{
this.dsTable.Tables.Clear();
SqlDataAdapter adap = new SqlDataAdapter();
SqlCommand comm = conDataConnection.CreateCommand();
comm.CommandType = CommandType.Text;
comm.CommandText = "exec sp_help " + tableName;
adap.SelectCommand = comm;
adap.Fill(dsTable);
// grab table identities column names
string[] keyrows = CheckForIdentity();
///now start creating the script for the values
this.dsTable.Tables.Clear();
comm = conDataConnection.CreateCommand();
comm.CommandType = CommandType.Text;
comm.CommandText = "select * from " + tableName;
adap.SelectCommand = comm;
adap.Fill(dsTable);
// check table for data
int rowCount = dsTable.Tables[0].Rows.Count;
if(rowCount==0)
{
MessageBox.Show("There is no data to script for.", "Empty Table...");
Exception x = new Exception("Empty Table");
throw(x);
}
// get table column name values
string columnNames = "";
DataRow columnRow = dsTable.Tables[0].Rows[0];
int c = 0;
foreach(DataColumn col in columnRow.Table.Columns)
{
if(CheckForKeyRow(keyrows, col.ColumnName.ToString()))
continue;
else
{
c++;
if(c==1)
columnNames += col.ColumnName.ToString();
else
columnNames += "," + col.ColumnName.ToString();
}
}
// now grab values and build SQL
foreach(DataRow row in dsTable.Tables[0].Rows)
{
int r = 0; // particular row column
valueScript += "INSERT INTO [dbo].[" + tableName + "] (" + columnNames + ") VALUES (";
int n = 0; // number of columns added to script
foreach(DataColumn col in row.Table.Columns)
{
if(CheckForKeyRow(keyrows, col.ColumnName.ToString()))
{
r++;
continue;
}
else
{
if(n>0)
valueScript += ",";
string colType = col.DataType.ToString();
// WriteLog("colType :" + colType);
// WriteLog("ToString : " + col.ToString());
// WriteLog(" \n");
if(Convert.IsDBNull(row[r]))
valueScript += "NULL";
else
{
#region dataType DataFixes
switch(colType)
{
case "System.DateTime":
{
DateTime dbdate = Convert.ToDateTime(row[r]);
valueScript += "'" + dbdate.ToString("yyyyMMdd HH:mm:ss") + "'";
break;
}
case "System.String":
case "System.Boolean":
case "System.Object":
{
string tempValue = row[r].ToString();
tempValue = tempValue.Replace("'", "''");
valueScript += "'" + tempValue.Trim() + "'";
break;
}
case "System.Int64":
{
valueScript += Convert.ToInt64(row[r]);
break;
}
case "System.Int32":
{
valueScript += Convert.ToInt32(row[r]);
break;
}
case "System.Int16":
{
valueScript += Convert.ToInt16(row[r]);
break;
}
case "System.Decimal":
{
valueScript += Convert.ToDecimal(row[r]);
break;
}
case "System.Double":
{
valueScript += Convert.ToDouble(row[r]);
break;
}
case "System.Single":
{
valueScript += Convert.ToSingle(row[r]);
break;
}
case "System.Byte":
{
valueScript += "'" + Convert.ToByte(row[r]) + "'";
break;
}
case "System.Byte[]":
{
valueScript += "0x";
string stringValue = Convert.ToString(row[r]);
foreach(char b in stringValue)
{
valueScript += Convert.ToByte(b);
}
break;
}
case "System.Guid":
{ // don't want a value here, since the insert will auto add one.
// really shouldn't have this value anyway, since the keyrow should handle this.
break;
}
}
#endregion
}
// add one to the count of values added to valueScript
n++;
}
// add one to the column count
r++;
}
valueScript += ")\r\n\r\nGO\r\n\r\n";
}
}
catch(Exception x)
{
WriteLog(x.Message);
}
return valueScript;
}
/// <summary>
/// checks for an identity column
/// </summary>
/// <returns>name(s) of identity column(s)</returns>
private string[] CheckForIdentity()
{
try
{
string test = dsTable.Tables[2].Rows[0][0].ToString();
if(test=="No identity column defined.")
return null;
int rowCount = dsTable.Tables[2].Rows.Count;
if(rowCount > 0 )
{
string[] keyrows = new string[dsTable.Tables[2].Rows.Count];
int x = 0;
foreach(DataRow row in dsTable.Tables[2].Rows)
{
// define the identity columns
keyrows[x] = row["Identity"].ToString();
x++;
}
return keyrows;
}
}
catch(Exception x)
{
WriteLog("CheckForIdentity :" + x.Message);
}
return null;
}
/// <summary>
/// checks for primary key column and concatenates a creation string for a Primary Key on an SQL Table
/// </summary>
/// <param name="tableName">name of the table to check for a primary key</param>
/// <returns>string</returns>
private string CheckForPrimaryKey(string tableName)
{
string returnString = "";
// number of rows
int rowCount = dsTable.Tables[6].Rows.Count;
if(rowCount > 0)
{
// now cycle through and check for "PRIMARY KEY"
foreach(DataRow row in dsTable.Tables[6].Rows)
{
//WriteLog(row["constraint_type"].ToString() + " Value : " + Convert.ToInt32(row["constraint_type"].ToString().IndexOf("RIMARY KEY",0)));
if(row["constraint_type"].ToString().IndexOf("RIMARY KEY",0) == 1)
{
//grab all values and create primary key string
returnString += ",\r\nCONSTRAINT [" + row[1].ToString() + "] PRIMARY KEY CLUSTERED\r\n(";
// check to see if more than one field is in key
string[] columns = row[6].ToString().Split(',');
if(columns.Length>1)
{
string columnNames = "";
int count = 1;
foreach(string s in columns)
{
if(count == columns.Length)
columnNames += "[" + s + "]";
else
columnNames += "[" + s +"],";
count++;
}
returnString += columnNames + " ASC\r\n";
}
else
returnString += "[" + row[6].ToString() + "] ASC\r\n";
returnString += ") ON [PRIMARY]\r\n";
}
}
return returnString;
}
else
return "";
}
// TODO: find a solution for multi column Primary Keys...
/// <summary>
/// checks that the passed in columnName does not exist in keys, so it is not an indentifier.
/// Helps when the ID column (PrimaryKey) is a generated column
/// Doesn't help when the PrimaryKey is multi column....
/// </summary>
/// <param name="keys">a list of the Primary Key column names</param>
/// <param name="columnName">name of the current column being scripted for</param>
/// <returns></returns>
private bool CheckForKeyRow(string[] keys, string columnName)
{
if(keys!=null)
{
foreach(string f in keys)
{
if(f.ToString() == columnName.ToString())
return true;
}
}
return false;
}
/// <summary>
/// this is an internal function to write debug information to a textfile
/// if the cmdline option /debug is used.
/// </summary>
/// <param name="message"></param>
private void WriteLog(string message)
{
ProjectMethods.WriteLog("SqlFunctions", message);
}
}
}