PDA物流配送系统项目开发过程中如何在PDA端设置服务器连接参数分享

 PDA物流配送系统项目开发过程中如何在PDA端设置服务器连接参数分享

相信大家都会遇到,通过PDA来设置连接参数比如IP  数据库名 和密码 等等 这样就更加灵活的 来配置服务器

 

  public partial class FrmSetServer : Form
    {
        private bool Isckp = false;
       
        public FrmSetServer()
        {
            InitializeComponent();
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.WindowState = System.Windows.Forms.FormWindowState.Normal;

            this.TxtDataName.Text = GlobalVariable.m_DataBase;
            InitServer();
        }

        private void BtnOK_Click(object sender, EventArgs e)
        {
            if (this.TxtServer.Text.Trim().Length <= 0)
            {
                MessageBox.Show("请输入服务器名!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                this.TxtServer.Focus();
                this.TxtServer.SelectAll();
                return;
            }

            if (this.TxtDataName.Text.Trim().Length <= 0)
            {
                MessageBox.Show("请输入数据库名!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                this.TxtDataName.Focus();
                this.TxtDataName.SelectAll();
                return;
            }

            try
            {
                Cursor.Current = Cursors.WaitCursor;
                if (!GetData())
                {
                    Cursor.Current = Cursors.Default;
                    return;
                }
                else
                {
                    Isckp = true;
                    MessageBox.Show("数据库服务器连接成功。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
                }
            }
            catch (Exception ex)
            {
                if (ex.Message == "SqlException")
                {
                    MessageBox.Show("数据库连接失败!" + "\r\n" + ((System.Data.SqlClient.SqlException)(ex)).Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                }
                else
                {
                    MessageBox.Show("数据库连接失败!" + "\r\n" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                }
            }

            FileStream MyFileStream = default(FileStream);
            byte[] WriteText = null;

            try
            {
                //如果系统 参数路径 存在此文件
                if (System.IO.File.Exists(GlobalVariable.AppPath + "\\" + GlobalVariable.m_ServerFile) == false)
                {
                    MyFileStream = File.Open(GlobalVariable.AppPath + "\\" + GlobalVariable.m_ServerFile, FileMode.OpenOrCreate | FileMode.Append, FileAccess.Write);
                    MyFileStream.Close();
                }
                if (System.IO.File.Exists(GlobalVariable.AppPath + "\\" + GlobalVariable.m_ServerFile) == true)
                {
                    MyFileStream = File.Open(GlobalVariable.AppPath + "\\" + GlobalVariable.m_ServerFile, FileMode.OpenOrCreate | FileMode.CreateNew, FileAccess.Write);
                    WriteText = Encoding.ASCII.GetBytes(TxtServer.Text.Trim() + "\r\n" + TxtDataUser.Text.Trim() + "\r\n" + TxtDataPwd.Text.Trim() + "\r\n" + GlobalVariable.m_DataBase.Trim() + "\r\n");

                    MyFileStream.Write(WriteText, 0, (int)WriteText.Length);
                    MyFileStream.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
            }
            Cursor.Current = Cursors.Default;
            MyFileStream = null;

            if (Isckp)
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private bool GetData()
        {
            bool ret = false;
            try
            {
                Cursor.Current = Cursors.WaitCursor;
                GlobalVariable.m_Server = this.TxtServer.Text.Trim();
                GlobalVariable.m_DataUser = this.TxtDataUser.Text.Trim();
                GlobalVariable.m_DataPwd = this.TxtDataPwd.Text.Trim();
                GlobalVariable.m_DataBase = this.TxtDataName.Text.Trim();

                GlobalVariable.m_Conn = "Data Source=" + GlobalVariable.m_Server + ";Initial Catalog=" + GlobalVariable.m_DataBase +
                    ";Persist Security Info=True;User ID=" + GlobalVariable.m_DataUser + ";Password=" + GlobalVariable.m_DataPwd + "";
                GlobalVariable.Init();
                if (GlobalVariable.Conn.State == ConnectionState.Closed && GlobalVariable.m_Server.Trim().Length > 0) GlobalVariable.Conn.Open();

                ret = true;
            }
            catch (Exception ex)
            {
                if (ex.Message == "SqlException")
                {
                    MessageBox.Show("数据库连接失败!" + "\r\n" + ((System.Data.SqlClient.SqlException)(ex)).Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                }
                else
                {
                    MessageBox.Show("数据库连接失败!" + "\r\n" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                }
            }
            return ret;
        }

        private void InitServer()
        {
            FileStream MyFileStream = default(FileStream);
            byte[] WriteText = null;

            try
            {
                //如果系统 参数路径 存在此文件
                if (System.IO.File.Exists(GlobalVariable.AppPath + "\\" + GlobalVariable.m_ServerFile) == true)
                {
                    MyFileStream = System.IO.File.OpenRead(GlobalVariable.AppPath + "\\" + GlobalVariable.m_ServerFile);

                    //将变量读取到byte 的数组中
                    WriteText = new byte[(int)MyFileStream.Length];
                    MyFileStream.Read(WriteText, 0, (int)MyFileStream.Length);
                    string str1 = "";
                    string[] str2 = null;
                    str1 = Encoding.ASCII.GetString(WriteText, 0, WriteText.Length);
                    str2 = str1.Split(("\r\n").ToCharArray());

                    TxtServer.Text = str2[0];
                    TxtDataUser.Text = str2[2];
                    TxtDataPwd.Text = str2[4];
                    MyFileStream.Close();

                    try
                    {
                        if (str2.Length > 6) GlobalVariable.m_DataBase = str2[6].Trim();
                    }
                    catch
                    {
                    }
                    TxtDataName.Text = GlobalVariable.m_DataBase;
                }
                else
                {
                    TxtServer.Text = "";
                    TxtDataUser.Text = "sa";
                    TxtDataPwd.Text = "";
                    TxtDataName.Text = GlobalVariable.m_DataBase;
                    //TxtServer.Focus();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
            }
            MyFileStream = null;
        }

        private void FrmSetServer_Load(object sender, EventArgs e)
        {
            this.TxtServer.Focus();
            this.TxtServer.SelectAll();
        }

        private void TxtPort_GotFocus(object sender, EventArgs e)
        {
            if (this.Visible) IptPan.Enabled = true;
        }

        private void FrmSetServer_Closing(object sender, CancelEventArgs e)
        {
            try
            {
                if (!Isckp) this.DialogResult = DialogResult.Cancel;
            }
            catch
            {
            }
        }

        private void FrmSetServer_Closed(object sender, EventArgs e)
        {
            try
            {
                if (!Isckp) this.DialogResult = DialogResult.Cancel;
                IptPan.Enabled = false;
                Application.DoEvents();
            }
            catch
            {
            }
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void TxtServer_GotFocus(object sender, EventArgs e)
        {
            Application.DoEvents();
            IptPan.Enabled = true;
        }

    }
}

 

界面效果:

 

原创文章,禁止做为商用,转载请注明出处:http://blog.csdn.net/haohantech

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值