admin posted this 05 September 2018
I think you should create a configuration form that allows you to change your connection string at runtime as the following

- Server: Contains your sql server name, you can enter your server name or select from the available server list
- Database: Enter your database name that you want to connect
- User name: Use the sql username to connect to your sql server
- Password: Use the sql password to connect to your sql server
Put your connection string in the app.config file
<configuration>
<connectionStrings>
<add name="cn" connectionString="Data Source=YourServerName;Initial Catalog=YourDatabase;User ID=YourUserSql;Password=YourPasswordSql;" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
Create an AppSetting class to read and update your connection string
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WindowsForms1
{
public class AppSetting
{
Configuration config;
public AppSetting()
{
config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
}
//Get connection string from App.Config file
public string GetConnectionString(string key)
{
return config.ConnectionStrings.ConnectionStrings[key].ConnectionString;
}
//Save connection string to App.config file
public void SaveConnectionString(string key,string value)
{
config.ConnectionStrings.ConnectionStrings[key].ConnectionString = value;
config.ConnectionStrings.ConnectionStrings[key].ProviderName = "System.Data.SqlClient";
config.Save(ConfigurationSaveMode.Modified);
}
}
}
Open your configuration form, then add code to handle connect button and save button
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsForm1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnConnect_Click(object sender, EventArgs e)
{
string connectionString = string.Format("Data Source={0};Initial Catalog={1};User ID={2};Password={3};", cboServer.Text, txtDatabase.Text, txtUsername.Text, txtPassword.Text);
try
{
SqlHelper helper = new SqlHelper(connectionString);
if (helper.IsConnection)
MessageBox.Show("Test connection succeeded.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void Form1_Load(object sender, EventArgs e)
{
//Add server name to combobox
cboServer.Items.Add(".");
cboServer.Items.Add("(local)");
cboServer.Items.Add(@".\SQLEXPRESS");
cboServer.Items.Add(string.Format(@"{0}\SQLEXPRESS", Environment.MachineName));
cboServer.SelectedIndex = 3;
}
private void btnSave_Click(object sender, EventArgs e)
{
//Set connection string
string connectionString = string.Format("Data Source={0};Initial Catalog={1};User ID={2};Password={3};", cboServer.Text, txtDatabase.Text, txtUsername.Text, txtPassword.Text);
try
{
SqlHelper helper = new SqlHelper(connectionString);
if (helper.IsConnection)
{
AppSetting setting = new AppSetting();
setting.SaveConnectionString("cn", connectionString);
MessageBox.Show("Your connection string has been successfully saved.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
You should change your connection string
SqlConnection con = new SqlConnection(cs)
to
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString)
or you can create a global class that's help you easily to change the connection string
public static class Global
{
public static string ConnectionString => ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
}
then change your connection string as the following
SqlConnection con = new SqlConnection(Global.ConnectionString)