Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,
I'm trying to write a login Windows form application with SqlServerCe as underlying database. Problem is I have been failing. Is there a fault in my syntax ?
naturally I have added reference to the SqlServerCe namespace and also in the reference dlls
please give a blind eye to the string concatenation. This is written in a testing environment.
Please help

What I have tried:

private void button1_Click(object sender, EventArgs e)
        {
            string lg = "select username, password from creds where username= '" + txtusername + "' and password= '" + txtpassword + "'";

            con.Open();
            SqlCeCommand cmd = new SqlCeCommand(lg, con);
            SqlCeDataReader dr;
            dr = cmd.ExecuteReader();
            int k = 0;
            while (dr.Read())
            {
                k++;
            }

            if (k == 1)
                MessageBox.Show("success!");

            else
                MessageBox.Show("failure");

            con.Close();
        }
Posted
Updated 12-Mar-17 11:43am
Comments
PIEBALDconsult 12-Mar-17 10:48am    
0) Please learn to use parameterized statements.
1) Please don't store plain-text passwords.
2) Please don't put data access code directly in your UI code.
There are many articles here that should help:
https://www.codeproject.com/Articles/704865/Salted-Password-Hashing-Doing-it-Right
https://www.codeproject.com/Articles/608860/Understanding-and-Implementing-Password-Hashing
https://www.codeproject.com/Articles/425150/Beginners-guide-to-a-secure-way-of-storing-passwor
https://www.codeproject.com/Articles/54164/Secure-Password-Authentication-Explained-Simply
[no name] 12-Mar-17 10:56am    
What on earth does "failing" mean? Do you get any errors? What errors do you get? When you step through your code, what do your variables contain? What does your query contain? Is your connection actually open or do you get errors there too?

1 solution

C#
using System.Data.SqlClient;

private void button1_Click(object sender, EventArgs e)
    {
        string conString = "Data Source=MyData.sdf;Persist Security Info=False;";
        string lg = "select username, password from creds where username= '" + txtusername + "' and password= '" + txtpassword + "'";

        SqlCeConnection con = new SqlCeConnection();
        con.ConnectionString = conString;
        SqlCeCommand selectCmd = con.CreateCommand();
        selectCmd.CommandText = lg;

        SqlCeDataAdapter adp = new SqlCeDataAdapter(selectCmd);

        DataSet ds = new DataSet();

        // Note: Fill will leave the connection in its original state;
        // In this case, the connection was closed so it will be left closed
        //
        adp.Fill(ds);



        if (ds.Tables[0].Rows.Count > 0)
            MessageBox.Show("success!");

        else
            MessageBox.Show("no data exists");

    }
 
Share this answer
 
Comments
[no name] 13-Mar-17 8:33am    
Could you maybe point out the description of the problem in the OPs posting and then indicate how your code solves that problem?
jekin77 14-Mar-17 8:31am    
So , the question was - "Is there a fault in my syntax ?" YES !!!
My answer is the correct one !!!
Just compare two snippets , you will see the difference that no need any explanation!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900