码迷,mamicode.com
首页 > 数据库 > 详细

4、数据库连接信息管理

时间:2017-10-04 14:18:25      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:配置   dialog   copy   alignment   panel   sts   tac   sqlserver   wow   

一、简介

       本篇的主要目标是管理及维护程序的数据库连接信息。添加及修改数据库的连接信息是通过Microsoft.Data.ConnectionUI.Dialog.dll类中提供的DataConnectionDialog。

二、数据库连接信息的数据模型

1、定义数据库的类型

    public enum ServerType
    {
        SqlServer,
        Oracle,
        MySQL,
        SQLite
    }

2、定义数据库连接信息的模型

    public class Connection
    {
        /// <summary>
        /// Id 唯一标识
        /// </summary>
        public string Id { get; set; }
        /// <summary>
        /// 连接字符串
        /// </summary>
        public string ConnectionString { get; set; }
        /// <summary>
        /// 连接名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 数据库类型
        /// </summary>
        public ServerType Type { get; set; }
    }

三、数据库连接信息列表页面

View中GridControl代码如下:

<dxg:GridControl x:Name="grid"  Grid.Row="2"  HorizontalContentAlignment="Center" 
                         ItemsSource="{Binding Connections,Mode=TwoWay}" 
                         SelectedItem="{Binding SelectedConnection,Mode=TwoWay}" >
            <dxg:GridControl.View>
                <dxg:TableView x:Name="view" ShowGroupPanel="False" NavigationStyle="Row" UseEvenRowBackground="True"  AutoWidth="True"/>
            </dxg:GridControl.View>
            <dxg:GridControl.Columns>
                <dxg:GridColumn Header="名称"  FieldName="Name"  />
                <dxg:GridColumn Header="类型"  FieldName="Type"  />
            </dxg:GridControl.Columns>
        </dxg:GridControl>

 

ViewModel中需要定义一个列表数据源:

 private ObservableCollection<Connection> _connections;
        public ObservableCollection<Connection> Connections
        {
            get
            {
                return this._connections;
            }
            set
            {
                if (this._connections != value)
                {
                    this._connections = value;
                    this.RaisePropertyChanged("Connections");
                }
            }
        }

        private Connection _selectedConnection;
        public Connection SelectedConnection
        {
            get
            {
                return this._selectedConnection;
            }
            set
            {
                if (this._selectedConnection != value)
                {
                    this._selectedConnection = value;
                    this.RaisePropertyChanged("SelectedConnection");
                }
            }
        }

 

  四、数据库连接信息明细页面:

              技术分享

添加弹出DataConnectionDialog页面的代码

        private DataConnectionDialog GetDatabaseConnection()
        {
            string result = string.Empty;
            DataConnectionDialog dialog = new DataConnectionDialog();
            dialog.DataSources.Clear();

            //添加数据源列表,可以向窗口中添加所需要的数据源类型 必须至少有一项 
            dialog.DataSources.Add(DataSource.SqlDataSource);       //Sql Server
            dialog.DataSources.Add(DataSource.OracleDataSource);    //Oracle 

            //设置默认数据提供程序
            dialog.SelectedDataSource = DataSource.SqlDataSource;
            dialog.SelectedDataProvider = DataProvider.SqlDataProvider;
             
            if (DataConnectionDialog.Show(dialog) != System.Windows.Forms.DialogResult.OK)
            {
                dialog.ConnectionString = string.Empty;
            }
            return dialog;
        }

效果如下:

技术分享

 

 是不是有一种很熟悉的赶脚呢。

  五、数据库连接信息保存

         数据库的连接信息我将其保存在Application.LocalUserAppDataPath 路径下,这样的话,即使别人从我的电脑中Copy这个程序,也不会将我的数据库的连接信息给泄露出去。

         因此,我在数据库中定义了一个类ApplicationSettings,该类的目的是保存应用程序的配置信息。完成代码如下:

 public class ApplicationSettings
    {
        public ObservableCollection<Connection> ConnectionList { get; set; }

        public void Save()
        {
            var streamWriter = new StreamWriter(Application.LocalUserAppDataPath + @"\slndbcompare.config", false);
            using (streamWriter)
            {
                var xmlSerializer = new XmlSerializer(typeof(ApplicationSettings));
                xmlSerializer.Serialize(streamWriter, this);
            }
        }


        public static ApplicationSettings Load()
        {
            ApplicationSettings appSettings = null;
            var xmlSerializer = new XmlSerializer(typeof(ApplicationSettings));
            var fi = new FileInfo(Application.LocalUserAppDataPath + @"\slndbcompare.config");
            if (fi.Exists)
            {
                using (FileStream fileStream = fi.OpenRead())
                {
                    appSettings = (ApplicationSettings)xmlSerializer.Deserialize(fileStream);
                }
            }
            if (appSettings==null)
            {
                appSettings = new ApplicationSettings();
            }
            ApplicationContext.Current.Settings = appSettings;
            return appSettings;
        }
    }  

4、数据库连接信息管理

标签:配置   dialog   copy   alignment   panel   sts   tac   sqlserver   wow   

原文地址:http://www.cnblogs.com/jeffrey-net/p/7625490.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!