Results 1 to 5 of 5

Thread: Connect c# application to database

  1. #1
    Join Date
    Oct 2012
    Posts
    4

    Connect c# application to database

    I have this code to connect a c# application to my database in sql managment studio:

    public Form1()
    {
    InitializeComponent();
    }
    private bool CompareStrings(string string1, string string2)
    {
    return string.Compare(string1, string2, true, System.Globalization.CultureInfo.InvariantCulture) == 0 ? true : false;
    }
    private void Login_Click(object sender, EventArgs e)
    {
    try
    {
    SqlConnection con = new SqlConnection("Server=servername;DataBase=database _db;Trusted_Connection=True;User Id=Id;Password=password");
    con.Open();
    SqlCommand command = new SqlCommand("SELECT ISNULL(UserName,'') AS UserName, ISNULL(Password, '') AS Password FROM LoginDetails WHERE UserName = " + UserName.Text + " and Password =" + Password.Text + "'", con);
    SqlDataReader dr = command.ExecuteReader();

    string userText = UserName.Text;
    string passwordText = Password.Text;

    while (dr.Read())
    {
    if (this.CompareStrings(dr["UserName"].ToString(), userText) && this.CompareStrings(dr["Password"].ToString(), passwordText))
    {
    MessageBox.Show("OK");
    }
    else
    {
    MessageBox.Show("ERROR");
    }
    }

    dr.Close();
    con.Close();
    }
    catch (Exception a)
    {
    MessageBox.Show(a.Message);
    }
    }

    I have a simple login page to write username and password
    the problem is that when I start this code and write username and password I get an error: the login failed for user". the user is not associeted with a trusted sql connection
    Last edited by Angelica; 10-25-2012 at 01:28 AM.

  2. #2
    Join Date
    Sep 2002
    Posts
    5,938
    When you set Trusted_Connection=True, you ask for windows authentication that doesn't use you id/pwd specified. It uses current windows account instead.

  3. #3
    Join Date
    Oct 2012
    Posts
    4
    what can I do to fix it?

  4. #4
    Join Date
    Sep 2002
    Posts
    5,938
    Depends on whether you like to use windows or sql authentication, you can find all kind of connection strings here http://www.connectionstrings.com/sql-server-2008

  5. #5
    Join Date
    Jan 2013
    Posts
    1
    Connect barcode features to SQL reporting services in to C# application, http://www.avapose.com/dotnet_barcod...enerator.shtml

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •