学苏州话:C#2005 中枚举局域网中数据库实例的方法

来源:百度文库 编辑:中科新闻网 时间:2024/04/29 14:21:19
小弟以前是学VB的,最近开始学C#,下面这个是我接触的第一个程序,感觉有些地方还不是很上手,望达人相助啊

以下是MSDN中一个枚举的实例,可以在控制台中显示局域网中的数据库实例

using System.Data.Sql;

class Program
{
static void Main()
{
// Retrieve the enumerator instance and then the data.
SqlDataSourceEnumerator instance =
SqlDataSourceEnumerator.Instance;
System.Data.DataTable table = instance.GetDataSources();

// Display the contents of the table.
DisplayData(table);

Console.WriteLine("Press any key to continue.");
Console.ReadKey();
}

private static void DisplayData(System.Data.DataTable table)
{
foreach (System.Data.DataRow row in table.Rows)
{
foreach (System.Data.DataColumn col in table.Columns)
{
Console.WriteLine("{0} = {1}", col.ColumnName, row[col]);
}
Console.WriteLine("============================");
}
}
}

现在我做了一个小窗体,上面只有一个ComboBox控件,我想点击ComboBox的倒三角按钮时程序自动搜寻数据库并将数据库名添加到ComboBox中该怎么做呢?

一般在Load的时候就把数据加载到ComboBox了,而不是等你按的时候

把数据取出来,adp.fill还是datareader随便你。然后给ComboBox的Items负值
//比如 我这里用DataSet
foreach DataRow dr in ds.Rows
{
cb.Items.Add(dr[0])
}