@@ -21,87 +21,56 @@ public Connection()
2121 }
2222
2323 /// <summary>
24- /// Initializes a new instance of the <see cref="Connection"/> class with the specified connection parameters.
24+ /// Initializes a new instance of the <see cref="Connection"/> class with the specified parameters.
2525 /// </summary>
26- /// <param name="dataSource ">The data source or IP address .</param>
27- /// <param name="initialCatalog">The initial catalog or database name .</param>
28- /// <param name="integratedSecurity">Indicates whether to use integrated security .</param>
29- /// <param name="userID ">The user ID for SQL authentication.</param>
30- /// <param name="password ">The password for SQL authentication.</param>
31- public Connection ( string dataSource , string ? initialCatalog , bool integratedSecurity , string ? userID = "" , string ? password = "" )
26+ /// <param name="serverAddress ">The address of the server .</param>
27+ /// <param name="useSQLAuthentication">Indicates whether to use SQL authentication .</param>
28+ /// <param name="databaseName">The name of the database .</param>
29+ /// <param name="sqlUsername ">The username for SQL authentication.</param>
30+ /// <param name="sqlPassword ">The password for SQL authentication.</param>
31+ public Connection ( string serverAddress , bool useSQLAuthentication , string ? databaseName = null , string ? sqlUsername = null , string ? sqlPassword = null )
3232 {
33- DataSource = dataSource ;
34- InitialCatalog = initialCatalog ;
35- IntegratedSecurity = integratedSecurity ;
36- UserID = userID ;
37- Password = password ;
38- }
33+ Server = serverAddress ;
34+ UseSQLAuthentication = useSQLAuthentication ;
3935
36+ if ( databaseName != null )
37+ DatabaseName = databaseName ;
4038
41- /// <summary>
42- /// Initializes a new instance of the <see cref="Connection"/> class with the specified connection parameters.
43- /// </summary>
44- /// <param name="server">The server address.</param>
45- /// <param name="database">The database name.</param>
46- /// <param name="initialCatalog">The initial catalog or database name.</param>
47- /// <param name="integratedSecurity">Indicates whether to use integrated security.</param>
48- /// <param name="userID">The user ID for SQL authentication.</param>
49- /// <param name="password">The password for SQL authentication.</param>
50- /// <param name="useServerAddress">Indicates whether to use the server address.</param>
51- public Connection ( string server , string ? database , string ? initialCatalog , bool integratedSecurity , string ? userID , string ? password , bool useServerAddress )
52- {
53- UseServerAddress = useServerAddress ;
54- Server = server ;
55- Database = database ;
56- IntegratedSecurity = integratedSecurity ;
57- InitialCatalog = initialCatalog ;
58- UserID = userID ;
59- Password = password ;
39+ if ( useSQLAuthentication )
40+ {
41+ SQLUsername = sqlUsername ;
42+ SQLPassword = sqlPassword ;
43+ }
6044 }
6145
6246 #endregion
6347
6448
6549 #region Properties
6650 /// <summary>
67- /// Gets or sets a value indicating whether to use the server address.
68- /// </summary>
69- public bool UseServerAddress { get ; set ; }
70-
71- /// <summary>
72- /// Gets or sets the data source or IP address.
73- /// </summary>
74- public string ? DataSource { get ; set ; }
75-
76- /// <summary>
77- /// Gets or sets the server address.
78- /// </summary>
79- public string ? Server { get ; set ; }
80-
81- /// <summary>
82- /// Gets or sets the database name.
51+ /// Gets or sets the address of the server.
8352 /// </summary>
84- public string ? Database { get ; set ; }
53+ private string Server { get ; set ; }
8554
8655 /// <summary>
87- /// Gets or sets the initial catalog or database name .
56+ /// Gets or sets a value indicating whether to use SQL authentication .
8857 /// </summary>
89- public string ? InitialCatalog { get ; set ; }
58+ private bool UseSQLAuthentication { get ; set ; }
9059
9160 /// <summary>
92- /// Gets or sets the user ID for SQL authentication.
61+ /// Gets or sets the username for SQL authentication.
9362 /// </summary>
94- public string ? UserID { get ; set ; }
63+ private string ? SQLUsername { get ; set ; }
9564
9665 /// <summary>
9766 /// Gets or sets the password for SQL authentication.
9867 /// </summary>
99- public string ? Password { get ; set ; }
68+ private string ? SQLPassword { get ; set ; }
10069
10170 /// <summary>
102- /// Gets or sets a value indicating whether to use integrated security .
71+ /// Gets or sets the name of the database .
10372 /// </summary>
104- public bool IntegratedSecurity { get ; set ; }
73+ private string ? DatabaseName { get ; set ; }
10574
10675 /// <summary>
10776 /// Gets the database provider type, which is MSSQL for this class.
@@ -148,58 +117,24 @@ public bool TestConnection()
148117 /// <returns>The connection string.</returns>
149118 public string GetConnectionString ( )
150119 {
151- var connectionStringBuilder = new SqlConnectionStringBuilder ( ) ;
120+ SqlConnectionStringBuilder sqlConnectionStringBuilder = new ( ) ;
121+ sqlConnectionStringBuilder . DataSource = Server ;
122+ sqlConnectionStringBuilder . IntegratedSecurity = ! UseSQLAuthentication ;
152123
153- if ( UseServerAddress )
154- {
155- if ( Server == null )
156- {
157- throw new Exception ( "Server cannot be null when connecting through server address." ) ;
158- }
124+ if ( DatabaseName != null )
125+ sqlConnectionStringBuilder . InitialCatalog = DatabaseName ;
159126
160- connectionStringBuilder . DataSource = Server ;
161-
162- if ( Database != null )
163- {
164- connectionStringBuilder . InitialCatalog = Database ;
165- }
166- }
167- else
127+ if ( UseSQLAuthentication )
168128 {
169- if ( DataSource == null )
170- {
171- throw new Exception ( "DataSource cannot be null" ) ;
172- }
173-
174- connectionStringBuilder . DataSource = DataSource ;
175-
176- if ( InitialCatalog != null )
177- {
178- connectionStringBuilder . InitialCatalog = InitialCatalog ;
179- }
129+ sqlConnectionStringBuilder . UserID = SQLUsername ;
130+ sqlConnectionStringBuilder . Password = SQLPassword ;
180131 }
181132
182- connectionStringBuilder . IntegratedSecurity = IntegratedSecurity ;
133+ sqlConnectionStringBuilder . TrustServerCertificate = true ;
183134
184- if ( ! IntegratedSecurity )
185- {
186- if ( UserID == null )
187- {
188- throw new Exception ( "UserID cannot be null when logging in using SQL Authentication." ) ;
189- }
190-
191- connectionStringBuilder . UserID = UserID ;
192-
193- if ( Password == null )
194- {
195- throw new Exception ( "Password cannot be null when logging in using SQL Authentication." ) ;
196- }
197-
198- connectionStringBuilder . Password = Password ;
199- }
200-
201- return connectionStringBuilder . ConnectionString ;
135+ return sqlConnectionStringBuilder . ConnectionString ;
202136 }
137+
203138 #endregion
204139
205140 #region Static Method
0 commit comments