public static void ConnectionStringPersistantInfoTest() { SqlConnectionStringBuilder connectionStringBuilder = new SqlConnectionStringBuilder(DataTestUtility.TCPConnectionString); connectionStringBuilder.PersistSecurityInfo = false; string cnnString = connectionStringBuilder.ConnectionString; connectionStringBuilder.Clear(); using (SqlConnection sqlCnn = new SqlConnection(cnnString)) { sqlCnn.Open(); connectionStringBuilder.ConnectionString = sqlCnn.ConnectionString; Assert.True(connectionStringBuilder.Password == string.Empty, "Password must not persist according to set the PersistSecurityInfo by false!"); } connectionStringBuilder.ConnectionString = DataTestUtility.TCPConnectionString; connectionStringBuilder.PersistSecurityInfo = true; cnnString = connectionStringBuilder.ConnectionString; connectionStringBuilder.Clear(); using (SqlConnection sqlCnn = new SqlConnection(cnnString)) { sqlCnn.Open(); connectionStringBuilder.ConnectionString = sqlCnn.ConnectionString; Assert.True(connectionStringBuilder.Password != string.Empty, "Password must persist according to set the PersistSecurityInfo by true!"); } }
public void TestSqlConnectionStringBuilderEnclaveAttestationUrl() { SqlConnectionStringBuilder connectionStringBuilder = new SqlConnectionStringBuilder(); Assert.Equal(string.Empty, connectionStringBuilder.EnclaveAttestationUrl); connectionStringBuilder.DataSource = @"localhost"; // Create a connection object with the above builder and verify the expected value. VerifyEnclaveAttestationUrlSetting(connectionStringBuilder, ""); SqlConnectionStringBuilder connectionStringBuilder2 = new SqlConnectionStringBuilder(); connectionStringBuilder2.EnclaveAttestationUrl = "www.foo.com"; Assert.Equal("www.foo.com", connectionStringBuilder2.EnclaveAttestationUrl); connectionStringBuilder2.DataSource = @"localhost"; // Create a connection object with the above builder and verify the expected value. VerifyEnclaveAttestationUrlSetting(connectionStringBuilder2, "www.foo.com"); connectionStringBuilder2.Clear(); Assert.Equal(SqlConnectionAttestationProtocol.NotSpecified, connectionStringBuilder2.AttestationProtocol); Assert.Equal(string.Empty, connectionStringBuilder2.EnclaveAttestationUrl); Assert.True(string.IsNullOrEmpty(connectionStringBuilder2.DataSource)); }
private static SqlCommand FxSqlConnection(string pDatabase) { SqlConnection ObjSqlConnection = new SqlConnection(); SqlCommand ObjSqlCommand = new SqlCommand(); SqlConnectionStringBuilder ObjSqlConnectionString = FxSqlConnectionString(pDatabase); ObjSqlConnection.ConnectionString = ObjSqlConnectionString.ConnectionString; ObjSqlCommand.Connection = ObjSqlConnection; try { ObjSqlConnection.Open(); } catch { ObjSqlCommand = null; ClsFunctions.FxMessage(1, "No pudo conectarse con el servidor"); ClsFunctions.FxExit(); } ObjSqlConnectionString.Clear(); return(ObjSqlCommand); }
protected virtual void Dispose(bool disposing) { if (disposed) { return; } if (disposing) { if (builder != null) { builder.Clear(); builder = null; } if (linq != null) { linq.Dispose(); linq = null; } if (Host != null) { Host = null; } } disposed = true; }
private int DetermineConnection(string serverName, string databaseName) { connectionStringBuilder.Clear(); connectionStringBuilder.DataSource = serverName; connectionStringBuilder.InitialCatalog = databaseName; int objectConnectionID = repository.GetConnection(connectionStringBuilder.ConnectionString); if (objectConnectionID == -1) { // Need to add a new connectionID. objectConnectionID = repository.AddObject("CMD " + connectionStringBuilder.InitialCatalog, string.Empty, Repository.OLEDBGuid, repository.RootRepositoryObjectID); repository.AddAttribute(objectConnectionID, Repository.Attributes.ConnectionString, connectionStringBuilder.ConnectionString); repository.AddAttribute(objectConnectionID, Repository.Attributes.ConnectionServer, connectionStringBuilder.DataSource); repository.AddAttribute(objectConnectionID, Repository.Attributes.ConnectionDatabase, connectionStringBuilder.InitialCatalog); } return(objectConnectionID); }
private void btnTestConnection_Click(object sender, EventArgs e) { if (!string.IsNullOrEmpty(txtServer.Text)) { connBuilder.DataSource = txtServer.Text; connBuilder.PersistSecurityInfo = true; if (!radWindows.Checked) { connBuilder.UserID = txtUsername.Text; connBuilder.Password = txtPassword.Text; } else { connBuilder.IntegratedSecurity = true; } try { using (SqlConnection dbConnection = new SqlConnection(connBuilder.ConnectionString)) { dbConnection.Open(); DataTable tempDataTable = dbConnection.GetSchema(SqlClientMetaDataCollectionNames.Databases); cmbDatabase.DataSource = tempDataTable; cmbDatabase.DisplayMember = tempDataTable.Columns["database_name"].ColumnName; cmbDatabase.ValueMember = tempDataTable.Columns["database_name"].ColumnName; // set the connection string to FrmNewProject's DataBase object ((FrmNewProject)this.ParentForm).ConnBuilder = connBuilder; // MessageBox.Show("Connected successfully!", "Connected", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); dbConnection.Close(); } sucess = true; cmbDatabase.Enabled = sucess; } catch (SqlException) { txtPassword.Text = String.Empty; connBuilder.Clear(); MessageBox.Show("Connection failed", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { connBuilder.Clear(); MessageBox.Show("Please select a server", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void UpdateConnectionString(object sender, EventArgs e) { builder.Clear(); builder.DataSource = enteredDatabaseAddress.Text; builder.InitialCatalog = enteredDatabaseName.Text; builder.UserID = enteredLogin.Text; builder.Password = enteredPassword.Text; labelConnectionString.Text = builder.ToString(); }
public void Dispose() { mobjPassword.Dispose(); mobjCredential = null; mobjSqlConnectionStringBuilder.Clear(); mobjSqlConnection.Dispose(); mobjDataAdapters.Clear(); mobjDTRepositories.Dispose(); mobjDTUsers.Dispose(); }
static void Main(string[] args) { string domain = System.Environment.UserDomainName; Console.WriteLine(domain); //this need to be wrapped string password = "******"; SecureString secureString = new SecureString(); Array.ForEach(password.ToArray(), (z) => secureString.AppendChar(z)); //end secureString.MakeReadOnly(); try { //use sql crederntial and password SqlCredential sql = new SqlCredential(Environment.UserName, secureString); SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(); builder.Add("Data Source", domain); builder.Add("Initial Catalog", "Example"); SqlCredential sqlCredential = new SqlCredential("roman", secureString); using (SqlConnection sqlConnectio = new SqlConnection(builder.ConnectionString, sqlCredential)) { sqlConnectio.Open(); SqlCommand sqlCommand = new SqlCommand("select SYSTEM_USER", sqlConnectio); string result = (string)sqlCommand.ExecuteScalar(); Console.WriteLine(result); sqlConnectio.Close(); } //use windows authentication builder.Clear(); builder.Add("Data Source", domain); builder.Add("Initial Catalog", "Example"); builder.Add("Integrated Security", "SSPI"); using (SqlConnection sqlConnectionSecond = new SqlConnection(builder.ConnectionString)) { sqlConnectionSecond.Open(); SqlCommand sqlCommand = new SqlCommand("select SYSTEM_USER", sqlConnectionSecond); string result = (string)sqlCommand.ExecuteScalar(); Console.WriteLine(result); } } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { secureString.Dispose(); } }
public void TestSqlConnectionStringBuilderClear(SqlConnectionColumnEncryptionSetting sqlConnectionColumnEncryptionSetting) { SqlConnectionStringBuilder connectionStringBuilder = new SqlConnectionStringBuilder(); // Modify the default value. connectionStringBuilder.ColumnEncryptionSetting = sqlConnectionColumnEncryptionSetting; connectionStringBuilder.DataSource = @"localhost"; connectionStringBuilder.Clear(); Assert.Equal(SqlConnectionColumnEncryptionSetting.Disabled, connectionStringBuilder.ColumnEncryptionSetting); Assert.True(string.IsNullOrEmpty(connectionStringBuilder.DataSource)); }
public void TestSqlConnectionStringAttestationProtocol() { SqlConnectionStringBuilder connectionStringBuilder = new SqlConnectionStringBuilder(); Assert.Equal(SqlConnectionAttestationProtocol.NotSpecified, connectionStringBuilder.AttestationProtocol); connectionStringBuilder.DataSource = @"localhost"; // Create a connection object with the above builder and verify the expected value. VerifyAttestationProtocol(connectionStringBuilder, SqlConnectionAttestationProtocol.NotSpecified); SqlConnectionStringBuilder connectionStringBuilder2 = new SqlConnectionStringBuilder(); connectionStringBuilder2.AttestationProtocol = SqlConnectionAttestationProtocol.AAS; Assert.Equal(SqlConnectionAttestationProtocol.AAS, connectionStringBuilder2.AttestationProtocol); connectionStringBuilder2.DataSource = @"localhost"; // Create a connection object with the above builder and verify the expected value. VerifyAttestationProtocol(connectionStringBuilder2, SqlConnectionAttestationProtocol.AAS); connectionStringBuilder2.Clear(); Assert.Equal(SqlConnectionAttestationProtocol.NotSpecified, connectionStringBuilder2.AttestationProtocol); Assert.True(string.IsNullOrEmpty(connectionStringBuilder2.DataSource)); SqlConnectionStringBuilder connectionStringBuilder3 = new SqlConnectionStringBuilder(); connectionStringBuilder3.AttestationProtocol = SqlConnectionAttestationProtocol.HGS; Assert.Equal(SqlConnectionAttestationProtocol.HGS, connectionStringBuilder3.AttestationProtocol); connectionStringBuilder3.DataSource = @"localhost"; // Create a connection object with the above builder and verify the expected value. VerifyAttestationProtocol(connectionStringBuilder3, SqlConnectionAttestationProtocol.HGS); connectionStringBuilder3.Clear(); Assert.Equal(SqlConnectionAttestationProtocol.NotSpecified, connectionStringBuilder3.AttestationProtocol); Assert.True(string.IsNullOrEmpty(connectionStringBuilder3.DataSource)); SqlConnectionStringBuilder connectionStringBuilder4 = new SqlConnectionStringBuilder(); connectionStringBuilder4.AttestationProtocol = SqlConnectionAttestationProtocol.None; Assert.Equal(SqlConnectionAttestationProtocol.None, connectionStringBuilder4.AttestationProtocol); connectionStringBuilder4.DataSource = @"localhost"; VerifyAttestationProtocol(connectionStringBuilder4, SqlConnectionAttestationProtocol.None); connectionStringBuilder4.Clear(); Assert.Equal(SqlConnectionAttestationProtocol.NotSpecified, connectionStringBuilder4.AttestationProtocol); Assert.Empty(connectionStringBuilder4.DataSource); }
// constructor for this Class // recieves credentials from Globals Class public DataConnection(string server, string database, string userId, string password) { this.Server = server; this.Database = database; this.UserID = userId; this.password = password; c_sqlb.Clear(); c_sqlb["Server"] = this.Server; c_sqlb["Database"] = this.Database; c_sqlb["User ID"] = this.UserID; c_sqlb["Password"] = this.Password; connstring = c_sqlb.ConnectionString; conn = new SqlConnection(connstring); }
public static IEnumerable <object[]> GetConnectionStrings() { var builder = new SqlConnectionStringBuilder(); foreach (var cnnString in DataTestUtility.GetConnectionStrings(withEnclave: false)) { builder.Clear(); builder.ConnectionString = cnnString; builder.ConnectTimeout = 5; builder.Pooling = false; yield return(new object[] { builder.ConnectionString }); builder.Pooling = true; yield return(new object[] { builder.ConnectionString }); } }
/// <summary> /// Overrides the native Didspose method /// </summary> /// <param name="disposing">True or False</param> protected virtual void Dispose(bool disposing) { if (!disposed) { if (disposing) { // Clear all property values that maybe have been set // when the class was instantiated Clear(); connectionString.Clear(); connectionString = null; } // Indicate that the instance has been disposed. disposed = true; } }
static void Main() { SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(); builder.DataSource = "(local)"; builder.IntegratedSecurity = true; builder.InitialCatalog = "AdventureWorks"; Console.WriteLine("Initial connection string: " + builder.ConnectionString); builder.Clear(); Console.WriteLine("After call to Clear, count = " + builder.Count); Console.WriteLine("Cleared connection string: " + builder.ConnectionString); Console.WriteLine(); Console.WriteLine("Press Enter to continue."); Console.ReadLine(); }
private string GetConnectionString() { var builder = new SqlConnectionStringBuilder(); builder.Clear(); builder.DataSource = Model.ServerName; if (Model.AuthenticationType == AuthenticationType.WindowsAuthentication) { builder.IntegratedSecurity = true; } else { builder.IntegratedSecurity = false; builder.UserID = Model.SqlServerUsername; builder.Password = Model.Password; } builder.InitialCatalog = Model.DatabaseName; return(builder.ConnectionString); }
public void Clear() { csb.Clear(); }
/// <summary> /// Method to build a SQL Server connection string. /// </summary> /// <returns>Fully formed connection string</returns> private String BuildConnectionString() { String retVal = ""; SqlConnectionStringBuilder sqlSB = null; try { String server; if (m_PortNumber != 1433) { server = m_Server + ":" + m_PortNumber.ToString(CultureInfo.CurrentCulture); } else { server = m_Server; } sqlSB = new SqlConnectionStringBuilder { ConnectRetryCount = m_ConnectRetryCount, ConnectRetryInterval = m_ConnectRetryInterval, ApplicationName = m_ApplicationName, ConnectTimeout = m_ConnectionTimeout, DataSource = server, InitialCatalog = m_DefaultDB, IntegratedSecurity = m_UseAuthentication, Password = m_Password, Pooling = m_ConnectionPooling, UserID = m_UserName, WorkstationID = m_WorkstationID }; retVal = sqlSB.ConnectionString; } // END try catch (Exception exUnhandled) { exUnhandled.Data.Add("Server", m_Server); exUnhandled.Data.Add("DefaultDB", m_DefaultDB); exUnhandled.Data.Add("UserName", m_UserName); exUnhandled.Data.Add("PortNumber", m_PortNumber.ToString(CultureInfo.CurrentCulture)); exUnhandled.Data.Add("UseAuthentication", m_UseAuthentication.ToString()); exUnhandled.Data.Add("ConnectRetryCount", m_ConnectRetryCount.ToString(CultureInfo.CurrentCulture)); exUnhandled.Data.Add("ConnectRetryInterval", m_ConnectRetryInterval.ToString(CultureInfo.CurrentCulture)); exUnhandled.Data.Add("ApplicationName", m_ApplicationName); exUnhandled.Data.Add("ConnectionTimeout", m_ConnectionTimeout.ToString(CultureInfo.CurrentCulture)); exUnhandled.Data.Add("WorkstationID", m_WorkstationID); throw; } // END catch (Exception eUnhandled) finally { if (sqlSB != null) { sqlSB.Clear(); sqlSB = null; } } // END finally return(retVal); } // END BuildConnectionString()