jtammyg
asked on
converted code from c# to Vb and getting error
Hi!
I have the following code which intends to get me a listbox of all the databases in a specific sql server. But I am getting an error at the
dim list as list = new list(32) <---- i have no idea what this does...this was converted online through a website.
The error I am getting is:
too few type arguments to 'system.collections.generi c.list(of T)'
any help you can give me on this will be greatly appreciated.
thanks!
Regards,
Tammy
I have the following code which intends to get me a listbox of all the databases in a specific sql server. But I am getting an error at the
dim list as list = new list(32) <---- i have no idea what this does...this was converted online through a website.
The error I am getting is:
too few type arguments to 'system.collections.generi
any help you can give me on this will be greatly appreciated.
thanks!
Regards,
Tammy
Public Shared Function FindDatabases(ByVal server As String) As String()
Dim connectionString As String = (TrustedConnection(server, "master") + ";Connection Timeout=3")
Dim connection As SqlConnection = Nothing
Dim command As SqlCommand = Nothing
Dim reader As SqlDataReader = Nothing
Dim list As List = New List(32)
Try
connection = New SqlConnection(connectionString)
command = New SqlCommand("DECLARE @t TABLE(db sysname NOT NULL PRIMARY KEY) INSERT @t(db) SELECT name FROM master.dbo.sysdataba" & _
"ses WITH(NOLOCK) WHERE name NOT IN(N'SpecialData',N'master',N'tempdb',N'model',N'msdb') AND" & _
" HAS_DBACCESS(name)=1 SELECT db FROM @t WHERE OBJECT_ID(N'[' + db + N'].dbo.table_version','R') I" & _
"S NOT NULL ORDER BY db", connection)
connection.Open()
reader = command.ExecuteReader((CommandBehavior.CloseConnection Or CommandBehavior.SingleResult))
While reader.Read
list.Add(reader.GetString(0))
End While
Finally
If ((Not (reader) Is Nothing) _
AndAlso Not reader.IsClosed) Then
reader.Close()
End If
If ((Not (connection) Is Nothing) _
AndAlso (connection.State <> ConnectionState.Closed)) Then
connection.Close()
End If
End Try
Return list.ToArray
End Function
What was the original C#?
Try:
Dim list As New list(Of Integer)
More info: http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx
Dim list As New list(Of Integer)
More info: http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx
Humm it seams your're adding string on the list! If so use
Dim list As New list(Of String)
Dim list As New list(Of String)
ASKER
This was the original C# code:
public static string[] FindDatabases(string server)
{
string connectionString = TrustedConnection(server, "master") + ";Connection Timeout=3";
SqlConnection connection = null;
SqlCommand command = null;
SqlDataReader reader = null;
List<string> list = new List<string>(0x20);
try
{
connection = new SqlConnection(connectionSt ring);
command = new SqlCommand("DECLARE @t TABLE(db sysname NOT NULL PRIMARY KEY) INSERT @t(db) SELECT name FROM master.dbo.sysdatabases WITH(NOLOCK) WHERE name NOT IN(N'N'SpecialData'',N'mas ter',N'tem pdb',N'mod el',N'msdb ') AND HAS_DBACCESS(name)=1 SELECT db FROM @t WHERE OBJECT_ID(N'[' + db + N'].dbo.table_version','R' ) IS NOT NULL ORDER BY db", connection);
connection.Open();
reader = command.ExecuteReader(Comm andBehavio r.CloseCon nection | CommandBehavior.SingleResu lt);
while (reader.Read())
{
list.Add(reader.GetString( 0));
}
}
finally
{
if ((reader != null) && !reader.IsClosed)
{
reader.Close();
}
if ((connection != null) && (connection.State != ConnectionState.Closed))
{
connection.Close();
}
}
return list.ToArray();
}
public static string[] FindDatabases(string server)
{
string connectionString = TrustedConnection(server, "master") + ";Connection Timeout=3";
SqlConnection connection = null;
SqlCommand command = null;
SqlDataReader reader = null;
List<string> list = new List<string>(0x20);
try
{
connection = new SqlConnection(connectionSt
command = new SqlCommand("DECLARE @t TABLE(db sysname NOT NULL PRIMARY KEY) INSERT @t(db) SELECT name FROM master.dbo.sysdatabases WITH(NOLOCK) WHERE name NOT IN(N'N'SpecialData'',N'mas
connection.Open();
reader = command.ExecuteReader(Comm
while (reader.Read())
{
list.Add(reader.GetString(
}
}
finally
{
if ((reader != null) && !reader.IsClosed)
{
reader.Close();
}
if ((connection != null) && (connection.State != ConnectionState.Closed))
{
connection.Close();
}
}
return list.ToArray();
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Okay that works...except that the 2 listboxes don't get populated with the servers in the 1 listbox and the databases in the second listbox...but i will post another question for that one.
Thanks a lot jpaulino!!!!!
Tammy
Thanks a lot jpaulino!!!!!
Tammy