打包时想把数据库自动安装到sqlserver2000,及修改web.config中数据库链接串,但是打包后安装快结束时报错:未将对象引用设置到对象的实例。点击确定后安装回滚。实在找不出原因来了。大家帮忙看看,谢谢!数据库安装类如下:private void ExecuteSql(string conn,string DatabaseName ,string Sql)
{
SqlConnection myConnection = new SqlConnection(conn);
SqlCommand myCommand = new SqlCommand(Sql,myConnection);
myCommand.Connection.Open();
myCommand.Connection.ChangeDatabase(DatabaseName);
try{myCommand.ExecuteNonQuery();}
catch(Exception ex)
{
throw ex;
}
finally{ myCommand.Connection.Close();} } public override void Install(System.Collections.IDictionary stateSaver)
{
base.Install(stateSaver);
try
{
string connstr=String.Format("data source={0};user id={1};password={2};",this.Context.Parameters["server"],this.Context.Parameters["user"],this.Context.Parameters["pwd"]);
SqlDataReader dr;
SqlConnection sqlconn=new SqlConnection(connstr);
SqlCommand sqlcmd=new SqlCommand("select name From master.dbo.sysdatabases where name='"+this.Context.Parameters["dbname"]+"'",sqlconn);
sqlconn.Open();
dr=sqlcmd.ExecuteReader();
if(!dr.Read()) //如果存在数据库,则不创建新的
{
ExecuteSql(connstr, "master", "CREATE DATABASE " + this.Context.Parameters["dbname"]);
}
dr.Close();
sqlconn.Close();
System.Diagnostics.Process sqlProcess = new System.Diagnostics.Process();
sqlProcess.StartInfo.FileName = "osql.exe ";
sqlProcess.StartInfo.Arguments = String.Format(" -U {0} -P {1} -d {2} -i {3}db.sql", this.Context.Parameters["user"], this.Context.Parameters["pwd"],this.Context.Parameters["dbname"],this.Context.Parameters["targetdir"]);
sqlProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
//throw new InstallException(sqlProcess.StartInfo.Arguments );
sqlProcess.Start();
sqlProcess.WaitForExit();
sqlProcess.Close();
System.IO.FileInfo sqlFileInfo =new System.IO.FileInfo(String.Format("{0}db.sql",this.Context.Parameters["targetdir"]));
if (!sqlFileInfo.Exists) {sqlFileInfo.Delete();}
modify("server="+this.Context.Parameters["server"]+";database="+this.Context.Parameters["dbname"]+";User Id="+this.Context.Parameters["user"]+";password="+this.Context.Parameters["pwd"]+"");
string strSql=string.Format("server={0};user id={1};password={2};Database=master",this.Context.Parameters["server"],this.Context.Parameters["user"],this.Context.Parameters["pwd"]); }
catch(Exception ex )
{
throw ex;
}
}
void modify(string nvalue)
{
string sFile =this.Context.Parameters["targetdir"]+"Web.config";
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(sFile);
XmlNode node = xmldoc.SelectSingleNode("add");
if (node != null)
node.Attributes["value"].Value =nvalue.ToString();
xmldoc.Save(sFile);
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货