SQL Server 2005 with VB.net 2005
SQL Server 2005 with VB.net 2005
am 09.06.2007 14:05:44 von Omar Abid
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@hotmail.com
omar.abid2006@gmail.com
Omar abid
Re: SQL Server 2005 with VB.net 2005
am 09.06.2007 18:51:13 von Ed Murphy
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.
Re: SQL Server 2005 with VB.net 2005
am 10.06.2007 08:39:22 von Karl
There is a view that has a list of all the columns in the whole
database. I believe its information_sys.columns
Re: SQL Server 2005 with VB.net 2005
am 10.06.2007 09:38:09 von Omar Abid
On 9 juin, 18:51, Ed Murphy 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
Re: SQL Server 2005 with VB.net 2005
am 10.06.2007 09:39:06 von Omar Abid
On 10 juin, 08:39, Karl wrote:
> 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
Re: SQL Server 2005 with VB.net 2005
am 10.06.2007 09:57:42 von Ed Murphy
Omar Abid wrote:
> On 9 juin, 18:51, Ed Murphy 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.
Re: SQL Server 2005 with VB.net 2005
am 10.06.2007 10:57:55 von Omar Abid
On 10 juin, 09:57, Ed Murphy wrote:
> Omar Abid wrote:
> > On 9 juin, 18:51, Ed Murphy 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
Re: SQL Server 2005 with VB.net 2005
am 10.06.2007 11:06:20 von Erland Sommarskog
Omar Abid (omar.abid2006@gmail.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, esquel@sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downlo ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books .mspx
Re: SQL Server 2005 with VB.net 2005
am 10.06.2007 22:36:48 von Ed Murphy
Omar Abid wrote:
> On 10 juin, 09:57, Ed Murphy wrote:
>> Omar Abid wrote:
>>> On 9 juin, 18:51, Ed Murphy 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?
Re: SQL Server 2005 with VB.net 2005
am 11.06.2007 01:02:50 von Omar Abid
On 10 juin, 11:06, Erland Sommarskog wrote:
> Omar Abid (omar.abid2...@gmail.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...@sommarskog.se
>
> Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/down loads/books...
> Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo/previousversions/boo ks.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
Re: SQL Server 2005 with VB.net 2005
am 11.06.2007 10:04:20 von Erland Sommarskog
Omar Abid (omar.abid2006@gmail.com) writes:
> 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 ?
I just posted a sample on how to do it. You even reply to that post. Could
you care to explain why it does not address your problem?
--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downlo ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books .mspx
Re: SQL Server 2005 with VB.net 2005
am 11.06.2007 10:26:16 von Omar Abid
On 11 juin, 10:04, Erland Sommarskog wrote:
> Omar Abid (omar.abid2...@gmail.com) writes:
> > 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 ?
>
> I just posted a sample on how to do it. You even reply to that post. Could
> you care to explain why it does not address your problem?
>
> --
> Erland Sommarskog, SQL Server MVP, esq...@sommarskog.se
>
> Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/down loads/books...
> Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo/previousversions/boo ks.mspx
I tried the code but I want any SQL Data Base and not Adventure Works
Re: SQL Server 2005 with VB.net 2005
am 11.06.2007 23:30:20 von Erland Sommarskog
Omar Abid (omar.abid2006@gmail.com) writes:
> I tried the code but I want any SQL Data Base and not Adventure Works
Just replace AdventureWorks in the connection string with the database
of your choice. I used AdventureWorks to give you a working sample.
--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downlo ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books .mspx
Re: SQL Server 2005 with VB.net 2005
am 12.06.2007 01:33:33 von giorgi.piero
On 11 Giu, 14:30, Erland Sommarskog wrote:
> > I tried the code but I want any SQL Data Base and not Adventure Works
>
> Just replace AdventureWorks in the connection string with the database
> of your choice. I used AdventureWorks to give you a working sample.
And just to be more specific...
' Connection string, change server and database!
strConn = "Application Name=systablesdemo;Integrated
Security=SSPI;"
strConn &= "Data Source=(local);Initial Catalog= ***** YOUR
DATABASE HERE **** "
P
Re: SQL Server 2005 with VB.net 2005
am 12.06.2007 08:54:02 von Omar Abid
On 11 juin, 23:30, Erland Sommarskog wrote:
> Omar Abid (omar.abid2...@gmail.com) writes:
> > I tried the code but I want any SQL Data Base and not Adventure Works
>
> Just replace AdventureWorks in the connection string with the database
> of your choice. I used AdventureWorks to give you a working sample.
>
> --
> Erland Sommarskog, SQL Server MVP, esq...@sommarskog.se
>
> Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/down loads/books...
> Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo/previousversions/boo ks.mspx
Thank you the problem was solved, and I used mid to get the table
name, because the output was "[DBO].[table]"
Thanks again for your help
Omar Abid
Re: SQL Server 2005 with VB.net 2005
am 12.06.2007 23:10:18 von Erland Sommarskog
Omar Abid (omar.abid2006@gmail.com) writes:
> Thank you the problem was solved, and I used mid to get the table
> name, because the output was "[DBO].[table]"
You could modify the query if you don't want the schema.
Then again, in SQL 2005, it may be dangerous to ignore the schema, since
SQL 2005 makes schemas useful, and it can be deceivable to work with.
The AdventureWorks database is a good example of this.
--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downlo ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books .mspx
Re: SQL Server 2005 with VB.net 2005
am 12.06.2007 23:46:24 von Omar Abid
On 12 juin, 23:10, Erland Sommarskog wrote:
> Omar Abid (omar.abid2...@gmail.com) writes:
> > Thank you the problem was solved, and I used mid to get the table
> > name, because the output was "[DBO].[table]"
>
> You could modify the query if you don't want the schema.
>
> Then again, in SQL 2005, it may be dangerous to ignore the schema, since
> SQL 2005 makes schemas useful, and it can be deceivable to work with.
> The AdventureWorks database is a good example of this.
>
> --
> Erland Sommarskog, SQL Server MVP, esq...@sommarskog.se
>
> Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/down loads/books...
> Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo/previousversions/boo ks.mspx
Thank you for your advice, it was really helpful to me
Omar Abid