webserice与winform的app.config
我在一个应用程序中引用了一个webserice(app.config中自动把webserice路径写进去)
webserice如果发布路径是活的话.我怎么样在应用程序中修改(应用程序已经发布)
我现在有一个想法,就是用程序本身来修改app.config.可是我不知道如何来修改..
请高手.指点一下..如果不行的话,怎么作啊..
<?xml version= "1.0 " encoding= "utf-8 " ?>
<configuration>
<configSections>
<sectionGroup name= "applicationSettings " type= "System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 " >
<section name= "brith.Properties.Settings " type= "System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 " requirePermission= "false " />
</sectionGroup>
</configSections>
<connectionStrings>
<add name= "brith.Properties.Settings.birdConnectionString " connectionString= "Data Source=SYTDC-JI7TFCDTG;Initial Catalog=bird;Persist Security Info=True;User ID=birth;Password=birth "
providerName= "System.Data.SqlClient " />
</connectionStrings>
<applicationSettings>
<brith.Properties.Settings>
<setting name= "brith_WebReference_Service " serializeAs= "String ">
<value> http://192.168.0.91:9000/Service.asmx </value>
</setting>
<setting name= "brith_WebReference1_Service " serializeAs= "String ">
<value> http://192.168.0.91:9000/Service.asmx </value>
</setting>
</brith.Properties.Settings>
</applicationSettings>
</configuration>
[解决办法]
这是我写的一个通过修改外部ini文件来解决webservices地址转向的问题,可以直接打开ini文件进行修改,也可以通过别的程序来修改。
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data.SqlClient;
using System.Xml;
using System.Data;
using System.Data.Common;
using System.Runtime.InteropServices;
using System.IO;
using System.Text;
[WebService(Namespace = "web ")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
public SqlConnection sqlcon=null;
public Service () {
//如果使用设计的组件,请取消注释以下行
// InitializeComponent();
sqlcon = Connection( "CenterServer ");
}
[WebMethod(Description = "申请ID号 ")]
public string GetValid(string hospital)
{
string newid = Newid();
ExecSql( "insert into validid (id,state,applytime,hospital) values( ' " + newid + " ',0,getdate(), ' "+hospital+ " ') ");
return newid;
}
[WebMethod(Description = "数据处理 ")]
public int UploadData(string id,string sql,int type)
{
if (DbRead( "select count(*) as total from validid where id= ' " + id + " ' ", 0) == "1 ")
{
string getid = DbRead( "select state from validid where id= ' " + id + " ' ", 0);
if (getid == "0 ")
{
if (ExecSql(sql))
{
ExecSql( "update validid set idx=(select max(isnull(idx,0))+1 from validid),state=1,text= ' " + sql.Replace( " ' ", "@replacesinglequotes ") + " ',type= ' "+type+ " ',exectime=getdate() where id= ' " + id + " ' ");
return 1;
}
else
{
return 2;
}
}
else if (getid == "1 ")
{
return 1;
}
else
{
return 2;
}
}
else
{
return 0;
}
}
private bool ExecSql(string sql)
{
DataSet ds = new DataSet();
if (sqlcon == null)
{
return false;
}
SqlCommand cmd = new SqlCommand(sql, sqlcon);
cmd.CommandTimeout = 300;
try
{
cmd.ExecuteNonQuery();
return true;
}
catch
{
return false;
}
}
[WebMethod(Description = "绑定DataSet ")]
public DataSet DBbind(string sql)
{
DataSet ds = new DataSet();
if (sqlcon == null)
{
ds = null;
return ds;
}
SqlDataAdapter adp = new SqlDataAdapter(sql, sqlcon);
adp.Fill(ds);
return ds;
}
private string Newid()
{
DataSet ds = new DataSet();
ds = DBbind( "select newid() as id ");
return ds.Tables[0].Rows[0][ "id "].ToString();
}
private string DbRead(string sql, int col)
{
try
{
DataSet ds = new DataSet();
ds = DBbind(sql);
if (ds == null || ds.Tables.Count <= 0)
{
return " ";
}
else
{
return ds.Tables[0].Rows[0][col].ToString();
}
}
catch
{
return " ";
}
}
private SqlConnection Connection(string section)
{
SqlConnection nwindConn;
try
{
string DateBaseName = IniReadValue(section, "datebasename ", Server.MapPath( "sysini.ini "));
string UserName = IniReadValue(section, "username ", Server.MapPath( "sysini.ini "));
string Pwd = IniReadValue(section, "password ", Server.MapPath( "sysini.ini "));
string Address = IniReadValue(section, "address ", Server.MapPath( "sysini.ini "));
if (DateBaseName.Trim() == " ")
{
nwindConn = null;
return nwindConn;
}
if (Address.Trim() == " ")
{
if (UserName.Trim() == " ")
{
nwindConn = new SqlConnection( "Data Source=localhost;Integrated Security=SSPI; " + "Initial Catalog= " + DateBaseName.Trim() + ";Connect Timeout=45 ");
}
else
{
nwindConn = new SqlConnection( "server=.;database= " + DateBaseName.Trim() + ";uid= " + UserName.Trim() + ";pwd= " + Pwd.Trim() + ";Connect Timeout=45 ");
}
}
else
{
if (UserName.Trim() == " ")
{
nwindConn = new SqlConnection( "Data Source= " + Address.Trim() + ";Integrated Security=SSPI; " + "Initial Catalog= " + DateBaseName.Trim() + ";Connect Timeout=45 ");
}
else
{
nwindConn = new SqlConnection( "server= " + Address.Trim() + ";database= " + DateBaseName.Trim() + ";uid= " + UserName.Trim() + ";pwd= " + Pwd.Trim() + ";Connect Timeout=45 ");
}
}
nwindConn.Open();
if (nwindConn.State.ToString() == "Open ")
{
return nwindConn;
}
else
{
nwindConn = null;
return nwindConn;
}
}
catch
{
nwindConn = null;
return nwindConn;
}
}
#region 读写ini文件 API定义
[DllImport( "kernel32.dll ", EntryPoint = "WritePrivateProfileStringA ")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport( "kernel32.dll ", EntryPoint = "GetPrivateProfileStringA ")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
#endregion
[WebMethod(Description = "写INI文件 ")]
public bool IniWriteValue(string Section, string Key, string Value,string IniPath)
{
try
{
WritePrivateProfileString(Section, Key, Value, IniPath);
return true;
}
catch
{
return false;
}
}
[WebMethod(Description = "读INI文件 ")]
public string IniReadValue(string Section, string Key,string IniPath)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, " ", temp, 255, IniPath);
return temp.ToString();
}
}
[解决办法]
在调用WebService的方法以前加上yourService.Url = yourConfig.WebServiceUrl,然后在你的配置文件中修改webservice的路径即可