-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConnection.cs
84 lines (75 loc) · 2.53 KB
/
Connection.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
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
namespace dbscript
{
public class Connection
{
public string serverName;
public string username;
public string password;
public bool connectionOK;
public string connectionError = "";
public Connection(string svrName, string usrName, string pssWord)
{
serverName = svrName;
username = usrName;
password = pssWord;
}
public string connectionString()
{
string connstr = "Data Source=" + serverName
+ "; User Id=" + username
+ "; Password=" + password;
return connstr;
}
public bool testConnection()
{
Console.WriteLine("\nUsing connection string: " + connectionString());
Server srvr = server();
try
{
srvr.Initialize();
string srvrVersion = srvr.Information.VersionString;
Console.WriteLine("Connection to Server " + serverName.ToUpper() + " is OK");
Console.WriteLine("Server Version is " + srvrVersion + "\n");
return true;
}
catch (Exception e)
{
Console.WriteLine("\nERROR: Connection to Server " + serverName.ToUpper() + " failed\n");
connectionError = e.InnerException.ToString();
return false;
}
}
public Database database(string dbName)
{
Database db = new Database(server(), dbName);
return db;
}
public Server server()
{
ServerConnection conn = serverConnection();
Server srvr = new Server(conn);
return srvr;
}
public Server server(string database)
{
ServerConnection conn = serverConnection();
conn.DatabaseName = database;
Server srvr = new Server(conn);
return srvr;
}
public ServerConnection serverConnection()
{
ServerConnection conn = new ServerConnection();
conn.LoginSecure = false;
conn.Login = username;
conn.Password = password;
conn.ServerInstance = serverName;
return conn;
}
}
}