zoukankan      html  css  js  c++  java
  • [原创]闲来无事,写了个c#的数据库附加工具,现附上源代码

      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 

     源代码打包下载:点这里

  • 相关阅读:
    今天整理一下以前各博客网站上的文章
    转一篇详解Excel逻辑函数的文章
    Google中国的首页变化
    [转]在QuantumGrid4.5中手动添加数据
    [转]VISTA服务介绍
    Idiomatic Phrases Game zoj 2750 Dijkstra
    Fleury 求欧拉回路
    QS Network ZOJ 1586 Prim
    Burn the Linked Camp ZOJ 2770 差分约束系统 SPFA
    ZOJ 1092 POJ 2240 Arbitrage Floyd
  • 原文地址:https://www.cnblogs.com/wbcms/p/1668309.html
Copyright © 2011-2022 走看看