如果连接字符串没有退出,我尝试创建连接字符串。然后必须创建一个新的连接字符串。我这样试试:
internal static string Search(string data, string customerSchema)
{
string result = String.Empty;
// get search string from the data
XDocument doc = XDocument.Parse(data);
XElement el = doc.Root.Element("search");
if (el == null || String.IsNullOrWhiteSpace(el.Value))
return ConstructFaultResponse("No search parameter found", String.Empty);
// var config = ConfigurationManager.OpenExeConfiguration(DbSchema.ConnectionNameSuffix);
var zipcodeConnection = ConfigurationManager.ConnectionStrings[DbSchema.Sfs];
if (zipcodeConnection == null) {
System.Configuration.ConfigurationManager.ConnectionStrings.Add(new ConnectionStringSettings {
Name = DbSchema.Sfs,
ConnectionString = zipcodeConnection.ConnectionString,
ProviderName = "System.Data.SqlClient" //Depends on the provider, this is for SQL Server
});
}
if (zipcodeConnection != null) {
try {
using (SqlConnection conn = new SqlConnection(zipcodeConnection.ConnectionString)) {
if (el.Value.Length > 6) {
conn.Open();
using (SqlCommand cmd = new SqlCommand(@"select top 1 s.street, s.town
from streets s
join Addresses a on a.streetid=s.id
where a.zipcode=@zip and a.HouseNumber=@nr", conn)) {
cmd.Parameters.AddWithValue("zip", el.Value.Substring(0, 6));
cmd.Parameters.AddWithValue("nr", el.Value.Substring(6));
using (SqlDataReader reader = cmd.ExecuteReader()) {
if (reader.Read()) {
XElement xResponse = new XElement("response",
new XElement("straatnaam", reader.GetString(0)),
new XElement("plaatsnaam", reader.GetString(1))
);
return xResponse.ToString();
}
}
}
}
}
return ConstructFaultResponse("Unknown address", "unknown_address");
}
catch (Exception e) {
return ConstructFaultResponse(e.Message, "BAG_Zipcode_error");
}
}
但问题出在这段代码之后:
if (zipcodeConnection == null) {
System.Configuration.ConfigurationManager.ConnectionStrings.Add(new ConnectionStringSettings {
Name = DbSchema.Sfs,
ConnectionString = zipcodeConnection.ConnectionString,
ProviderName = "System.Data.SqlClient" //Depends on the provider, this is for SQL Server
});
它不会跳到这个:
if (zipcodeConnection != null) {
谢谢
Oke,我改为:
var zipcodeConnection = ConfigurationManager.ConnectionStrings["BAG_Zipcodes"];
if (zipcodeConnection == ConfigurationManager.ConnectionStrings["BAG_Zipcodes"])
zipcodeConnection = ConfigurationManager.ConnectionStrings["BAG_Zipcodes"];
else if (zipcodeConnection != ConfigurationManager.ConnectionStrings["BAG_Zipcodes"]) {
zipcodeConnection = ConfigurationManager.ConnectionStrings[DbSchema.DefaultConnectionName];
}
但它永远不会出现在这个问题上:
else if (zipcodeConnection != ConfigurationManager.ConnectionStrings["BAG_Zipcodes"]) {
zipcodeConnection = ConfigurationManager.ConnectionStrings[DbSchema.DefaultConnectionName];
}
谢谢
我有这样的连接字符串:
<add name="BAG_Zipcodes" connectionString="Password=;Persist Security Info=True;User ID=sa;Initial Catalog=e;Data Source=" />
因此,如果它在webconfig中具有该连接字符串,则使用该连接字符串。否则使用:
zipcodeConnection = ConfigurationManager.ConnectionStrings[DbSchema.DefaultConnectionName];
答案 0 :(得分:0)
您的zipcodeConnection从未设置过值,因此它仍然为空。
你不需要像你一样检查声明是否两次。我建议就这样做
if (zipcodeConnection == null)
{
// Set the zipCodeConnection's value
}
try {
using (...) {
}
}
在此处回答您的编辑问题:
因为您的zipcodeConnection始终是
zipcodeConnection == ConfigurationManager.ConnectionStrings["BAG_Zipcodes"]
这就是为什么它总是跳过
if (zipcodeConnection != ConfigurationManager.ConnectionStrings["BAG_Zipcodes"])
答案 1 :(得分:0)
我这样解决了:
var zipcodeConnection = ConfigurationManager.ConnectionStrings["BAG_Zipcodes"];
zipcodeConnection = ConfigurationManager.ConnectionStrings[DbSchema.DefaultConnectionName];
if (zipcodeConnection == ConfigurationManager.ConnectionStrings["BAG_Zipcodes"])
zipcodeConnection = ConfigurationManager.ConnectionStrings["BAG_Zipcodes"];
if (zipcodeConnection == ConfigurationManager.ConnectionStrings[DbSchema.DefaultConnectionName])
zipcodeConnection = ConfigurationManager.ConnectionStrings[DbSchema.DefaultConnectionName];
但它可以简明扼要吗?
谢谢
答案 2 :(得分:0)
我不确定你要做什么但是我建议你尝试设置zipcodeConnectionString如果它为null所以你需要做这样的事情
var zipcodeConnection = ConfigurationManager.ConnectionStrings[DbSchema.DefaultConnectionName];
if (zipcodeConnection == null)
{
zipcodeConnection = ConfigurationManager.ConnectionStrings["BAG_Zipcodes"]
}