[原创]闲来无事,写了个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
源代码打包下载:点这里
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
源代码打包下载:点这里
相关文章推荐
- [原创]C# C/S结构数据库系统框架详解及源代码下载
- c#实现附加分离备份还原数据库(绝对原创)
- 闲来无事,学着用C#写邮箱助手、群发小工具
- 数据库智能提示和源代码管理工具(支持SVN)SQLPrompt_7.0.0.40, RedGate_SQL_SourceControl.V3.0.13.4214
- 常规Java工具,算法,加密,数据库,面试题,源代码分析,解决方案
- (原创)C# 用数据集Dataset批量添加记录到数据库
- 【原创】C#搭建足球赛事资料库与预测平台(2) 数据库与XCode组件
- 一个简单实用的sql数据库查询工具(c#)
- (原创)《Lucene in Action 2nd Edition》Chapter11.2.2中的工具类SearcherManager翻译为C#版本
- C# 附加数据库方法(将SQL Server 数据库文件附加至SQL Server实例)
- Excel导入导出数据库小工具 附全部源代码(1月19日最新修改)
- 数据库小工具(C#)
- C#创建数据库 附加数据库等操作
- 基于AE的个人地理数据库(mdb格式)导入到企业级数据库(sde数据库中)中(C#)[原创]
- c# 将数据库(包括图片)备份到EXCEL中(原创)
- 【技术原创】京东商城价格图片分析解析源代码下载(C#),附演示程序 转
- 数据库编程辅助工具(C#开源)
- 如何在C#代码中实现在Sqlserver2000中添加用户?以及附加数据库?
- C#使用 SQLite 数据库 开发的配置过程及基本操作类,实例程序:工商银行贵金属行情查看小工具