基本信息
源码名称:wpf 登陆页面示例源码 用的sqlserver数据库
源码大小:0.03M
文件格式:.rar
开发语言:C#
更新时间:2014-04-18
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;

namespace SQLServerClient
{
    /// <summary>
    /// LoginWindow.xaml 的交互逻辑
    /// </summary>
    public partial class LoginWindow : Window
    {
        public LoginWindow()
        {
            InitializeComponent();
        }
        string[] server_arr = new string[0];

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            string filename = AppDomain.CurrentDomain.BaseDirectory   "\\db.log";
            if (File.Exists(filename))
            {
                string text = File.ReadAllText(filename);
                server_arr = text.Trim('$').Split('$');
                foreach (string line in server_arr)
                {
                    comboBox1.Items.Add(line.Split('|')[0]);
                }
            }
        }

        //登录
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Connection conn = new Connection();
            conn.Server = comboBox1.Text.Trim();
            conn.Database = textBox1.Text.Trim();
            conn.UID = textBox2.Text.Trim();
            conn.PWD = textBox3.Text.Trim();

            DBHelper db = new DBHelper(conn.ToString());
            try
            {
                db.GetValue("select 1");
                LogServer(conn);
                App.dbConnection = conn;
                MainWindow win = new MainWindow();
                win.Show();
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        //取消
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
            Application.Current.Shutdown();
        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                button1_Click(null, null);
            }
        }
        private void LogServer(Connection conn)
        {
            var oldconn = GetConnection(conn.Server);
            if (oldconn != null) return;

            string line = string.Format("${0}|{1}|{2}|{3}"
                , conn.Server
                , conn.Database
                , conn.UID
                , conn.PWD);

            string filename = AppDomain.CurrentDomain.BaseDirectory   "\\db.log";
            if (File.Exists(filename))
            {
                File.Create(filename);
            }
            File.AppendAllText(filename, line);
        }

        private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var conn = GetConnection(comboBox1.SelectedItem.ToString());
            if (conn != null)
            {
                textBox1.Text = conn.Database;
                textBox2.Text = conn.UID;
                textBox3.Text = conn.PWD;
            }
        }
        private Connection GetConnection(string server)
        {
            if (server_arr.Length == 0) return null;
            foreach (string line in server_arr)
            {
                string[] ss = line.Split('|');
                if (ss[0].ToLower() == server.ToLower())
                {
                    Connection conn = new Connection();
                    conn.Server = ss[0];
                    conn.Database = ss[1];
                    conn.UID = ss[2];
                    conn.PWD = ss[3];
                    return conn;
                }
            }
            return null;
        }
    }
}