-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDataService.cs
78 lines (69 loc) · 2.51 KB
/
DataService.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
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using Microsoft.Data.SqlClient;
public class DataService
{
public DataService()
{
}
public static void ExecuteCommand(string query)
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = "<dbname>.database.windows.net";
builder.UserID = "<username>";
builder.Password = "<Password>";
builder.InitialCatalog = "sqlforopenai";
var connectionString = builder.ConnectionString;
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
using (SqlCommand command = new SqlCommand(query, connection))
{
command.ExecuteNonQuery();
}
}
}
public static List<List<string>> GetDataTable(string query)
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = "sqlforopenai.database.windows.net";
builder.UserID = "barut";
builder.Password = "Deneme!12345";
builder.InitialCatalog = "sqlforopenai";
var rows = new List<List<string>>();
var connectionString = builder.ConnectionString;
using (var connection = new SqlConnection(connectionString))
{
connection.Open();
using (var command = new SqlCommand(query, connection))
{
using (var reader = command.ExecuteReader())
{
bool headersAdded = false;
while (reader.Read())
{
var cols = new List<string>();
var headerCols = new List<string>();
if (!headersAdded)
{
for (var i = 0; i < reader.FieldCount; i++)
{
headerCols.Add(reader.GetName(i).ToString());
}
rows.Add(headerCols);
headersAdded = true;
}
var row = new List<string>();
for (var i = 0; i <= reader.FieldCount - 1; i++)
{
cols.Add(reader.GetValue(i).ToString());
}
rows.Add(cols);
}
}
}
}
return rows;
}
}