-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSqlServerUtils.cs
37 lines (34 loc) · 1.28 KB
/
SqlServerUtils.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
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Data.SqlClient;
namespace Arcane.Framework.Sources.Extensions;
/// <summary>
/// Utilities for SQL Server database operations.
/// </summary>
internal static class SqlServerUtils
{
/// <summary>
/// Get all columns for the specified table.
/// </summary>
/// <param name="schema">Schema name</param>
/// <param name="table">Table name</param>
/// <param name="sqlConnection">SQL connection to use</param>
/// <returns>Column name and True if it is a primary key</returns>
public static IEnumerable<(string columnName, bool isPrimaryKey)> GetColumns(string schema, string table,
SqlConnection sqlConnection)
{
var getColumnsSql = File
.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Sources", "SqlServer", "SqlSnippets",
"GetColumns.sql"))
.Replace("{dbName}", sqlConnection.Database)
.Replace("{schema}", schema)
.Replace("{table}", table);
var command = new SqlCommand(getColumnsSql, sqlConnection);
using var reader = command.ExecuteReader();
while (reader.Read())
{
yield return (reader.GetString(0), reader.GetInt32(1) == 1);
}
}
}