您的位置:首页 > 数据库

[原创]闲来无事,写了个c#的数据库附加工具,现附上源代码

2010-02-14 22:25 375 查看
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Text;
7 using System.Windows.Forms;
8 using System.Data.SqlClient;
9
10 namespace addsql
11 {
12 public partial class frmMain : Form
13 {
14 //服务器连接状态 公共变量
15 public bool Flag = false;
16 public SqlConnection conn = new SqlConnection();
17 public string str = "";
18
19 public frmMain()
20 {
21 InitializeComponent();
22 }
23
24 private void button2_Click(object sender, EventArgs e)
25 {
26 if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
27 {
28 this.txtDataBaseName.Text = this.openFileDialog1.FileName;
29 }
30 }
31
32 private void button3_Click(object sender, EventArgs e)
33 {
34 if (this.openFileDialog2.ShowDialog() == DialogResult.OK)
35 {
36 this.txtRz.Text = this.openFileDialog2.FileName;
37 }
38 }
39
40 private void frmMain_Load(object sender, EventArgs e)
41 {
42
43 }
44
45 private void btnOk_Click(object sender, EventArgs e)
46 {
47 string strServer = this.txtServerName.Text.Trim().ToString();
48 string strUserId = this.txtSa.Text.Trim().ToString();
49 string strPwd = this.txtPwd.Text.Trim().ToString();
50 str = "Data Source="+strServer+";Initial Catalog=master;User ID="+strUserId+";Password="+strPwd+";";
51 try
52 {
53 conn.ConnectionString = str;
54 conn.Open();
55 MessageBox.Show("服务器连接成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
56 Flag = true;
57 }
58 catch
59 {
60 MessageBox.Show("服务器连接失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
61 this.txtServerName.Text = "";
62 this.txtSa.Text = "";
63 this.txtPwd.Text = "";
64 }
65 }
66
67 private void button1_Click(object sender, EventArgs e)
68 {
69 //string databasename = this.txtDataBaseName.Text.Trim().ToString();
70 ////获取真实的数据库名
71 //databasename = databasename.Substring(databasename.LastIndexOf('\\') + 1, databasename.LastIndexOf('.') - databasename.LastIndexOf('\\') - 1);
72 if (Flag == false)
73 {
74 MessageBox.Show("请先连接服务器", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
75 }
76 else
77 {
78 //开始附加数据库代码
79 try
80 {
81 if (conn.State == ConnectionState.Closed)
82 {
83 conn.Open();
84 }
85 SqlCommand cmd = new SqlCommand();
86 cmd.Connection = conn;
87 cmd.CommandText = "sp_attach_db";
88 cmd.Parameters.Add(new SqlParameter(@"dbname",SqlDbType.NVarChar));
89 cmd.Parameters[@"dbname"].Value = this.txtName.Text.Trim();
90 cmd.Parameters.Add(new SqlParameter(@"filename1",SqlDbType.NVarChar));
91 cmd.Parameters[@"filename1"].Value = this.txtDataBaseName.Text.Trim();
92 cmd.Parameters.Add(new SqlParameter(@"filename2",SqlDbType.NVarChar));
93 cmd.Parameters[@"filename2"].Value = this.txtRz.Text.Trim();
94 cmd.CommandType = CommandType.StoredProcedure;
95 cmd.ExecuteNonQuery();
96 MessageBox.Show("附加数据库成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
97 }
98 catch(Exception ex)
99 {
100 //关闭数据库连接
101 conn.Close();
102 MessageBox.Show("附加数据库失败!\n"+ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
103 }
104 }
105 }
106 }
107 }
108
源代码打包下载:点这里
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: