• 大小: 27KB
    文件类型: .7z
    金币: 1
    下载: 0 次
    发布日期: 2021-06-06
  • 语言: C#
  • 标签: 加密  解密  代码  

资源简介

程序把连接字符串保存在硬盘里,每次启动程序的时候可以读取该字符串,用来连接数据库。连接字符串里有用户名和密码等信息,可以加密保存,读取时解密。

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Common;

namespace Chapter_03
{
    public partial class ConnectionStringBuilderDialog : Form
    {
        public ConnectionStringBuilderDialog()
        {
            InitializeComponent();
        }

        string _ConnectionString;
        public string ConnectionString { get { return _ConnectionString; } }

        DbProviderFactory _ProviderFactory;
        public DialogResult EditConnectionString(DbProviderFactory ProviderFactory DbConnectionStringBuilder ConnectionStringBuilder)
        {
            _ConnectionString = ConnectionStringBuilder.ConnectionString;
            _ProviderFactory = ProviderFactory;

            propGrid.Selectedobject = ConnectionStringBuilder;
            DialogResult retval = this.ShowDialog();

            if (retval == DialogResult.OK)
                _ConnectionString = ConnectionStringBuilder.ConnectionString;

            return retval;
        }

        private void btnTest_Click(object sender EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                using (DbConnection cn = _ProviderFactory.CreateConnection())
                {
                    DbConnectionStringBuilder bldr = (DbConnectionStringBuilder)propGrid.Selectedobject;
                    cn.ConnectionString = bldr.ConnectionString;
                    cn.Open();
                }
                this.Cursor = Cursors.Default;
                MessageBox.Show(“Success!“ “Connection attempt succeeded!“ MessageBoxButtons.OK MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                this.Cursor = Cursors.Default;
                MessageBox.Show(ex.Message “Connection attempt failed!“ MessageBoxButtons.OK MessageBoxIcon.Error);
            }
            this.Cursor = Cursors.Default;
        }
    }
}

评论

共有 条评论