附加数据库

        /// <summary>
        /// 附加数据库
        /// </summary>
        /// <param name="strSql"></param>
        /// <param name="DataName"></param>
        /// <param name="strMdf"></param>
        /// <param name="strLdf"></param>
        /// <param name="path"></param>
        private void CreateDataBase(string strSql, string DataName, string strMdf, string strLdf, string path)
        {
            SqlConnection myConn = new SqlConnection(strSql);
            String str = null;
            try
            {
                str = " EXEC sp_attach_db @dbname='" + DataName + "',@filename1='" + strMdf + "',@filename2='" + strLdf + "'";
                SqlCommand myCommand = new SqlCommand(str, myConn);
                myConn.Open();
                myCommand.ExecuteNonQuery();
               DialogResult r= MessageBox.Show("数据库安装成功!点击确定继续");//需Using System.Windows.Forms
               if (r== DialogResult.OK)
               {
                   UpdateApp(txtLoginName.Text.Trim(), txtLoinPwd.Text.Trim());
                   IsShowNewForm = true;
                   this.Close();
               }
            }
            catch (Exception e)
            {
                MessageBox.Show("数据库安装失败!" + e.Message + "\n\n" + "您可以手动附加数据");
                System.Diagnostics.Process.Start(path);//打开安装目录
            }
            finally
            {
                myConn.Close();
                this.Close();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog FBD = new FolderBrowserDialog();
            FBD.SelectedPath = "选择你要存放目录的路径";
            if (FBD.ShowDialog() == DialogResult.OK)
            {
                txtPath.Text = FBD.SelectedPath;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (IsFree())
            {
                string server = ".";//服务器名称
                string uid = txtLoginName.Text.Trim().ToString();//SQlServer用户名
                string pwd = txtLoinPwd.Text.Trim().ToString();//密码
                string path = txtPath.Text.Trim().ToString();//安装目录
                string strSql = "server=" + server + ";uid=" + uid + ";pwd=" + pwd + ";database=master";//连接数据库字符串
                string DataName = "HeatingFS";//数据库名
                string strMdf = path + @"\\Heating_Data.MDF";//MDF文件路径,这里需注意文件名要与刚添加的数据库文件名一样!
                string strLdf = path + @"\\Heating_Log.LDF";//LDF文件路径
                this.CreateDataBase(strSql, DataName, strMdf, strLdf, path);//开始创建数据库

            }
        }

        public static void UpdateApp(string LoginName, string LoginPwd)
        {
            Configuration appConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (!string.IsNullOrEmpty(LoginName) && !string.IsNullOrEmpty(LoginPwd))
            {
                appConfig.AppSettings.Settings["sqlcon"].Value = "server=.;uid=" + LoginName + ";pwd=" + LoginPwd + ";database=HeatingFS";
            }
            appConfig.Save();

        }

        private void frmCreateDB_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (IsShowNewForm)
            {
                System.Diagnostics.Process.Start(Application.StartupPath + "\\SJTD_HDYX.exe"); 
            }
        }

 

posted on 2012-10-25 14:23  酒歌  阅读(187)  评论(0编辑  收藏  举报