web版的sql server附加器

其实现在软件或web应用程序好多都要sql server, 本来对程序员用企业管理器就可以附加进去;
这个附加器打开web页而就可以进行附加,只要把该页嵌入到站点下,就可以实现与企业管理器相同的功能!
该附加器主要针对mdf和日志文件进行附加;
只需选择mdf日志文件程序自动查找!

完整代码
  源代码下载

 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 13 {
1414    public string sqlServer;
1515    public string uid;
1616    public string pwd;
1717    public string sqlstr;
1818    protected void Page_Load(object sender, EventArgs e)
1919    {
2020      
2121    }

2222    protected void btnStup_Click(object sender, EventArgs e)
2323    {
2424        string fileExtend = Path.GetExtension(File_MDF.PostedFile.FileName);//获取扩展名
2525        string file_Name = Path.GetFileName(File_MDF.PostedFile.FileName); //获取文件名
2626       
2727        if (fileExtend.ToUpper().CompareTo(".MDF"== 0)
2828        {
2929           // Response.Write(fileExtend.ToString()+"<br>名:"+file_Name.ToString()+"<br>"+File_MDF.Value);         
3030            string fileName=file_Name.Substring(0,file_Name.LastIndexOf("_",file_Name.Length));//得到正确名称
3131            string logname = File_MDF.Value.Substring(0, File_MDF.Value.LastIndexOf("_",File_MDF.Value.Length)) + "_Log.LDF";
3232            string logname1 = File_MDF.Value.Substring(0, File_MDF.Value.LastIndexOf("_",File_MDF.Value.Length)) + "_log.ldf";
3333            if (File.Exists(logname)||File.Exists(logname1))
3434            {
3535                if (rdosql.Checked)
3636                {
3737                    sqlServer = "server=" + txtServer.Text + ";User id=" + txtuid.Text + ";Pwd=" + txtpwd.Text + "";
3838                }

3939                else
4040                {
4141                    sqlServer = "server=" + txtServer.Text + ";uid="+uid+"";
4242                }

4343                SqlConnection sqlCon = new SqlConnection(sqlServer);
4444                sqlCon.Open();
4545                try
4646                {
4747                    string str = "EXEC sp_attach_db @dbname=N'" + fileName + "',@filename1=N'" + File_MDF.Value + "',@filename2=N'" + logname + "'";
4848                    SqlCommand cmd = new SqlCommand(str, sqlCon);
4949                    cmd.ExecuteNonQuery();
5050                }

5151                catch (SqlException exc)
5252                {
5353                    if (chkAppend.Checked)
5454                    {
5555                        SqlCommand cmd1 = new SqlCommand("EXEC sp_detach_db @dbname='" + fileName + "';" + "EXEC sp_attach_single_file_db @dbname='" + fileName + "',@physname='" + File_MDF.Value + "'", sqlCon);
5656                        cmd1.ExecuteNonQuery();
5757                        Response.Write("<script>alert('数据附加成功!');</script>");
5858                    }

5959                    else
6060                    {
6161                        Response.Write("<script>alert('数据库未能安装成功  提示:将原有的数据库分离');</script>");
6262                    }

6363                }

6464                finally
6565                {
6666                    if (sqlCon.State == ConnectionState.Open)
6767                        sqlCon.Close(); 
6868                    //搞定,收工!
6969                }

7070                
7171            }

7272            else
7373            {
7474                Response.Write("<script>alert('警告,找不到相应的日志文件!');</script>");
7575            }

7676        }

7777        else
7878        {
7979            Response.Write("<script>alert('选择文件有误!');</script>");
8080        }

8181    }

8282    protected void rdosql_CheckedChanged(object sender, EventArgs e)
8383    {
8484        uid = txtuid.Text;
8585        pwd = txtpwd.Text;
8686    }

8787    protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
8888    {
8989        uid = "integrated security=true;";
9090    }

9191}

92 92
不应该发布在首页,有不对的,请各位高手指点!

转载于:https://www.cnblogs.com/accpxcb/archive/2007/11/27/973937.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值