-
Notifications
You must be signed in to change notification settings - Fork 1
/
ExportSql.cs
139 lines (112 loc) · 4.16 KB
/
ExportSql.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
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.IO;
using System.Text;
using Microsoft.SqlServer.Server;
namespace ExportSql
{
public class StoredProcedures
{
[SqlProcedure]
public static void Sql2Csv (SqlString sql, SqlString filePath, SqlString fileName,
SqlInt32 includeHeader, SqlString delimeter, SqlInt32 useQuoteIdentifier, SqlInt32 overWriteExisting, SqlString encoding)
{
// Put your code here
filePath = FormatPath(filePath.ToString());
fileName = FormatFileName(fileName.ToString());
var fileNameWithPath = filePath + fileName;
var sqlConnection = new SqlConnection("context connection=true");
try
{
sqlConnection.Open();
var command = new SqlCommand(sql.ToString(), sqlConnection);
var reader = command.ExecuteReader();
var dt = new DataTable("Results");
dt.Load(reader);
DataTableToFile(dt, delimeter.ToString(), includeHeader, (useQuoteIdentifier.Value == 1),
fileNameWithPath.ToString(), overWriteExisting.Value == 1, encoding.ToString());
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
private static string FormatPath(string filePath)
{
if (Right(filePath, 1) == @"\")
{
filePath = filePath.Trim();
}
else
{
filePath = filePath.ToString().Trim() + @"\";
}
return filePath;
}
private static string FormatFileName(string fileName)
{
if (fileName.Substring(0, 1) == @"\")
{
return fileName.Substring(1).Trim();
}
return fileName.Trim();
}
private static string Right(string value, int length)
{
if (String.IsNullOrEmpty(value)) return string.Empty;
return value.Substring(value.Length - length);
}
private static void DataTableToFile(DataTable dt, string delimiter, SqlInt32 includeHeaders, bool withQuotedIdentifiers,
string fileName, bool overWrite = true, string encoding="")
{
var result = "";
var columnNames = new List<string>();
var encode = Encoding.Default;
var isCodePage = int.TryParse(encoding, out var codePage);
if(isCodePage)
encode = Encoding.GetEncoding(codePage);
else if (encoding != "")
encode = Encoding.GetEncoding(encoding);
var sw = new StreamWriter(new FileStream(fileName, overWrite ? FileMode.Create : FileMode.Append, FileAccess.ReadWrite), encode);
foreach (DataColumn col in dt.Columns)
{
if (withQuotedIdentifiers)
columnNames.Add("\"" + col.ColumnName + "\"");
else
columnNames.Add(col.ColumnName);
}
if (includeHeaders==1)
{
result = string.Join(delimiter, columnNames.ToArray());
sw.WriteLine(result);
}
foreach (DataRow row in dt.Rows)
{
var rowValues = new List<string>();
foreach (DataColumn column in dt.Columns)
{
if(withQuotedIdentifiers)
rowValues.Add("\"" + GetString(row[column]) + "\"");
else
rowValues.Add(GetString(row[column]));
}
result = string.Join(delimiter, rowValues.ToArray());
sw.WriteLine(result);
}
sw.Close();
sw.Dispose();
}
private static string GetString(object objValue)
{
if (objValue == null || Convert.IsDBNull(objValue))
{
return "";
}
return objValue.ToString();
}
}
}