如果不存在,创建连接字符串

本文关键字:连接 字符串 创建 如果不 不存在 如果 | 更新日期: 2023-09-27 18:03:11

如果connectionstring不存在,我尝试创建connectionstring。然后必须创建一个新的连接字符串。我试着这样做:

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) {

谢谢

好的,我改成了:

 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中有那个connectionstring,那么使用那个。然后使用:

  zipcodeConnection = ConfigurationManager.ConnectionStrings[DbSchema.DefaultConnectionName];

如果不存在,创建连接字符串

我是这样解决的:

 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];

但它能简洁吗?

谢谢

你的zipcodeConnection从来没有设置值,所以它仍然是空的。

你不需要像你那样检查if语句两次。我建议就这样做

                    if (zipcodeConnection == null) 
                    {
                        // Set the zipCodeConnection's value
                    }
                    try {
                            using (...) {
                                         }
                        }

在这里回答你的编辑问题:

因为你的zipcodeConnection总是

zipcodeConnection == ConfigurationManager.ConnectionStrings["BAG_Zipcodes"]

这就是为什么它总是跳过

if (zipcodeConnection != ConfigurationManager.ConnectionStrings["BAG_Zipcodes"])

我不知道你想做什么但我建议你尝试设置zipcodeConnectionString如果它为空你需要做这样的事情

var zipcodeConnection = ConfigurationManager.ConnectionStrings[DbSchema.DefaultConnectionName];
if (zipcodeConnection == null)
{
   zipcodeConnection = ConfigurationManager.ConnectionStrings["BAG_Zipcodes"]
}