dbTalk Databases Forums  

SQL Server 2005 with VB.net 2005

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss SQL Server 2005 with VB.net 2005 in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Omar Abid
 
Posts: n/a

Default SQL Server 2005 with VB.net 2005 - 06-09-2007 , 07:05 AM






Hi every body,
Im using VB 2005 to create a program that open SQL Data base
The problem that i want to detect the tables of a database
so how can i know a data base tables instantly
Thank you
omar.abid (AT) hotmail (DOT) com
omar.abid2006 (AT) gmail (DOT) com
Omar abid


Reply With Quote
  #2  
Old   
Ed Murphy
 
Posts: n/a

Default Re: SQL Server 2005 with VB.net 2005 - 06-09-2007 , 11:51 AM






Omar Abid wrote:

Quote:
Im using VB 2005 to create a program that open SQL Data base
The problem that i want to detect the tables of a database
so how can i know a data base tables instantly
(When posting to multiple newsgroups, please cross-post to
all at once, rather than multi-posting to each one separately.)

Anyway, Google ("SQL Server 2005" systables) and see if that
points you in the right direction.


Reply With Quote
  #3  
Old   
Karl
 
Posts: n/a

Default Re: SQL Server 2005 with VB.net 2005 - 06-10-2007 , 01:39 AM



There is a view that has a list of all the columns in the whole
database. I believe its information_sys.columns


Reply With Quote
  #4  
Old   
Omar Abid
 
Posts: n/a

Default Re: SQL Server 2005 with VB.net 2005 - 06-10-2007 , 02:38 AM



On 9 juin, 18:51, Ed Murphy <emurph... (AT) socal (DOT) rr.com> wrote:
Quote:
Omar Abid wrote:
Im using VB 2005 to create a program that open SQL Data base
The problem that i want to detect the tables of a database
so how can i know a data base tables instantly

(When posting to multiple newsgroups, please cross-post to
all at once, rather than multi-posting to each one separately.)

Anyway, Google ("SQL Server 2005" systables) and see if that
points you in the right direction.
i didn't know how to do that can u explain more to me



Reply With Quote
  #5  
Old   
Omar Abid
 
Posts: n/a

Default Re: SQL Server 2005 with VB.net 2005 - 06-10-2007 , 02:39 AM



On 10 juin, 08:39, Karl <knelso... (AT) gmail (DOT) com> wrote:
Quote:
There is a view that has a list of all the columns in the whole
database. I believe its information_sys.columns
Thank u for reply but how to do that with VB2005



Reply With Quote
  #6  
Old   
Ed Murphy
 
Posts: n/a

Default Re: SQL Server 2005 with VB.net 2005 - 06-10-2007 , 02:57 AM



Omar Abid wrote:

Quote:
On 9 juin, 18:51, Ed Murphy <emurph... (AT) socal (DOT) rr.com> wrote:
Omar Abid wrote:
Im using VB 2005 to create a program that open SQL Data base
The problem that i want to detect the tables of a database
so how can i know a data base tables instantly
(When posting to multiple newsgroups, please cross-post to
all at once, rather than multi-posting to each one separately.)

Anyway, Google ("SQL Server 2005" systables) and see if that
points you in the right direction.

i didn't know how to do that can u explain more to me
If you don't know how to use Google, then you need more help than
anyone in this newsgroup is likely to be able to provide.


Reply With Quote
  #7  
Old   
Omar Abid
 
Posts: n/a

Default Re: SQL Server 2005 with VB.net 2005 - 06-10-2007 , 03:57 AM



On 10 juin, 09:57, Ed Murphy <emurph... (AT) socal (DOT) rr.com> wrote:
Quote:
Omar Abid wrote:
On 9 juin, 18:51, Ed Murphy <emurph... (AT) socal (DOT) rr.com> wrote:
Omar Abid wrote:
Im using VB 2005 to create a program that open SQL Data base
The problem that i want to detect the tables of a database
so how can i know a data base tables instantly
(When posting to multiple newsgroups, please cross-post to
all at once, rather than multi-posting to each one separately.)

Anyway, Google ("SQL Server 2005" systables) and see if that
points you in the right direction.

i didn't know how to do that can u explain more to me

If you don't know how to use Google, then you need more help than
anyone in this newsgroup is likely to be able to provide.
See man i use google to read news my emails and so on...
For google groups i don't use it so much and then i don't need to know
how it works....
Thank u 4all



Reply With Quote
  #8  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: SQL Server 2005 with VB.net 2005 - 06-10-2007 , 04:06 AM



Omar Abid (omar.abid2006 (AT) gmail (DOT) com) writes:
Quote:
Im using VB 2005 to create a program that open SQL Data base
The problem that i want to detect the tables of a database
so how can i know a data base tables instantly
Your question is very open-ended and it is not clear what you really
want assistance with. Is it running a metadata query in SQL Server? Or
is about data access from VB .Net in general? In the latter case, I would
suggest that you are better off by first learning the basics before you
play with metadata.

Nevertheless, a query you could run to get all tables is this one:

SELECT quotename(schema_name(schema_id)) + '.' + quotename(name)
FROM sys.tables
ORDER BY 1

As for running it from VB - there are many possible variations depending
on what you want to do with the data. Here is a console-mode prorgam that
just prints the table names, and which uses DataAdapater.Fill. It also
includes some error handling.

Imports System.Data
Imports System.Data.Sql
Imports System.Data.SqlClient

Module Bugtest

Private Sub ErrorFill(ByVal sender as Object, ByVal args as FillErrorEventArgs)
Console.WriteLine(args.Errors.Message)
args.Continue = true
End Sub


Private Sub PrintSqlErrors(ByVal errors As SqlErrorCollection, ByVal what As String)
Dim e As SqlError
For Each e In errors
Console.Write (Now.ToString("HH:mm:ss.fff") & " " & what & _
" Message: Msg " & e.Number.ToString() & _
", Severity " & e.Class.ToString() & _
", State: " & e.State.ToString() & _
", Procedure: " & e.Procedure & _
", Line no: " & e.LineNumber.ToString & vbCrLf & _
e.Message & vbCrLf)
Next
End Sub

Private Sub OutputException(ex As Exception)
If TypeOf ex Is SqlException Then
Dim SqlEx As SqlException = DirectCast(ex, SqlException)
PrintSqlErrors(SqlEx.Errors, "Error")
Else
Console.WriteLine(ex.ToString())
End if
End Sub

Private Sub SqlInfoMessage(ByVal sender As Object, ByVal e As SqlInfoMessageEventArgs)
PrintSqlErrors(e.Errors, "INFO MSG")
End Sub

Private Sub PrintDataTable(ByVal tbl As DataTable)
Console.Writeline ("================================================ =========" & vbCrLf)
For Each col As DataColumn In tbl.Columns
Console.Writeline (col.ColumnName & vbTab)
Next col
Console.Writeline (vbCrLf)
For Each row As DataRow In tbl.Rows
For Each col As DataColumn In tbl.Columns
Console.Writeline (row(col).ToString() & vbTab)
Next col
Console.Writeline(vbCrLf)
Next row
End Sub


Public Sub Main()

Dim cn As New SqlConnection, _
strConn As String

' This does not help.
' AddHandler cn.InfoMessage, AddressOf SqlInfoMessage

' Connection string, change server and database!
strConn = "Application Name=systablesdemo;Integrated Security=SSPI;"
strConn &= "Data Source=(local);Initial Catalog=AdventureWorks"

Try
cn.ConnectionString = strConn
cn.Open()
Catch ex As Exception
Console.Writeline(ex.Message, "Connection failed!")
cn = Nothing
Exit Sub
End Try

Dim cmd As SqlCommand = cn.CreateCommand()

cmd.CommandText = "SELECT quotename(schema_name(schema_id)) + "
cmd.CommandText &= " '.' + quotename(name) FROM sys.tables "
cmd.CommandText &= "ORDER BY 1"
Dim dt As New DataTable, _
da As SqlDataAdapter = New SqlDataAdapter(cmd), _
no_of_rows As Integer
AddHandler da.FillError, AddressOf ErrorFill
Try
no_of_rows = da.Fill(dt)
Catch e As Exception
OutputException(e)
End Try
Console.Writeline("No of rows filled " & no_of_rows.ToString() & vbCrLf)
PrintDataTable(dt)

cn.Close()
cn.Dispose()

End Sub

End Module




--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
  #9  
Old   
Ed Murphy
 
Posts: n/a

Default Re: SQL Server 2005 with VB.net 2005 - 06-10-2007 , 03:36 PM



Omar Abid wrote:

Quote:
On 10 juin, 09:57, Ed Murphy <emurph... (AT) socal (DOT) rr.com> wrote:
Omar Abid wrote:
On 9 juin, 18:51, Ed Murphy <emurph... (AT) socal (DOT) rr.com> wrote:
Omar Abid wrote:
Im using VB 2005 to create a program that open SQL Data base
The problem that i want to detect the tables of a database
so how can i know a data base tables instantly
(When posting to multiple newsgroups, please cross-post to
all at once, rather than multi-posting to each one separately.)
Anyway, Google ("SQL Server 2005" systables) and see if that
points you in the right direction.
i didn't know how to do that can u explain more to me
If you don't know how to use Google, then you need more help than
anyone in this newsgroup is likely to be able to provide.

See man i use google to read news my emails and so on...
Do you also use it as a search engine?


Reply With Quote
  #10  
Old   
Omar Abid
 
Posts: n/a

Default Re: SQL Server 2005 with VB.net 2005 - 06-10-2007 , 06:02 PM



On 10 juin, 11:06, Erland Sommarskog <esq... (AT) sommarskog (DOT) se> wrote:
Quote:
Omar Abid (omar.abid2... (AT) gmail (DOT) com) writes:
Im using VB 2005 to create a program that open SQL Data base
The problem that i want to detect the tables of a database
so how can i know a data base tables instantly

Your question is very open-ended and it is not clear what you really
want assistance with. Is it running a metadata query in SQL Server? Or
is about data access from VB .Net in general? In the latter case, I would
suggest that you are better off by first learning the basics before you
play with metadata.

Nevertheless, a query you could run to get all tables is this one:

SELECT quotename(schema_name(schema_id)) + '.' + quotename(name)
FROM sys.tables
ORDER BY 1

As for running it from VB - there are many possible variations depending
on what you want to do with the data. Here is a console-mode prorgam that
just prints the table names, and which uses DataAdapater.Fill. It also
includes some error handling.

Imports System.Data
Imports System.Data.Sql
Imports System.Data.SqlClient

Module Bugtest

Private Sub ErrorFill(ByVal sender as Object, ByVal args as FillErrorEventArgs)
Console.WriteLine(args.Errors.Message)
args.Continue = true
End Sub

Private Sub PrintSqlErrors(ByVal errors As SqlErrorCollection, ByVal what As String)
Dim e As SqlError
For Each e In errors
Console.Write (Now.ToString("HH:mm:ss.fff") & " " & what & _
" Message: Msg " & e.Number.ToString() & _
", Severity " & e.Class.ToString() & _
", State: " & e.State.ToString() & _
", Procedure: " & e.Procedure & _
", Line no: " & e.LineNumber.ToString & vbCrLf & _
e.Message & vbCrLf)
Next
End Sub

Private Sub OutputException(ex As Exception)
If TypeOf ex Is SqlException Then
Dim SqlEx As SqlException = DirectCast(ex, SqlException)
PrintSqlErrors(SqlEx.Errors, "Error")
Else
Console.WriteLine(ex.ToString())
End if
End Sub

Private Sub SqlInfoMessage(ByVal sender As Object, ByVal e As SqlInfoMessageEventArgs)
PrintSqlErrors(e.Errors, "INFO MSG")
End Sub

Private Sub PrintDataTable(ByVal tbl As DataTable)
Console.Writeline ("================================================ =========" & vbCrLf)
For Each col As DataColumn In tbl.Columns
Console.Writeline (col.ColumnName & vbTab)
Next col
Console.Writeline (vbCrLf)
For Each row As DataRow In tbl.Rows
For Each col As DataColumn In tbl.Columns
Console.Writeline (row(col).ToString() & vbTab)
Next col
Console.Writeline(vbCrLf)
Next row
End Sub

Public Sub Main()

Dim cn As New SqlConnection, _
strConn As String

' This does not help.
' AddHandler cn.InfoMessage, AddressOf SqlInfoMessage

' Connection string, change server and database!
strConn = "Application Name=systablesdemo;Integrated Security=SSPI;"
strConn &= "Data Source=(local);Initial Catalog=AdventureWorks"

Try
cn.ConnectionString = strConn
cn.Open()
Catch ex As Exception
Console.Writeline(ex.Message, "Connection failed!")
cn = Nothing
Exit Sub
End Try

Dim cmd As SqlCommand = cn.CreateCommand()

cmd.CommandText = "SELECT quotename(schema_name(schema_id)) + "
cmd.CommandText &= " '.' + quotename(name) FROM sys.tables "
cmd.CommandText &= "ORDER BY 1"
Dim dt As New DataTable, _
da As SqlDataAdapter = New SqlDataAdapter(cmd), _
no_of_rows As Integer
AddHandler da.FillError, AddressOf ErrorFill
Try
no_of_rows = da.Fill(dt)
Catch e As Exception
OutputException(e)
End Try
Console.Writeline("No of rows filled " & no_of_rows.ToString() & vbCrLf)
PrintDataTable(dt)

cn.Close()
cn.Dispose()

End Sub

End Module

--
Erland Sommarskog, SQL Server MVP, esq... (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books...
Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
Thank u for the your reply and the links.
What I really want is too easy and clear :
I create a program that open SQL 2005 Data Bases and show tables of
the data base.
What I have done is to get a table data but I have to know the table
name before opening any one.
What I want is to know the tables name of the current data base.
Any idea ?
Omar Abid



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.