Database Connection String Info

前端 未结 6 1222
误落风尘
误落风尘 2020-12-31 10:55

In .Net is there a class in .Net where you can get the DB name, and all the connection string info without acutally doing a substring on the connection string?

EDIT:

相关标签:
6条回答
  • 2020-12-31 10:59
    ConnectionInfo connectionInfo = new ConnectionInfo ();
    connectionInfo = logOnInfo.ConnectionInfo;
    
    connectionInfo.DatabaseName = database;
    connectionInfo.ServerName = server;
    connectionInfo.Password = password;
    connectionInfo.UserID = user;
    

    EDIT: Looks like Nathan beat me to it, as mine is from the same page.

    EDIT AGAIN: I should note that ConnectionInfo is in the CrystalDecisions.Shared namespace. As for how to get it from a generic DB connection, I'm not sure.

    0 讨论(0)
  • 2020-12-31 10:59

    if you build your connection string using the Connection string builder (e.g. OracleConnectionStringBuilde, and it will be different for different database), in that case easily retrieve the information out of it.

    here it explained:

    http://msdn.microsoft.com/en-us/library/ms254947.aspx

    0 讨论(0)
  • 2020-12-31 10:59
    SqlConnection sq = new SqlConnection(ConnectionString);
    

    Reference

    Done with "using" statement (from MSDN)

    using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();
            // Do work here; connection closed on following line.
        }
    
    0 讨论(0)
  • 2020-12-31 11:22

    Yep ConnectionInfo

    http://msdn.microsoft.com/en-us/library/ms226340(VS.80).aspx

    EDIT: I, along with Chris, realized that this is only works if you have the Crystal Reports namespaces imported. Otherwise I'm not sure

    0 讨论(0)
  • 2020-12-31 11:24

    After you initialize the connection with the connection string, you can get those information from properties of the initialized connection object.

    Check System.Data.Common.DbConnection.

    0 讨论(0)
  • 2020-12-31 11:25

    You can use the provider-specific ConnectionStringBuilder class (within the appropriate namespace), or System.Data.Common.DbConnectionStringBuilder to abstract the connection string object if you need to. You'd need to know the provider-specific keywords used to designate the information you're looking for, but for a SQL Server example you could do either of these two things:

    Given

    string connectionString = "Data Source = .\\SQLEXPRESS;Database=Northwind;Integrated Security=True;";
    

    You could do...

    System.Data.Common.DbConnectionStringBuilder builder = new System.Data.Common.DbConnectionStringBuilder();
    
    builder.ConnectionString = connectionString;
    
    string server = builder["Data Source"] as string;
    string database = builder["Database"] as string;
    

    Or

    System.Data.SqlClient.SqlConnectionStringBuilder builder = new System.Data.SqlClient.SqlConnectionStringBuilder();
    
    builder.ConnectionString = connectionString;
    
    string server = builder.DataSource;
    string database = builder.InitialCatalog;
    
    0 讨论(0)
提交回复
热议问题