如何使用 c#.net 从组合框中的 NpgsqlConnection 获取所有数据库的列表
How to get list of all database from NpgsqlConnection in a combobox using c#.net
我正在尝试开发一个控制台应用程序,我需要知道所有数据库列表名称。但是这段代码不起作用。这是我的代码:
public static List<string> AllNpgsqlDatabaseName()
{
List<string> li = new List<string>();
NpgsqlConnection connection = new NpgsqlConnection("Host=localhost;Username=root;Password=root;");
connection.Open();
NpgsqlCommand command = connection.CreateCommand();
command.CommandText = "SELECT name from sys.databases";
NpgsqlDataReader Reader;
Reader = command.ExecuteReader();
while (Reader.Read())
{
string row = "";
for (int i = 0; i < Reader.FieldCount; i++)
row += Reader.GetValue(i).ToString() + ", ";
li.Add(row);
}
connection.Close();
return li;
}
你可以运行
SELECT datname FROM pg_database
我正在尝试开发一个控制台应用程序,我需要知道所有数据库列表名称。但是这段代码不起作用。这是我的代码:
public static List<string> AllNpgsqlDatabaseName()
{
List<string> li = new List<string>();
NpgsqlConnection connection = new NpgsqlConnection("Host=localhost;Username=root;Password=root;");
connection.Open();
NpgsqlCommand command = connection.CreateCommand();
command.CommandText = "SELECT name from sys.databases";
NpgsqlDataReader Reader;
Reader = command.ExecuteReader();
while (Reader.Read())
{
string row = "";
for (int i = 0; i < Reader.FieldCount; i++)
row += Reader.GetValue(i).ToString() + ", ";
li.Add(row);
}
connection.Close();
return li;
}
你可以运行
SELECT datname FROM pg_database