如何为custom db控件创建一个Connection属性? [英] how make a Connection property for custome db control?
问题描述
我创建了一个链接在数据库中的组件,所以应该有一个Connection属性,如下所示:
I made a component that link in database so should have a Connection property as follow :
private string constr;
private SqlConnection con;
...
...
[RefreshProperties(RefreshProperties.All),
DefaultValue(""),
Editor("Microsoft.VSDesigner.Data.SQL.Design.SqlConnectionStringEditor, Microsoft.VSDesigner, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
"System.Drawing.Design.UITypeEditor, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public string ConnectionString
{
get
{
return constr;
}
set
{
constr = value;
if (!this.DesignMode)
{
if (con.State != ConnectionState.Closed)
con.Close();
con.ConnectionString = constr;
if (!this.DesignMode)
con.Open();
}
}
}
现在当我使用这个组件时,每件事都很好,但是当我在设计时打开连接属性组合框时只显示在服务器资源管理器窗口中设置的连接字符串,但我需要选择在app.config中设置的连接字符串,但这不显示。任何人都可以告诉我如何在我的属性组合框中的app.config中添加连接字符串。谢谢
now when I use this component every thing is good but when I open connection property combobox in design time only connectionstring that set in server explorer window shown but I need to choose connectin string that set in app.config but this does not show . anybody can tell me how add connection string in app.config in my property combobox . thanks
推荐答案
public static List<string> ReadAppSettings()
{
List<string> list = new List<string>();
// Get the AppSettings section.
ConnectionStringSettingsCollection connections =
ConfigurationManager.ConnectionStrings;
if (connections.Count != 0)
{
Console.WriteLine();
Console.WriteLine("Using ConnectionStrings property.");
Console.WriteLine("Connection strings:");
// Get the collection elements.
foreach (ConnectionStringSettings connection in
connections)
{
list.Add(connection.Name);
}
return list;
}
else
{
Console.WriteLine();
Console.WriteLine("No connection string is defined.");
Console.WriteLine();
}
}
使用此列表作为组合框的数据源。
从组合框中选择一个值后,获取所选DB键的值。
Use this list as datasource to your combobox.
After selecting a value from combobox, get Value for Selected DB key.
这篇关于如何为custom db控件创建一个Connection属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!