nesesito help in a checkbox c # [closed]

-1

hello good I'm wanting to connect sql server in c # but in the form of checkbox esoty doing it I have a checkbox asks if authentication is by windows or is per user

At the moment of calling a string, you do not recognize it, I leave you the code beforehand thanks

        if (tipoAuthentificacion == true) {
        string connectionString0 = string.Format("Data Source={0};Initial Catalog={1};Integrated Security=True;", cbServer.Text, txtDataBase.Text);

        }
        else
        {
            string connectionString1 = string.Format("Data Source={0};Initial Catalog={1}; User Id={2};Password={3};", cbServer.Text, txtDataBase.Text, txtUsername.Text, txtPassword.Text);

        }

        string connections = connectionString0 /supuesto error que no reconoce

        try
        {
            CapaNegocio.Conexion Heloper = new Conexion(connections);
            if (Heloper.CheckConnection)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("Probando conexion exitosa");
            }

        }
        catch (Exception ex)
        {
            DevComponents.DotNetBar.MessageBoxEx.Show(ex.Message);

        }

capture design

    
asked by Mati 04.04.2017 в 15:04
source

2 answers

0
    if (tipoAuthentificacion == true) 
    {
        string connectionString0 = string.Format("Data Source={0};Initial Catalog={1};Integrated Security=True;", cbServer.Text, txtDataBase.Text);
    }
    else
    {
        string connectionString1 = string.Format("Data Source={0};Initial Catalog={1}; User Id={2};Password={3};", cbServer.Text, txtDataBase.Text, txtUsername.Text, txtPassword.Text);
    }
    string connections = connectionString0;

This is your code block, the error is very simple, you are declaring the variables inside the if, therefore when you finish the variables are deleted, that is, the variables that you declare inside the keys can only be used there that are local, to solve the error you should put it like this

string connectionString;
if (tipoAuthentificacion == true) 
{
    connectionString = string.Format("Data Source={0};Initial Catalog={1};Integrated Security=True;", cbServer.Text, txtDataBase.Text);
}
else
{
    connectionString = string.Format("Data Source={0};Initial Catalog={1}; User Id={2};Password={3};", cbServer.Text, txtDataBase.Text, txtUsername.Text, txtPassword.Text);
}

try
{
    CapaNegocio.Conexion Heloper = new Conexion(connectionString);
    if (Heloper.CheckConnection)
    {
        DevComponents.DotNetBar.MessageBoxEx.Show("Probando conexion exitosa");
    }
}
catch (Exception ex)
{
    DevComponents.DotNetBar.MessageBoxEx.Show(ex.Message);
}
    
answered by 04.04.2017 / 16:35
source
0

The truth is that I would not advise this what you are asking, do not ask what type of connection you want to make from a user interface, the connection string is defined in app.config and is taken from there directly.

If you want to use username and password or windows authentication, you use the notepad and change the .config and the application is executed, but the connection string is configuration.

Connection Chains and Configuration Files

How to get Connection String from App .Config in C #

Analyze the articles to see where you define the connection string and how you retrieve it using the class ConfigurationManager

    
answered by 04.04.2017 в 16:21