How to add SQL ConnectionString editor form.

I have created little C Sharp TOOLKIT application utilizing SQL ConnectionString.

I use standard windows installer. It compiles nicely creating TOOLKIT.exe.config file with default ConnectionString settings.

Sure my customer could go to TOOLKIT.exe.config XML file and edit string to connect to their SQL database. It is simple for us, but for customer it can be little harder.

I would like to make their lives easier.

My question – Is there any simple way to add a form that would edit ConnectionString after software is installed.

Something like we have by name “Add Connection”

http://www.windowsitpro.com/Files/09/9002/Figure_05.gif

Thank you a lot!



Answer this question

How to add SQL ConnectionString editor form.

  • Kaos

    To call DataLink Dialog

    Add Reference to the Following two Com Component

    1-Microsoft ActiveX Data Objects
    2-Microsoft OLEDB Service Component

    then you can write some thing like

    private void btnConnect_Click(object sender, EventArgs e)

    {

    string connectionString = "";

    DataLinks cnDlg = new DataLinks();

    ADODB._Connection con;

    con = (ADODB._Connection)cnDlg.PromptNew();

    connectionString=con.ConnectionString;

    //Write The connection string to confi file

    //ADOcon.Close();

    //You can use Configuration classes to write to your config file

    }

    for moe info look at

    TO: Build a Connection String Programmatically in ADO.NET by Using Visual C# .NET

    ADO Data Access through COM Interop in .NET

    Configuration Class

    Read/Write App.Config File with .NET 2.0




  • chakravarthy_b

    Mohammad, thank you.

    YES, It does works. But it offers more than I asked for.

    Now enduser need to go over all types of datasource options. Too many choices.

    I would like to create something simple with only one MS SQL connection available on one screen.

    Only textboxes

    Server, Database, Auth Type, Username and Password 

    Something like this http://www.ogsys.com/images/image006.jpg

    Maybe we should use SQLDMO, Interop.SQLDMO.dll

    Any ideas,

    Thank you again.

     


  • Ather.

    you can use SMO to build this dialog i created quick one modify it add error checking etc

    but at first add reference to

  • Microsoft.SqlServer.ConnectionInfo.dll
  • Microsoft.SqlServer.Smo.dll
  • Microsoft.SqlServer.SmoEnum.dll
  • and the following code to  the form
  • using Microsoft.SqlServer.Management.Smo;

    using Microsoft.SqlServer.Management.Common;

    //add two ComboBox cboServers and cboDatabases

    //and radioButtons optWin and optSql

    //and two TextBox txtUser and txtPassword

    //and Button Control button1

     

    private void ListServers()

    {

    //For Local only

    // DataTable avilableServers = SmoApplication.EnumAvailableSqlServers(true);

    //By computer Name

    // DataTable avilableServers = SmoApplication.EnumAvailableSqlServers();

    //For Network

    DataTable avilableServers = SmoApplication.EnumAvailableSqlServers();

    foreach (DataRow server in avilableServers.Rows)

    {

    string serverName = server["Server"].ToString();

    // if we got any instance

    if(!String.IsNullOrEmpty(serverName))

    {

    //add it to combo

    cboServers.Items.Add(serverName);

    }

    }

    }

    private void ListDatabases(string serverName)

    {

    cboDatabases.Items.Clear();

    Server server = new Server(serverName);

    foreach (Database db in server.Databases)

    {

    cboDatabases.Items.Add(db.Name);

    }

    }

    private void button1_Click(object sender, EventArgs e)

    {

    ListServers();

    }

    //When server change list the Databases

    private void cboServers_SelectedIndexChanged(object sender, EventArgs e)

    {

    ListDatabases(cboServers.SelectedText);

    }

    //Show Connection string

    private void btnCnString_Click(object sender, EventArgs e)

    {

    //may be there simple method another this but this what came in my mind know

    StringBuilder sb = new StringBuilder();

    sb.Append("Data Source =");

    sb.Append(cboServers.Text);

    sb.Append(";");

    sb.Append("Initial Catalog = ");

    sb.Append(cboDatabases.Text);

    sb.Append(";");

    if (optWin.Checked == true)

    {

    sb.Append("Integrated Security=SSPI;");

    }

    else //if(optSql.Checked==true)

    {

    sb.Append("User ID=");

    sb.Append(txtUser.Text);

    sb.Append(";");

    sb.Append("Password=");

    sb.Append(txtPassword.Text);

    sb.Append(";");

    }

    MessageBox.Show(sb.ToString());

    }



  • IamManick

    I started to write a class like that (have it mostly finished somewhere).

    The presentable portion of it is at

    http://www.codeproject.com/cs/database/locate_sql_servers.asp



  • How to add SQL ConnectionString editor form.