代码导读:|DataDirectory|
对于用户计算机上的应用程序而言,|DataDirectory|变量代表的路径就是应用程序的.exe文件所在位置的路径。
对于使用ClickOnce所部署的应用程序而言,|DataDirectory|变量所代表的路径则是ClickOnce所建立的特殊文件目录。
对于Web应用程序而言,|DataDirectory|变量所代表的路径就是App_Date文件夹。
也可以根据自己的需要设置|DataDirectory|的默认值,代码:AppDomain.CurrentDomain.SetData("DataDirectory",newpath)
方法一:通过master数据库中的存储过程来附加\分离数据库(推荐)
public class DataBaseControl
{
/// <summary>
/// 实例化一个数据库连接对象
/// </summary>
private SqlConnection conn;
/// <summary>
/// 实例化一个新的数据库操作对象Comm
/// </summary>
private SqlCommand comm;
/// <summary>
/// 要操作的数据库名称
/// </summary>
/// <summary>
/// 数据库连接字符串
/// </summary>
private string connectionString;
public string ConnectionString
{
get { return connectionString; }
set { connectionString = value; }
}
/// <summary>
/// SQL操作语句/存储过程
/// </summary>
private string strSQL;
public string StrSQL
{
get { return strSQL; }
set { strSQL = value; }
}
/// <summary>
/// 要操作的数据库名称
/// </summary>
private string dataBaseName;
public string DataBaseName
{
get { return dataBaseName; }
set { dataBaseName = value; }
}
/// <summary>
/// 数据库文件完整地址
/// </summary>
private string dataBase_MDF;
public string DataBase_MDF
{
get { return dataBase_MDF; }
set { dataBase_MDF = value; }
}
/// <summary>
/// 数据库日志文件完整地址
/// </summary>
private string dataBase_LDF;
public string DataBase_LDF
{
get { return dataBase_LDF; }
set { dataBase_LDF = value; }
}
///<summary>
///附加数据库
///</summary>
public void AttachDB()
{
try
{
conn = new SqlConnection(connectionString);
conn.Open();
comm = new SqlCommand();
comm.Connection = conn;
comm.CommandText = "sp_attach_db";//系统数据库master 中的一个附加数据库存储过程。
comm.Parameters.Add(new SqlParameter(@"dbname", SqlDbType.NVarChar));
comm.Parameters[@"dbname"].Value = dataBaseName;
comm.Parameters.Add(new SqlParameter(@"filename1", SqlDbType.NVarChar)); //一个主文件mdf,一个或者多个日志文件ldf,或次要文件ndf
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 detachDB()
{
try
{
conn = new SqlConnection(connectionString);
conn.Open();
comm = new SqlCommand();
comm.Connection = conn;
comm.CommandText = @"sp_detach_db";//系统数据库master 中的一个分离数据库存储过程。
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();
}
}
}
}
//操作界面事件
private void btnAttachDB_Click(object sender, EventArgs e)
{
DataBaseControl DBC = new DataBaseControl();
DBC.ConnectionString = "Data Source=.; Initial Catalog=master;Integrated Security=SSPI";
DBC.DataBaseName = "dbHotelSystem";
DBC.DataBase_MDF = Directory.GetParent(@"Hotel.MDF") + "\\dbHotelSystem.MDF";
DBC.DataBase_LDF = Directory.GetParent(@"Hotel.MDF") + "\\dbHotelSystem_log.ldf";
DBC.AttachDB();
}
private void btnDetachDB_Click(object sender, EventArgs e)
{
DataBaseControl DBC = new DataBaseControl();
DBC.ConnectionString = "Data Source=.; Initial Catalog=master;Integrated Security=SSPI";
DBC.DataBaseName = "dbHotelSystem";
DBC.detachDB();
}
{
/// <summary>
/// 实例化一个数据库连接对象
/// </summary>
private SqlConnection conn;
/// <summary>
/// 实例化一个新的数据库操作对象Comm
/// </summary>
private SqlCommand comm;
/// <summary>
/// 要操作的数据库名称
/// </summary>
/// <summary>
/// 数据库连接字符串
/// </summary>
private string connectionString;
public string ConnectionString
{
get { return connectionString; }
set { connectionString = value; }
}
/// <summary>
/// SQL操作语句/存储过程
/// </summary>
private string strSQL;
public string StrSQL
{
get { return strSQL; }
set { strSQL = value; }
}
/// <summary>
/// 要操作的数据库名称
/// </summary>
private string dataBaseName;
public string DataBaseName
{
get { return dataBaseName; }
set { dataBaseName = value; }
}
/// <summary>
/// 数据库文件完整地址
/// </summary>
private string dataBase_MDF;
public string DataBase_MDF
{
get { return dataBase_MDF; }
set { dataBase_MDF = value; }
}
/// <summary>
/// 数据库日志文件完整地址
/// </summary>
private string dataBase_LDF;
public string DataBase_LDF
{
get { return dataBase_LDF; }
set { dataBase_LDF = value; }
}
///<summary>
///附加数据库
///</summary>
public void AttachDB()
{
try
{
conn = new SqlConnection(connectionString);
conn.Open();
comm = new SqlCommand();
comm.Connection = conn;
comm.CommandText = "sp_attach_db";//系统数据库master 中的一个附加数据库存储过程。
comm.Parameters.Add(new SqlParameter(@"dbname", SqlDbType.NVarChar));
comm.Parameters[@"dbname"].Value = dataBaseName;
comm.Parameters.Add(new SqlParameter(@"filename1", SqlDbType.NVarChar)); //一个主文件mdf,一个或者多个日志文件ldf,或次要文件ndf
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 detachDB()
{
try
{
conn = new SqlConnection(connectionString);
conn.Open();
comm = new SqlCommand();
comm.Connection = conn;
comm.CommandText = @"sp_detach_db";//系统数据库master 中的一个分离数据库存储过程。
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();
}
}
}
}
//操作界面事件
private void btnAttachDB_Click(object sender, EventArgs e)
{
DataBaseControl DBC = new DataBaseControl();
DBC.ConnectionString = "Data Source=.; Initial Catalog=master;Integrated Security=SSPI";
DBC.DataBaseName = "dbHotelSystem";
DBC.DataBase_MDF = Directory.GetParent(@"Hotel.MDF") + "\\dbHotelSystem.MDF";
DBC.DataBase_LDF = Directory.GetParent(@"Hotel.MDF") + "\\dbHotelSystem_log.ldf";
DBC.AttachDB();
}
private void btnDetachDB_Click(object sender, EventArgs e)
{
DataBaseControl DBC = new DataBaseControl();
DBC.ConnectionString = "Data Source=.; Initial Catalog=master;Integrated Security=SSPI";
DBC.DataBaseName = "dbHotelSystem";
DBC.detachDB();
}
方法二:通过Connection 来设置
public bool attachDB()
{
// 利用 SqlConnectionStringBuilder 对象来构建连接字符串。 using System.Data.SqlClient;
SqlConnectionStringBuilder connectionStringBuilder = new SqlConnectionStringBuilder();
connectionStringBuilder.DataSource = ".";
// 使用相对路径的手法来指定所要附加的 SQL Server 数据库文件。
connectionStringBuilder.AttachDBFilename = @"|DataDirectory|test.mdf";
connectionStringBuilder.IntegratedSecurity = true;
//附加数据库
connectionStringBuilder.UserInstance = false;
//从新设定附加至 SQL Server 实例的数据库名称
connectionStringBuilder.InitialCatalog ="test";
try
{
SqlConnection con = new SqlConnection(connectionStringBuilder.ConnectionString);
con.Open();
return true;//附加数据库成功!
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
}
{
// 利用 SqlConnectionStringBuilder 对象来构建连接字符串。 using System.Data.SqlClient;
SqlConnectionStringBuilder connectionStringBuilder = new SqlConnectionStringBuilder();
connectionStringBuilder.DataSource = ".";
// 使用相对路径的手法来指定所要附加的 SQL Server 数据库文件。
connectionStringBuilder.AttachDBFilename = @"|DataDirectory|test.mdf";
connectionStringBuilder.IntegratedSecurity = true;
//附加数据库
connectionStringBuilder.UserInstance = false;
//从新设定附加至 SQL Server 实例的数据库名称
connectionStringBuilder.InitialCatalog ="test";
try
{
SqlConnection con = new SqlConnection(connectionStringBuilder.ConnectionString);
con.Open();
return true;//附加数据库成功!
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
}