initiate sp_databases for a c# listbox
I have found that using sp_databases
, it might possibly be able to populate a ListBox
with all the databases on my remote server.
How I would incorporate 开发者_开发知识库that code on my form to get it to work properly?
From this namespace:
using System.Data.SqlClient;
Here is a solution:
using (SqlConnection cn = new SqlConnection("connection string to yer database"))
{
SqlCommand cm = new SqlCommand("exec sp_databases", cn);
SqlDataReader rdr;
cn.Open();
rdr = cm.ExecuteReader();
if (rdr.HasRows())
{
while (rdr.Read())
{
listBox1.Items.Add(rdr["DATABASE_NAME"].ToString());
}
}
rdr.Close();
}
As for connection string. Here is a good reference:
http://www.connectionstrings.com/sql-server-2008
精彩评论