none
sdf Database Password Problem RRS feed

  • Question

  • using VCS 2010

    I have an sdf database that I believe has no password. To add a password I do this:

    //  Button to change password
            private void changePasswordToolstripMenuItem_Click(object sender, EventArgs e)
            {
                bool stopLoop = false;
    
                while (stopLoop == false)
                {
                    ChangePasswordForm changePasswordForm = new ChangePasswordForm();
                    changePasswordForm.ShowDialog();
                    if (changePasswordForm.DialogResult == DialogResult.OK)
                    {
                        using (SqlCeEngine engine = new SqlCeEngine(@"Data Source = Password4.sdf;Password=" + changePasswordForm.OldPassword + ";"))
                        {
    
                            try
                            {
                                engine.Compact("Data Source = Password4.sdf;Password=" + changePasswordForm.NewPassword + ";");
                                stopLoop = true;
                            }
                            catch (Exception ex)
                            {
                                //MessageBox.Show("error" + ex.ToString());
                            }
                        }
                    }
                    else
                    {
                        stopLoop = true;
                    }
                    //Password4.Properties.Settings.Default.Password4ConnectionString = connectionString;
    
                    //itemsTableAdapter.Connection.ConnectionString = ("Data Source = database1.sdf;Password=" + textBox2.Text + ";");
                    changePasswordForm = null;
                }  // End while
    
            }

    When this is done, I close the program.

    When I restart the program, I can no longer restart it and it tells me the password is incorrect. This is the code I use to check it:

    {
                            enteredPassword = loginForm.Password;
                            connectionString = "Data Source = Password4.sdf;password = " + enteredPassword + ";";
                            Password4.Properties.Settings.Default.Password4ConnectionString = connectionString;
                            this.categoriesTableAdapter.Connection.ConnectionString = connectionString;
                            //Authenticate the entered password
                            try
                            {
                                //this.categoriesTableAdapter.Fill(this.password4DataSet.Categories);
                                this.settingsTableAdapter.Fill(this.password4DataSet.Settings);
                                loginAttempts = 0;
                                loginAuthenticated = true;
                                stopLoop = true;
                            }
                            catch { loginAttempts++; }

    Am I checking it correctly? Please comment.

    Monday, May 14, 2018 1:39 PM

Answers

All replies

  • Hello jack0987,

    Did you see the MSDN description about SqlCeEngine.Compact? which could change password settings of the database as above you do. After finish running the connection should be replace with new password. Below is my test example.

    class Program
        {
          
            static void Main(string[] args)
            {
                CreatDatebase();
                changePassword();
                Insert();
                Search();
            }
    
            private static void changePassword()
            {
                using (SqlCeEngine engine = new SqlCeEngine(@"Data Source = Test.sdf; Password = 123"))
                {
                    engine.Compact("Data Source = Test.sdf;Password=456");
                }
            }
    
            static void CreatDatebase()
            {
                if (File.Exists("Test.sdf"))
                    File.Delete("Test.sdf");
    
                string connStr = "Data Source = Test.sdf; Password = 123";
    
                SqlCeEngine engine = new SqlCeEngine(connStr);
                engine.CreateDatabase();
                engine.Dispose();
    
                SqlCeConnection conn = null;
    
                try
                {
                    conn = new SqlCeConnection(connStr);
                    conn.Open();
    
                    SqlCeCommand cmd = conn.CreateCommand();
                    cmd.CommandText = "CREATE TABLE myTable (col1 int, col2 ntext)";
                    cmd.ExecuteNonQuery();
                }
                catch { }
                finally
                {
                    conn.Close();
                }
            }
    
            static void Insert()
            {
    
                SqlCeConnection sqlConnection1 = new SqlCeConnection("Data Source = Test.sdf; Password = 456");
                SqlCeCommand cmd = new SqlCeCommand("INSERT [myTable] (col1, col2) VALUES (1, 'Tim')", sqlConnection1);
    
                try
                {
                    sqlConnection1.Open();
                    cmd.ExecuteNonQuery();
                }
                catch { }
                finally
                {
                    sqlConnection1.Close();
                }
            }
            static void Search()
            {
                SqlCeConnection conn = new SqlCeConnection("Data Source = Test.sdf;Password = 456");
    
                string query = "SELECT [col1], [col2] FROM myTable";
    
                SqlCeCommand cmd = new SqlCeCommand(query, conn);
    
                conn.Open();
                SqlCeDataReader rdr = cmd.ExecuteReader();
    
                try
                {
                    // Iterate through the results
                    //
                    while (rdr.Read())
                    {
                        int val1 = rdr.GetInt32(0);
                        string val2 = rdr.GetString(1);
                    }
                }
                finally
                {
                    // Always call Close when done reading
                    //
                    rdr.Close();
    
                    // Always call Close when done reading
                    //
                    conn.Close();
                }
            }
        }

    Output:

    1

    Tim

    Best Regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 15, 2018 8:23 AM
    Moderator
  • Thanks for your very considered reply. I will try it out and study it more.

    However, I do not see any method that simply tests the password to see if it is valid.

    Also, your test program creates a database with a password to begin with. I have a database that has no password to begin with and want to learn how to give it a password or if needed remove the password at a later time.

    Tuesday, May 15, 2018 12:10 PM
  • I don't think your TableAdapter is using the new connection string. Check the below link for more info about changing the connection string for a TableAdapter at run time.

    http://thecodemonk.com/2008/02/18/tableadapter-connection-strings/


    Paul ~~~~ Microsoft MVP (Visual Basic)

    • Marked as answer by jack0987 Wednesday, May 16, 2018 12:08 PM
    Tuesday, May 15, 2018 12:38 PM
  • Thanks, Paul. Good observation. I do, at a glance, believe you are right.

    Still, is there not a way for me to simply validate the password before I do anything?

    Tuesday, May 15, 2018 3:32 PM
  • I don't think it is possible to add a password to an existing table.  The password is assigned when creating the database.  Also the only way to validate the password is to try and open it, just put it inside try catch statement.

    Tuesday, May 15, 2018 4:48 PM
  • Thanks, Paul. Good observation. I do, at a glance, believe you are right.

    Still, is there not a way for me to simply validate the password before I do anything?

    You can call the Verify method:

    https://msdn.microsoft.com/en-us/library/a0a5czch%28v=vs.100%29.aspx


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Tuesday, May 15, 2018 5:37 PM
  • You can call the Verify method:

    https://msdn.microsoft.com/en-us/library/a0a5czch%28v=vs.100%29.aspx


    Thanks. I will test it.

    Wednesday, May 16, 2018 12:08 PM