在C#WPF中保存数据库配置详细信息

时间:2018-12-11 11:48:34

标签: c# wpf

我正在创建一个应用程序,其第一个窗口是数据库配置窗口。用户从此窗口中输入服务器详细信息,然后选择他们要使用的数据库,数据库连接详细信息将基于该数据库进行连接,并转移到登录窗口并打开该窗口。

登录名和后续窗口使用数据库详细信息作为其SQL连接字符串。

我的问题是这样的:如何从配置窗口中保存这些连接详细信息,以便如果用户已经建立了以前的连接,则用户可以跳过此屏幕?我有将这些详细信息存储到文件中并在App.xaml.cs中设置布尔条件的想法,但是我不知道该怎么做。

这是我的代码:

对于“数据库设置”窗口

DatabaseSettings.xaml.cs

private void btnTest_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            connectionString = "Data Source = " + txtServer.Text + "; User Id = " + txtUserID.Text + "; Password = " + txtPassword.Password + "";
            conn = new SqlConnection(connectionString);
            conn.Open();
            string message = "Connection Successful Please Select a Database to Proceed";
            string caption = "Success!";
            MessageBoxButton buttons = MessageBoxButton.OK;
            MessageBoxImage icon = MessageBoxImage.Information;
            System.Windows.MessageBox.Show(message, caption, buttons, icon);
            txtServer.IsEnabled = false;
            txtUserID.IsEnabled = false;
            txtPassword.IsEnabled = false;
            btnTest.IsEnabled = false;
            cmbdatabase.IsEnabled = true;
            btnConfigure.IsEnabled = true;
            btnConfigure.IsDefault = true;

            sql = "EXEC sp_databases";
            command = new SqlCommand(sql, conn);
            reader = command.ExecuteReader();

            cmbdatabase.Items.Clear();
            while (reader.Read())
            {
                cmbdatabase.Items.Add(reader[0].ToString());
            }
            conn.Close();
        }
        catch(Exception ex)
        {
            System.Windows.MessageBox.Show("Error: " + ex);
        }
    }

    static string Encrypt_Password(string value)
    {
        using (SHA256Managed sha2 = new SHA256Managed())
        {
            UTF8Encoding uTF8 = new UTF8Encoding();
            byte[] data = sha2.ComputeHash(uTF8.GetBytes(value));
            return Convert.ToBase64String(data);
        }
    }



private void btnConfigure_Click(object sender, RoutedEventArgs e)
        {
            try
            {

                string selected = cmbdatabase.Text;
                txtSelectedDBItem.Text = cmbdatabase.Text;
                string EncryptedPassword = Encrypt_Password(txtPassword.Password);
                connectionString = "Data Source = " + txtServer.Text + "; Initial Catalog = " + selected + "; User Id = " + txtUserID.Text + "; Password = " + txtPassword.Password + "";
                conn = new SqlConnection(connectionString);

                sql = "INSERT INTO Proc_Activity_Log ([UserName], [Password], [AccessTime], [MachineSerial]) values(@userId, @password, @accessTime, @machineserial)";
                conn.Open();
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    cmd.Parameters.AddWithValue("@userID", txtUserID.Text);
                    cmd.Parameters.AddWithValue("@password", EncryptedPassword);
                    cmd.Parameters.AddWithValue("@accessTime", dateTime.Text);
                    cmd.Parameters.AddWithValue("@machineserial", txtMachineSerialNo.Text);
                    cmd.ExecuteNonQuery();                    
                }

                UserInterface.frmLogin loginScreen = new UserInterface.frmLogin(txtUserID.Text, txtPassword.Password, txtServer.Text, txtSelectedDBItem.Text);
                spashScreen.Show();
                this.Close();

            }

            catch (Exception ex)
            {
                System.Windows.MessageBox.Show("Error Here:" + ex.Message);
            }
        }

对于登录屏幕

frmLogin.xaml.cs

public frmLogin(string uname, string pwd, string server, string db)
        {
            InitializeComponent();

            txtServerUser.Text = uname;
            txtServerPwd.Password = pwd;
            txtServer.Text = server;
            txtSelectedDBItem.Text = db;
        }

private void btnOk_Click(object sender, EventArgs e)
        {
            SqlConnection sqlCon = new SqlConnection(@"Data Source=" + txtServer.Text + "; Initial Catalog=" + txtSelectedDBItem.Text + "; Integrated Security=True;");

            try
            {
                if (sqlCon.State == ConnectionState.Closed)
                    sqlCon.Open();
                String query = "SELECT COUNT(1) FROM dbo.users WHERE name=@Username and password=@Password";
                SqlCommand sqlCmd = new SqlCommand(query, sqlCon);
                sqlCmd.CommandType = CommandType.Text;
                sqlCmd.Parameters.AddWithValue("@Username", txtLogin.Text);
                sqlCmd.Parameters.AddWithValue("@Password", txtPwd.Password);
                int count = Convert.ToInt32(sqlCmd.ExecuteScalar());
                if (count == 1)
                {
                    frmMDI yourDesktop = new frmMDI(txtLogin.Text, txtServerUser.Text, txtServerPwd.Password, txtServer.Text, txtSelectedDBItem.Text);
                    yourDesktop.Show();
                    this.Close();


                }
                else
                {
                    txtLogin.Text = "admin";
                    txtPwd.Password = "admin";
                    txtLogin.Focus();
                }

            }

            catch (Exception ex)
            {
                string message = "The Following Error Occurred: " + ex.Message;
                string caption = "Invalid Action";
                MessageBoxButton buttons = MessageBoxButton.OK;
                MessageBoxImage icon = MessageBoxImage.Error;
                System.Windows.MessageBox.Show(message, caption, buttons, icon);
            }

            finally
            {
                sqlCon.Close();
            }
        }

从下面提供的屏幕截图中,我们可以推断出哪些按钮代表了上面代码中的点击事件

“数据库设置”窗口-测试连接

Database Settings Window - Testing Connection

“数据库设置”窗口-配置数据库

Database Settings Window - Configuring Database

最后是登录窗口

login window

0 个答案:

没有答案