这个附加器打开web页而就可以进行附加,只要把该页嵌入到站点下,就可以实现与企业管理器相同的功能!
该附加器主要针对mdf和日志文件进行附加;
只需选择mdf日志文件程序自动查找!
![](https://www.cnblogs.com/images/cnblogs_com/accpxcb/108272/r_sqlAppend.jpg)
完整代码
源代码下载
1
1
using
System;
2
2
using
System.Data;
3
3
using
System.Configuration;
4
4
using
System.Web;
5
5
using
System.Web.Security;
6
6
using
System.Web.UI;
7
7
using
System.Web.UI.WebControls;
8
8
using
System.Web.UI.WebControls.WebParts;
9
9
using
System.Web.UI.HtmlControls;
10
10
using
System.Data.SqlClient;
11
11
using
System.IO;
12
12
public
partial
class
_Default : System.Web.UI.Page
13![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
13
{
14
14 public string sqlServer;
15
15 public string uid;
16
16 public string pwd;
17
17 public string sqlstr;
18
18 protected void Page_Load(object sender, EventArgs e)
19![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
19
{
20
20
21
21 }
22
22 protected void btnStup_Click(object sender, EventArgs e)
23![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
23
{
24
24 string fileExtend = Path.GetExtension(File_MDF.PostedFile.FileName);//获取扩展名
25
25 string file_Name = Path.GetFileName(File_MDF.PostedFile.FileName); //获取文件名
26
26
27
27 if (fileExtend.ToUpper().CompareTo(".MDF") == 0)
28![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
28
{
29
29 // Response.Write(fileExtend.ToString()+"<br>名:"+file_Name.ToString()+"<br>"+File_MDF.Value);
30
30 string fileName=file_Name.Substring(0,file_Name.LastIndexOf("_",file_Name.Length));//得到正确名称
31
31 string logname = File_MDF.Value.Substring(0, File_MDF.Value.LastIndexOf("_",File_MDF.Value.Length)) + "_Log.LDF";
32
32 string logname1 = File_MDF.Value.Substring(0, File_MDF.Value.LastIndexOf("_",File_MDF.Value.Length)) + "_log.ldf";
33
33 if (File.Exists(logname)||File.Exists(logname1))
34![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
34
{
35
35 if (rdosql.Checked)
36![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
36
{
37
37 sqlServer = "server=" + txtServer.Text + ";User id=" + txtuid.Text + ";Pwd=" + txtpwd.Text + "";
38
38 }
39
39 else
40![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
40
{
41
41 sqlServer = "server=" + txtServer.Text + ";uid="+uid+"";
42
42 }
43
43 SqlConnection sqlCon = new SqlConnection(sqlServer);
44
44 sqlCon.Open();
45
45 try
46![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
46
{
47
47 string str = "EXEC sp_attach_db @dbname=N'" + fileName + "',@filename1=N'" + File_MDF.Value + "',@filename2=N'" + logname + "'";
48
48 SqlCommand cmd = new SqlCommand(str, sqlCon);
49
49 cmd.ExecuteNonQuery();
50
50 }
51
51 catch (SqlException exc)
52![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
52
{
53
53 if (chkAppend.Checked)
54![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
54
{
55
55 SqlCommand cmd1 = new SqlCommand("EXEC sp_detach_db @dbname='" + fileName + "';" + "EXEC sp_attach_single_file_db @dbname='" + fileName + "',@physname='" + File_MDF.Value + "'", sqlCon);
56
56 cmd1.ExecuteNonQuery();
57
57 Response.Write("<script>alert('数据附加成功!');</script>");
58
58 }
59
59 else
60![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
60
{
61
61 Response.Write("<script>alert('数据库未能安装成功 提示:将原有的数据库分离');</script>");
62
62 }
63
63 }
64
64 finally
65![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
65
{
66
66 if (sqlCon.State == ConnectionState.Open)
67
67 sqlCon.Close();
68
68 //搞定,收工!
69
69 }
70
70
71
71 }
72
72 else
73![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
73
{
74
74 Response.Write("<script>alert('警告,找不到相应的日志文件!');</script>");
75
75 }
76
76 }
77
77 else
78![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
78
{
79
79 Response.Write("<script>alert('选择文件有误!');</script>");
80
80 }
81
81 }
82
82 protected void rdosql_CheckedChanged(object sender, EventArgs e)
83![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
83
{
84
84 uid = txtuid.Text;
85
85 pwd = txtpwd.Text;
86
86 }
87
87 protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
88![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
88
{
89
89 uid = "integrated security=true;";
90
90 }
91
91}
92
92
不应该发布在首页,有不对的,请各位高手指点!
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/None.gif)
7
![](/Images/OutliningIndicators/None.gif)
8
![](/Images/OutliningIndicators/None.gif)
9
![](/Images/OutliningIndicators/None.gif)
10
![](/Images/OutliningIndicators/None.gif)
11
![](/Images/OutliningIndicators/None.gif)
12
![](/Images/OutliningIndicators/None.gif)
13
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
29
![](/Images/OutliningIndicators/InBlock.gif)
30
![](/Images/OutliningIndicators/InBlock.gif)
31
![](/Images/OutliningIndicators/InBlock.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/InBlock.gif)
34
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
35
![](/Images/OutliningIndicators/InBlock.gif)
36
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
37
![](/Images/OutliningIndicators/InBlock.gif)
38
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
39
![](/Images/OutliningIndicators/InBlock.gif)
40
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
41
![](/Images/OutliningIndicators/InBlock.gif)
42
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
43
![](/Images/OutliningIndicators/InBlock.gif)
44
![](/Images/OutliningIndicators/InBlock.gif)
45
![](/Images/OutliningIndicators/InBlock.gif)
46
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
47
![](/Images/OutliningIndicators/InBlock.gif)
48
![](/Images/OutliningIndicators/InBlock.gif)
49
![](/Images/OutliningIndicators/InBlock.gif)
50
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
51
![](/Images/OutliningIndicators/InBlock.gif)
52
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
53
![](/Images/OutliningIndicators/InBlock.gif)
54
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
55
![](/Images/OutliningIndicators/InBlock.gif)
56
![](/Images/OutliningIndicators/InBlock.gif)
57
![](/Images/OutliningIndicators/InBlock.gif)
58
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
59
![](/Images/OutliningIndicators/InBlock.gif)
60
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
61
![](/Images/OutliningIndicators/InBlock.gif)
62
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
63
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
64
![](/Images/OutliningIndicators/InBlock.gif)
65
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
66
![](/Images/OutliningIndicators/InBlock.gif)
67
![](/Images/OutliningIndicators/InBlock.gif)
68
![](/Images/OutliningIndicators/InBlock.gif)
69
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
70
![](/Images/OutliningIndicators/InBlock.gif)
71
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
72
![](/Images/OutliningIndicators/InBlock.gif)
73
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
74
![](/Images/OutliningIndicators/InBlock.gif)
75
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
76
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
77
![](/Images/OutliningIndicators/InBlock.gif)
78
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
79
![](/Images/OutliningIndicators/InBlock.gif)
80
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
81
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
82
![](/Images/OutliningIndicators/InBlock.gif)
83
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
84
![](/Images/OutliningIndicators/InBlock.gif)
85
![](/Images/OutliningIndicators/InBlock.gif)
86
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
87
![](/Images/OutliningIndicators/InBlock.gif)
88
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
89
![](/Images/OutliningIndicators/InBlock.gif)
90
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
91
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
92
![](/Images/OutliningIndicators/None.gif)