我自己寫的一個操作資料庫的類

來源:互聯網
上載者:User

我自己寫的一個操作資料庫的類 using System;using System.Collections.Generic;using System.Windows.Forms;using System.Data.SqlClient;using System.Data;using System.ServiceProcess; namespace DataBaseOperation{    /// <summary>     /// 資料庫操作控制類     /// </summary>     public class DataBaseControl    {         #region 資料庫操作的各種欄位         /// <summary>         /// 資料庫連接字串         /// </summary>         public string ConnectionString;         /// <summary>         /// SQL動作陳述式/預存程序         /// </summary>         public string StrSQL;         /// <summary>         /// 執行個體化一個資料庫連接對象         /// </summary>         private SqlConnection Conn;         /// <summary>         /// 執行個體化一個新的資料庫操作對象Comm         /// </summary>         private SqlCommand Comm;         /// <summary>         /// 要操作的資料庫名稱         /// </summary>         public string DataBaseName;         /// <summary>         /// 資料庫檔案完整地址         /// </summary>         public string DataBase_MDF;         /// <summary>         /// 資料庫記錄檔完整地址         /// </summary>         public string DataBase_LDF;         /// <summary>         /// 備份檔案名         /// </summary>         public string DataBaseOfBackupName;         /// <summary>         /// 備份檔案路徑         /// </summary>         public string DataBaseOfBackupPath;         #endregion         #region 資料庫操作的各種方法         /// <summary>         /// 執行建立/修改資料庫和表的操作         /// </summary>         public void DataBaseAndTableControl()        {            try            {                Conn = new SqlConnection(ConnectionString);                Conn.Open();                 Comm = new SqlCommand();                Comm.Connection = Conn;                Comm.CommandText = StrSQL;                Comm.CommandType = CommandType.Text;                Comm.ExecuteNonQuery();                 //MessageBox.Show("資料庫操作成功!", "資訊提示", MessageBoxButtons.OK, MessageBoxIcon.Information);            }            catch (Exception ex)            {                MessageBox.Show(ex.Message, "資訊提示", MessageBoxButtons.OK, MessageBoxIcon.Information);            }            finally            {                Conn.Close();            }        }         /// <summary>         /// 附加資料庫         /// </summary>         public void AddDataBase()        {            try            {                Conn = new SqlConnection(ConnectionString);                Conn.Open();                 Comm = new SqlCommand();                Comm.Connection = Conn;                Comm.CommandText = "sp_attach_db";                 Comm.Parameters.Add(new SqlParameter(@"dbname", SqlDbType.NVarChar));                Comm.Parameters[@"dbname"].Value = DataBaseName;                Comm.Parameters.Add(new SqlParameter(@"filename1", SqlDbType.NVarChar));                Comm.Parameters[@"filename1"].Value = DataBase_MDF;                Comm.Parameters.Add(new SqlParameter(@"filename2", SqlDbType.NVarChar));                Comm.Parameters[@"filename2"].Value = DataBase_LDF;                 Comm.CommandType = CommandType.StoredProcedure;                Comm.ExecuteNonQuery();                 //MessageBox.Show("附加資料庫成功", "資訊提示", MessageBoxButtons.OK, MessageBoxIcon.Information);            }            catch (Exception ex)            {                MessageBox.Show(ex.Message, "資訊提示", MessageBoxButtons.OK, MessageBoxIcon.Information);            }            finally            {                Conn.Close();            }        }         /// <summary>         /// 分離資料庫         /// </summary>         public void DeleteDataBase()        {            try            {                Conn = new SqlConnection(ConnectionString);                Conn.Open();                 Comm = new SqlCommand();                Comm.Connection = Conn;                Comm.CommandText = @"sp_detach_db";                 Comm.Parameters.Add(new SqlParameter(@"dbname", SqlDbType.NVarChar));                Comm.Parameters[@"dbname"].Value = DataBaseName;                 Comm.CommandType = CommandType.StoredProcedure;                Comm.ExecuteNonQuery();                 //MessageBox.Show("分離資料庫成功", "資訊提示", MessageBoxButtons.OK, MessageBoxIcon.Information);            }            catch (Exception ex)            {                MessageBox.Show(ex.Message, "資訊提示", MessageBoxButtons.OK, MessageBoxIcon.Information);            }            finally            {                Conn.Close();            }        }         /// <summary>         /// 備份資料庫         /// </summary>         public void BackupDataBase()        {            try            {                Conn = new SqlConnection(ConnectionString);                Conn.Open();                 Comm = new SqlCommand();                Comm.Connection = Conn;                Comm.CommandText = "use master;backup database @dbname to disk = @backupname;";                 Comm.Parameters.Add(new SqlParameter(@"dbname", SqlDbType.NVarChar));                Comm.Parameters[@"dbname"].Value = DataBaseName;                Comm.Parameters.Add(new SqlParameter(@"backupname", SqlDbType.NVarChar));                Comm.Parameters[@"backupname"].Value = @DataBaseOfBackupPath + @DataBaseOfBackupName;                 Comm.CommandType = CommandType.Text;                Comm.ExecuteNonQuery();                 //MessageBox.Show("備份資料庫成功", "資訊提示", MessageBoxButtons.OK, MessageBoxIcon.Information);            }            catch (Exception ex)            {                MessageBox.Show(ex.Message, "資訊提示", MessageBoxButtons.OK, MessageBoxIcon.Information);            }            finally            {                Conn.Close();            }        }         /// <summary>         /// 還原資料庫         /// </summary>         public void ReplaceDataBase()        {            try            {                string BackupFile = @DataBaseOfBackupPath + @DataBaseOfBackupName;                Conn = new SqlConnection(ConnectionString);                Conn.Open();                 Comm = new SqlCommand();                Comm.Connection = Conn;                Comm.CommandText = "use master;restore database @DataBaseName From disk = @BackupFile with replace;";                 Comm.Parameters.Add(new SqlParameter(@"DataBaseName", SqlDbType.NVarChar));                Comm.Parameters[@"DataBaseName"].Value = DataBaseName;                Comm.Parameters.Add(new SqlParameter(@"BackupFile", SqlDbType.NVarChar));                Comm.Parameters[@"BackupFile"].Value = BackupFile;                 Comm.CommandType = CommandType.Text;                Comm.ExecuteNonQuery();                 //MessageBox.Show("還原資料庫成功", "資訊提示", MessageBoxButtons.OK, MessageBoxIcon.Information);            }            catch (Exception ex)            {                MessageBox.Show(ex.Message, "資訊提示", MessageBoxButtons.OK, MessageBoxIcon.Information);            }            finally            {                Conn.Close();            }        }         #endregion         #region 調用的方法        /*  ///調用案例:       還原資料庫  private void button0_Click(object sender, EventArgs e)  {  DataBaseControl DBC = new DataBaseControl();  DBC.ConnectionString = "Data Source=(local);User id=sa;Password=123456; Initial Catalog=master";  DBC.DataBaseName = "MyDatabase";  DBC.DataBaseOfBackupName = @"back.bak";  DBC.DataBaseOfBackupPath = @"D:/Program Files/Microsoft SQL Server/MSSQL/Data/";  DBC.ReplaceDataBase();  }       附加資料庫  private void button1_Click_1(object sender, EventArgs e)  {  DataBaseControl DBC = new DataBaseControl();  DBC.ConnectionString = "Data Source=(local);User id=sa;Password=123456; Initial Catalog=master";  DBC.DataBaseName = "MyDatabase";  DBC.DataBase_MDF = @"D:/Program Files/Microsoft SQL Server/MSSQL/Data/MyDatabase_Data.MDF";  DBC.DataBase_LDF = @"D:/Program Files/Microsoft SQL Server/MSSQL/Data/MyDatabase_Log.LDF";  DBC.AddDataBase();  }       備份資料庫  private void button2_Click(object sender, EventArgs e)  {  DataBaseControl DBC = new DataBaseControl();  DBC.ConnectionString = "Data Source=(local);User id=sa;Password=123456; Initial Catalog=master";  DBC.DataBaseName = "MyDatabase";  DBC.DataBaseOfBackupName = @"back.bak";  DBC.DataBaseOfBackupPath = @"D:/Program Files/Microsoft SQL Server/MSSQL/Data/";  DBC.BackupDataBase();  }       分離資料庫  private void button3_Click(object sender, EventArgs e)  {  DataBaseControl DBC = new DataBaseControl();  DBC.ConnectionString = "Data Source=(local);User id=sa;Password=123456; Initial Catalog=master";  DBC.DataBaseName = "MyDatabase";  DBC.DeleteDataBase();  }  */        #endregion     }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.