-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
57 lines (42 loc) · 1.31 KB
/
Program.cs
File metadata and controls
57 lines (42 loc) · 1.31 KB
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
using System.Data.SqlClient;
RunProgram();
void RunProgram()
{
Console.Write("Connection string: ");
string connectionString = Console.ReadLine();
SqlConnection connection = new SqlConnection(connectionString);
try {
//open the connection
connection.Open();
Console.Write("Table name (enter to skip): ");
string tableName = Console.ReadLine();
if (string.IsNullOrEmpty(tableName))
{
return;
}
string sql = $"SELECT TOP 1 * FROM {tableName}";
SqlCommand command = new SqlCommand(sql, connection);
//execute the command
SqlDataReader reader = command.ExecuteReader();
//read the results
while (reader.Read())
{
// print number of columns
Console.WriteLine("\nNumber of columns: {0}\n\n", reader.FieldCount);
// print column names
for (int i = 0; i < reader.FieldCount; i++)
{
Console.WriteLine("{0}: {1}", reader.GetName(i), reader[i]);
}
}
}
catch (Exception e)
{
Console.WriteLine($"\n\n\n=====================\n\n >>> {e.Message}\n\n=====================\n\n");
}
finally
{
// close the connection
connection.Close();
}
}