Click to See Complete Forum and Search --> : Getting the databases in sqlserver.
asimnazir123
January 20th, 2004, 06:30 AM
I'd like my webApplication to be able to:
1) Present the user with a list of local and linked SQL servers
2) Once a SQL Server is selected, present the user with a
list of available Databases on that Server.
Or
present the user with a list of available Databases on that server.
i am using asp.net and vb.net.
zeeshan.net
January 24th, 2004, 06:33 PM
Hi asimnazir123,
This is very simple to get the all databases list from sql server. You know sql server use Meta Data (Data About Data) and store all database information in one "master" database's table "sysdatabase" so we use this table and get all the fill the dropdownlist of from all sql server databases.
Here is the example code. Simple change the uid and pwd that I use here. Simply change the connection string variable and use this script. In this case I will use "sa" the default username of sql server database and "kyo" my password for sql server.
<%@ Import Namespace="System.Data.SqlClient" %>
<Script Language="VB" Runat="Server">
Sub Page_Load(Sender As Object, E As EventArgs)
Dim StrCon As String = "server=localhost;uid=sa;pwd=kyo;database=master"
Dim ObjCon As New SqlConnection(StrCon)
Dim ObjCmd As New SqlCommand("select name from sysdatabases", ObjCon)
ObjCon.Open()
DrpData.DataSource = ObjCmd.ExecuteReader()
DrpData.DataTextField = "name"
DrpData.DataBind()
ObjCon.Close()
End Sub
</Script>
<html>
<head>
<title>All Sql Server Databases</title>
</head>
<body>
<form runat="server">
<b>Here is all databases are bind in DropDownList.</b>
<asp:DropDownList id="DrpData" runat="server" />
</form>
</body>
</html>
asimnazir123
January 26th, 2004, 02:25 AM
Thanks
ASIM
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.