基本信息
源码名称:winform开发(安装配置数据库)
源码大小:0.24M
文件格式:.rar
开发语言:C#
更新时间:2017-08-28
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 1 元×
微信扫码支付:1 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
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.SqlClient; namespace SetupSql { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { if (txtMDF.Text == string.Empty) { MessageBox.Show("相应的数据库文件不能为空!!!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } for (int i = 1; i < 100; i = i 1) { progressBar1.Value = 1 i; } SqlConnection con = new SqlConnection("Server=.;User id=sa;pwd="); con.Open(); string fileName = txtMDF.Text.Substring(txtMDF.Text.LastIndexOf("\\") 1); fileName = fileName.Substring(0, fileName.LastIndexOf("_")); string str = "EXEC sp_attach_db @dbname = N'" fileName "', @filename1 = N'" txtMDF.Text "', @filename2 = N'" txtLDF.Text "'"; SqlCommand cmd = new SqlCommand(str, con); try { cmd.ExecuteNonQuery(); } catch (Exception ex) { DialogResult dr; dr = MessageBox.Show(ex.Message "\n是否将当前数据库从 SQL Server 数据库中分离出去,并安装此数据库", "错误提示", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation); if (dr == DialogResult.Yes) { SqlCommand sCmd = new SqlCommand("EXEC sp_detach_db @dbname = '" fileName "';" "EXEC sp_attach_single_file_db @dbname = '" fileName "',@physname = '" txtMDF.Text "'", con); sCmd.ExecuteNonQuery(); MessageBox.Show("安装数据库成功!", "成功提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("数据库未能安装!", "安装提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); progressBar1.Value = 0; } } finally { if (con.State == ConnectionState.Open) con.Close(); } } private void btnWrowseMDF_Click(object sender, EventArgs e) { txtMDF.Text = ""; OpenFileDialog openFile = new OpenFileDialog(); openFile.Filter = "MDF files (*.MDF)|*.MDF|All files (*.*)|*.*"; openFile.ShowDialog(); if (txtMDF.Text == string.Empty) txtMDF.Text = openFile.FileName; } private void btnWrowseLDF_Click(object sender, EventArgs e) { txtLDF.Text = ""; OpenFileDialog openFile = new OpenFileDialog(); openFile.Filter = "LDF files (*.LDF)|*.LDF|All files (*.*)|*.*"; openFile.ShowDialog(); if (txtLDF.Text == string.Empty) txtLDF.Text = openFile.FileName; } private void btnExit_Click(object sender, EventArgs e) { Application.Exit(); } } }