Mirko Nastasi
Mirko Nastasi

Reputation: 19

database connection string with a button with a c# file

in the program there must be a request for these things: -server name

I'm looking for a way to do the above, I don't know about it. this is program:

private void button1_Click(object sender, EventArgs e) {

        SqlConnection con = new SqlConnection(@"Data Source=server; Initial Catalog=ACE_db; User Id=m.nastasi; Password=Mirko.123!");
        SqlDataAdapter sda = new SqlDataAdapter("Select Count(*) From Login where Data Source='" + textBox1.Text + "'; Initial Catalog='" + textBox2.Text + "'; ID='" + textBox3.Text + "' and Password='" + textBox4.Text + "' ",con); 
        
        
        
        this.Hide();

        Main ss = new Main();
        ss.show();

Upvotes: 0

Views: 137

Answers (1)

user20376806
user20376806

Reputation: 304

Since the code you provided is limited and the description is not very clear. According to your request, I re-wrote a simple sqlserver connection tool using Winforms. If you have any more information to add please let me know.

code show as below:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace Navicate
{
    public partial class Form1 : Form
    {
        string server, databaseName, userid, password;
 
        SqlConnection conn;
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            textBox4.PasswordChar = '*';
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            server = textBox1.Text;
            databaseName = textBox2.Text;
            userid = textBox3.Text;
            password = textBox4.Text;
            if (server == "" && databaseName == "" && userid == "" && password == "")
            {
                MessageBox.Show("Please fill in the database connection information completely");
            }
            else
            {
                if(OpenConnection(server, databaseName, userid, password))
                {
                    MessageBox.Show("Database connection successed");
                }
                else
                {
                    MessageBox.Show("Database connection failed");
                }
 
            }
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            if (OpenConnection(server, databaseName, userid, password))
            {
                CloseConnection();
                MessageBox.Show("Database disconnected");
            }
            else
            {
                MessageBox.Show("Please connect to the database first");
            }
        }
 
        private bool OpenConnection(string server, string databaseName, string userid, string password)
        {
            try
            {
                string strConn = "server=" + server +
                                 ";database=" + databaseName +
                                ";uid=" + userid +
                                ";pwd=" + password;
                conn = new SqlConnection(strConn);
                conn.Open();
            }
            catch (Exception)
            {
                return false;
                
 
            }
            return true;
        }
 
        private bool CloseConnection()
        {
            try
            {
                conn.Dispose();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
 
    }
}

The layout is as follows:

enter image description here

The effect is as follows:

enter image description here

Upvotes: 1

Related Questions