正在部署一个WINFORM项目,在项目下面有一个App.config文件,用于存在数据库连接字符串。
配置文件中代码如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="connectionstring" value="User ID=sa;Data Source=.;Password=123;Initial Catalog=test;" />
</appSettings>
</configuration>在安装程序类先执行数据库脚本文件,再修改配置文件 其代码如下:
//重写Install方法
public override void Install(System.Collections.IDictionary stateSaver)
{
base.Install(stateSaver);
try
{
//创建数据库
string connStr = string.Format("data source={0}; user id={1}; password={2}; persist security info = false; packet size=4096",
this.Context.Parameters["server"], this.Context.Parameters["User"],
this.Context.Parameters["pwd"]);
ExecuteSql(connStr, "master", "create database " + this.Context.Parameters["dbname"]);
//创建一个进程用来执行sql脚本---建表
System.Diagnostics.Process sqlProcess = new System.Diagnostics.Process();
sqlProcess.StartInfo.FileName = "cmd.exe ";
sqlProcess.StartInfo.UseShellExecute = false;
sqlProcess.StartInfo.RedirectStandardInput = true;
sqlProcess.StartInfo.RedirectStandardOutput = true;
sqlProcess.StartInfo.RedirectStandardError = true;
sqlProcess.StartInfo.CreateNoWindow = true;
string Path = Application.StartupPath.ToString();
string UserName = this.Context.Parameters["user"];
string Pwd = this.Context.Parameters["pwd"];
//string Path = this.Context.Parameters["targetdir"];
string Parameter = string.Format("osql -U {0} -P {1} -i {2}",
this.Context.Parameters["user"], this.Context.Parameters["pwd"],
"\""+this.Context.Parameters["targetdir"] + "HospitalSql.sql\"");
//设置该进程要启动的文档是"osql.exe",这个执行文件用来在命令行执行sql脚本文件
//sqlProcess.StartInfo.FileName = "osql.exe";
//配置进程需要的参数
//Context.Parameters--获取在运行InstallUtil.exe时输入的命令行参数
//sqlProcess.StartInfo.Arguments = string.Format("-U {0} -P {1} -i {2}HospitalSql.sql",
// this.Context.Parameters["user"], this.Context.Parameters["pwd"],
// this.Context.Parameters["targetdir"]);
sqlProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
sqlProcess.Start();
sqlProcess.StandardInput.WriteLine(Parameter);
sqlProcess.StandardInput.WriteLine("exit ");
sqlProcess.Close();
string exePath =@"C:\KYP\Myproject\Login\Login.exe.config";
Configuration config = ConfigurationManager.OpenExeConfiguration(exePath);//加载配置文件
config.AppSettings.Settings["connectionstring"].Value =
string.Format("User ID={0};Data Source={1};Password={2};Initial Catalog={3};",
this.Context.Parameters["User"], this.Context.Parameters["server"], this.Context.Parameters["pwd"], this.Context.Parameters["dbname"]);//向配置文件中的连接结点填写新的数据库连接字符串。
config.Save();//保存配置文件
}
catch (Exception ex)
{
Console.Write(ex.ToString());
MessageBox.Show("error:"+ex.ToString());
}
}
最后报错为:未将对象引用设置到实例。请教各位,在安装部署过程中该怎么修改配置文件。
我想这里报错的原因是:在执行修改配置文件的代码过程中,文件Login.exe.config还未生成。所以无法加载进行修改。
Login.exe.config是已经安装后的路径下的配置文件。是修改这个吗?还是修改源代码中的配置文件,那么应该怎么改。
配置文件中代码如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="connectionstring" value="User ID=sa;Data Source=.;Password=123;Initial Catalog=test;" />
</appSettings>
</configuration>在安装程序类先执行数据库脚本文件,再修改配置文件 其代码如下:
//重写Install方法
public override void Install(System.Collections.IDictionary stateSaver)
{
base.Install(stateSaver);
try
{
//创建数据库
string connStr = string.Format("data source={0}; user id={1}; password={2}; persist security info = false; packet size=4096",
this.Context.Parameters["server"], this.Context.Parameters["User"],
this.Context.Parameters["pwd"]);
ExecuteSql(connStr, "master", "create database " + this.Context.Parameters["dbname"]);
//创建一个进程用来执行sql脚本---建表
System.Diagnostics.Process sqlProcess = new System.Diagnostics.Process();
sqlProcess.StartInfo.FileName = "cmd.exe ";
sqlProcess.StartInfo.UseShellExecute = false;
sqlProcess.StartInfo.RedirectStandardInput = true;
sqlProcess.StartInfo.RedirectStandardOutput = true;
sqlProcess.StartInfo.RedirectStandardError = true;
sqlProcess.StartInfo.CreateNoWindow = true;
string Path = Application.StartupPath.ToString();
string UserName = this.Context.Parameters["user"];
string Pwd = this.Context.Parameters["pwd"];
//string Path = this.Context.Parameters["targetdir"];
string Parameter = string.Format("osql -U {0} -P {1} -i {2}",
this.Context.Parameters["user"], this.Context.Parameters["pwd"],
"\""+this.Context.Parameters["targetdir"] + "HospitalSql.sql\"");
//设置该进程要启动的文档是"osql.exe",这个执行文件用来在命令行执行sql脚本文件
//sqlProcess.StartInfo.FileName = "osql.exe";
//配置进程需要的参数
//Context.Parameters--获取在运行InstallUtil.exe时输入的命令行参数
//sqlProcess.StartInfo.Arguments = string.Format("-U {0} -P {1} -i {2}HospitalSql.sql",
// this.Context.Parameters["user"], this.Context.Parameters["pwd"],
// this.Context.Parameters["targetdir"]);
sqlProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
sqlProcess.Start();
sqlProcess.StandardInput.WriteLine(Parameter);
sqlProcess.StandardInput.WriteLine("exit ");
sqlProcess.Close();
string exePath =@"C:\KYP\Myproject\Login\Login.exe.config";
Configuration config = ConfigurationManager.OpenExeConfiguration(exePath);//加载配置文件
config.AppSettings.Settings["connectionstring"].Value =
string.Format("User ID={0};Data Source={1};Password={2};Initial Catalog={3};",
this.Context.Parameters["User"], this.Context.Parameters["server"], this.Context.Parameters["pwd"], this.Context.Parameters["dbname"]);//向配置文件中的连接结点填写新的数据库连接字符串。
config.Save();//保存配置文件
}
catch (Exception ex)
{
Console.Write(ex.ToString());
MessageBox.Show("error:"+ex.ToString());
}
}
最后报错为:未将对象引用设置到实例。请教各位,在安装部署过程中该怎么修改配置文件。
我想这里报错的原因是:在执行修改配置文件的代码过程中,文件Login.exe.config还未生成。所以无法加载进行修改。
Login.exe.config是已经安装后的路径下的配置文件。是修改这个吗?还是修改源代码中的配置文件,那么应该怎么改。
ConnString = new OleDbConnection(@"provider=microsoft.jet.oledb.4.0;"
+ @"data source="
+ System.Environment.CurrentDirectory
+ "c:\你的数据库名.mdb");
一定要 new 一哈!
每次打开连接前都要这条语句!你可以把它写在一个方法里面,调用它就行了!
<configuration>
<appSettings>
<add key="connectionstring" value="User ID={0};Data Source={1};Password={2};Initial Catalog={3};" />
</appSettings>
</configuration>
拿出来自己格式化,或者生成本地的序列化文件 保存 id,password,database..