0

So i m using this code for my sql connection to connect to local server :

public void ConnectToSql(string ServerName, string UserName, string Password)
        {
            string strConnection = "server=" + tbServer + ";uid=" + tbUser + "; pwd=" + tbPwd;
            SqlCon = new System.Data.SqlClient.SqlConnection();
            SqlCom = new System.Data.SqlClient.SqlCommand();
            SqlCon.ConnectionString = strConnection;
            SqlCom.CommandType = System.Data.CommandType.Text;
            SqlCom.Connection = SqlCon;

            {
                try
                {
                    SqlCon.Open();

                }
                catch (Exception ex)
                {
                    MessageBox.Show("Failed to connect to data source");
                }


            }

I call it like this :

 private void Button_Click(object sender, RoutedEventArgs e)
        {
            ConnectToSql(tbServer.Text, tbUser.Text, tbPwd.Text);
        }

In windows form works perfect but when i try it in WPF gues what , cant connect to local server. Any sugestions ??

Dean Kuga
  • 11,878
  • 8
  • 54
  • 108
Dani Dărăban
  • 23
  • 2
  • 13

3 Answers3

3

Quick solution:

replace this:

string strConnection = "server=" + tbServer + ";uid=" + tbUser + "; pwd=" + tbPwd;

for this:

string strConnection = "server=" + tbServer.Text + ";uid=" + tbUser + "; pwd=" + tbPwd.Text;

Correct solution:

UI is Not Data. Learn MVVM before you write a single line of code in WPF. Don't manipulate UI elements in procedural code like that. Create a proper ViewModel and have your ViewModel access the DAL and expose the data and logic.

Also, you're better off using an ORM than ancient ADO.Net and putting all SQL instructions as strings.

And please, for Christ's Sake, remove that from the code behind and create a proper Data Access Layer.

Community
  • 1
  • 1
Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154
2

Taking an educated guess your problem is probably on this line:

public void ConnectToSql(string ServerName, string UserName, string Password)
{
    string strConnection = "server=" + tbServer + ";uid=" + tbUser + "; pwd=" + tbPwd;
    //...

Rather than building your connection string with the parameters that are passed, you are using UI items, which probably don't build a very good connection string. Try this:

string strConnection = "server=" + ServerName + ";uid=" + UserName + "; pwd=" + Password;

Also I agree with everything HighCore is saying, this is just a quick fix.

Kevin DiTraglia
  • 25,746
  • 19
  • 92
  • 138
1

I would recommend using the SqlConnectionStringBuilder, try something like this as others said before WPF or WinForms has nothing to do with your Problem!

using (var cmd = new SqlCommand())
{
        cmd.CommandText = "Your Query"
        cmd.Connection = GetOpenDataConnection(".", "sampleDb", "sa", "12345");

        //Execute NonQuery or ExecuteReader
        cmd.Connection.Close();
}

private SqlConnection GetOpenDataConnection(string server, string database, string user, string password)
{
    var builder = new SqlConnectionStringBuilder();
    builder.DataSource = server;
    builder.InitialCatalog = database;
    builder.UserID = user;
    builder.Password = password;

    var connection = new SqlConnection(builder.ToString());
    connection.Open();
    return connection;
}
makim
  • 3,154
  • 4
  • 30
  • 49