/dbname=[DBNAME] /server=[SERVER] /user=[USER] /pwd=[PWD] /targetdir="[TARGETDIR]\"
/// <summary>
/// 附加数据库方法
/// </summary>
/// <param name="strSql">连接数据库字符串,连接master系统数据库</param>
/// <param name="DataName">数据库名字</param>
/// <param name="strMdf">数据库文件MDF的路径</param>
/// <param name="strLdf">数据库文件LDF的路径</param>
/// <param name="path">安装目录</param>
private void CreateDataBase( string strSql, string DataName, string strMdf, string strLdf, string path)
{
SqlConnection myConn = new SqlConnection(strSql);
String str = null ;
try
{
str = " EXEC sp_attach_db @dbname='"+DataName+"',@filename1='"+strMdf+"',@filename2='"+strLdf+"'";
SqlCommand myCommand = new SqlCommand(str, myConn);
myConn.Open();
myCommand.ExecuteNonQuery();
MessageBox.Show("数据库安装成功!点击确定继续");//需Using System.Windows.Forms
}
catch(Exception e)
{
MessageBox.Show("数据库安装失败!" + e.Message+"\n\n"+"您可以手动附加数据");
System.Diagnostics.Process.Start(path);//打开安装目录
}
finally
{
myConn.Close();
}
}
public override void Install(System.Collections.IDictionary stateSaver)
{
string server = this.Context.Parameters["server"];//服务器名称
string uid = this.Context.Parameters["user"];//SQlServer用户名
string pwd = this.Context.Parameters["pwd"];//密码
string path = this.Context.Parameters["targetdir"];//安装目录
string strSql = "server=" + server + ";uid=" + uid + ";pwd=" + pwd + ";database=master";//连接数据库字符串
string DataName = "JXC";//数据库名
string strMdf = path + @"JXC.mdf";//MDF文件路径,这里需注意文件名要与刚添加的数据库文件名一样!
string strLdf = path + @"jxc_log.ldf";//LDF文件路径
base.Install(stateSaver);
this.CreateDataBase(strSql, DataName, strMdf, strLdf, path);//开始创建数据库
}
联系客服