c#

位置:IT落伍者 >> c# >> 浏览文章

C#附加数据库


发布日期:2023年04月15日
 
C#附加数据库

近段时间学习了如何用安装包将数据库一起附加进去代替了以往需要用户手工附加的方式

/// <summary>

/// 数据库操作控制类

/// </summary>

public class DataBaseControl

{

/// <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;

/// <summary>

/// 执行创建/修改数据库和表的操作

/// </summary>

public void DataBaseAndTableControl()

{

try

{

Conn = new SqlConnection(ConnectionString);

ConnOpen();

Comm = new SqlCommand();

CommConnection = Conn;

CommCommandText = StrSQL;

CommCommandType = CommandTypeText;

CommExecuteNonQuery();

MessageBoxShow(数据库操作成功! 信息提示 MessageBoxButtonsOK MessageBoxIconInformation);

}

catch (Exception ex)

{

MessageBoxShow(exMessage 信息提示 MessageBoxButtonsOK MessageBoxIconInformation);

}

finally

{

ConnClose();

}

}

/// <summary>

/// 附加数据库

/// </summary>

public void AddDataBase()

{

try

{

Conn = new SqlConnection(ConnectionString);

ConnOpen();

Comm = new SqlCommand();

CommConnection = Conn;

CommCommandText = @sp_attach_db;

CommParametersAdd(new SqlParameter(@dbname SqlDbTypeNVarChar));

CommParameters[@dbname]Value = DataBaseName;

CommParametersAdd(new SqlParameter(@filename SqlDbTypeNVarChar));

CommParameters[@filename]Value = DataBase_MDF;

CommParametersAdd(new SqlParameter(@filename SqlDbTypeNVarChar));

CommParameters[@filename]Value = DataBase_LDF;

CommCommandType = CommandTypeStoredProcedure; //此处一定要用存储过程还有我也认识到存储过程的参数的写法不同与SQL语句在查询分析器中写附加数据库的语法为exec sp_attach_db @dbname=N数据库名@filename=NMDF文件路径@filename=NLDF文件路径按照我的推测存储过程方式添加参数后它的值直接跟在后面了

CommExecuteNonQuery();

MessageBoxShow(附加数据库成功 信息提示 MessageBoxButtonsOK MessageBoxIconInformation);

}

catch (Exception ex)

{

MessageBoxShow(exMessage 信息提示 MessageBoxButtonsOK MessageBoxIconInformation);

}

finally

{

ConnClose();

}

}

/// <summary>

/// 分离数据库

/// </summary>

public void DeleteDataBase()

{

try

{

Conn = new SqlConnection(ConnectionString);

ConnOpen();

Comm = new SqlCommand();

CommConnection = Conn;

CommCommandText = sp_detach_db;

CommParametersAdd(new SqlParameter(@dbname SqlDbTypeNVarChar));

CommParameters[@dbname]Value = DataBaseName;

CommCommandType = CommandTypeStoredProcedure;

CommExecuteNonQuery();

MessageBoxShow(分离数据库成功 信息提示 MessageBoxButtonsOK MessageBoxIconInformation);

}

catch (Exception ex)

{

MessageBoxShow(exMessage 信息提示 MessageBoxButtonsOK MessageBoxIconInformation);

}

finally

{

ConnClose();

}

}

/// <summary>

/// 备份数据库

/// </summary>

public void BackupDataBase()

{

try

{

Conn = new SqlConnection(ConnectionString);

ConnOpen();

Comm = new SqlCommand();

CommConnection = Conn;

CommCommandText = use master;backup database @dbname to disk = @backupname;;

CommParametersAdd(new SqlParameter(@dbname SqlDbTypeNVarChar));

CommParameters[@dbname]Value = DataBaseName;

CommParametersAdd(new SqlParameter(@backupname SqlDbTypeNVarChar));

CommParameters[@backupname]Value = @DataBaseOfBackupPath + @DataBaseOfBackupName;

//这里添加参数的方式就跟我以前的做法一样只不过他加了@我认识加@是个好习惯防止特殊字符被转义我以后也采用这种方式

CommCommandType = CommandTypeText;

CommExecuteNonQuery();

MessageBoxShow(备份数据库成功 信息提示 MessageBoxButtonsOK MessageBoxIconInformation);

}

catch (Exception ex)

{

MessageBoxShow(exMessage 信息提示 MessageBoxButtonsOK MessageBoxIconInformation);

}

finally

{

ConnClose();

}

}

/// <summary>

/// 还原数据库

/// </summary>

public void ReplaceDataBase()

{

try

{

string BackupFile = @DataBaseOfBackupPath + @DataBaseOfBackupName;

Conn = new SqlConnection(ConnectionString);

ConnOpen();

Comm = new SqlCommand();

CommConnection = Conn;

CommCommandText = use master;restore database @DataBaseName From disk = @BackupFile with replace;;

CommParametersAdd(new SqlParameter(@DataBaseName SqlDbTypeNVarChar));

CommParameters[@DataBaseName]Value = DataBaseName;

CommParametersAdd(new SqlParameter(@BackupFile SqlDbTypeNVarChar));

CommParameters[@BackupFile]Value = BackupFile;

CommCommandType = CommandTypeText;

CommExecuteNonQuery();

MessageBoxShow(还原数据库成功 信息提示 MessageBoxButtonsOK MessageBoxIconInformation);

}

catch (Exception ex)

{

MessageBoxShow(exMessage 信息提示 MessageBoxButtonsOK MessageBoxIconInformation);

}

finally

{

ConnClose();

}

}

}

有了这些方法我就可以在我的安装类中附加数据库了接下来我要学习一下如何让用户安装时有下拉框选择

测试代码如下

private void btnAttach_Click(object sender EventArgs e)

{

DataBaseControl DBC = new DataBaseControl();

DBCConnectionString = Data Source=(local);User id=用户名;Password=密码; Initial Catalog=master;

DBCDataBaseName = LabelLive;

DBCDataBase_MDF = @C:\Program Files\Microsoft SQL Server\MSSQL\MSSQL\Data\LabelLiveMDF;

DBCDataBase_LDF = @C:\Program Files\Microsoft SQL Server\MSSQL\MSSQL\Data\LabelLive_LogLDF;

DBCAddDataBase();

}

private void btnDetach_Click(object sender EventArgs e)

{

DataBaseControl DBC = new DataBaseControl();

DBCConnectionString = Data Source=(local);User id=用户名;Password=密码; Initial Catalog=master;

DBCDataBaseName = LabelLive;

DBCDeleteDataBase();

}

private void btnBackup_Click(object sender EventArgs e)

{

DataBaseControl DBC = new DataBaseControl();

DBCConnectionString = Data Source=(local);User id=用户名;Password=密码; Initial Catalog=master;

DBCDataBaseName = LabelLive;

DBCDataBaseOfBackupName = @LabelLivebak;

DBCDataBaseOfBackupPath = @D:\;

DBCBackupDataBase();

}

private void btnRestore_Click(object sender EventArgs e)

{

DataBaseControl DBC = new DataBaseControl();

DBCConnectionString = Data Source=(local);User id=用户名;Password=密码; Initial Catalog=master;

DBCDataBaseName = LabelLive;

DBCDataBaseOfBackupName = @LabelLivebak;

DBCDataBaseOfBackupPath = @D:\;

DBCReplaceDataBase();

}

               

上一篇:.NET下的多线程编程应用程序域

下一篇:探析C#文件方式读写结构体