Microsoft SQL Server 2005 Express

  • Thread starter Thread starter Omar Abid
  • Start date Start date
O

Omar Abid

Hi,
I'm Trying to make a program with vb2005express that connect to any
Microsft SQL data base and show it's table. I'm having some problems
can anyone help me?
Omar Abid
 
Hi,
I'm Trying to make a program with vb2005express that connect to any
Microsft SQL data base and show it's table. I'm having some problems
can anyone help me?
Omar Abid

You don't actually say what problems you're having, so I'm going to
assume you can't get anything to work. So as a demo just start a new
Windows Application and add something like this to a new form's code:

' Typed in the message so watch out!

' Add this to the top of the class
Imports System.Data.SqlClient

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
' This is your connection string from www.connectionstrings.com
' Be sure to change the necessary parts before running
Dim conn As New SqlConnection("Data
Source=myServerAddress;Initial Catalog=myDataBase;User
Id=myUsername;Password=myPassword;")
Using (conn)
conn.Open()
Dim com As SqlCommand = conn.CreateCommand()
Using (com)
com.CommandType = CommandType.Text
' Again change "MyTable" to whatever the table name
should be
com.CommandText = "Select * From MyTable"
Dim da As New SqlDataAdapter(com)
Using (da)
Dim dt As New DataTable("MyTable")
Using (dt)
da.Fill(dt)
Dim dgv As New DataGridView()
dgv.Dock = DockStyle.Fill
dgv.DataSource = dt
Me.Controls.Add(dgv)
End Using
End Using
End Using
End Using
End Sub

Hope that helps.

Thanks,

Seth Rowe
 
Hi,
I'm Trying to make a program with vb2005express that connect to any
Microsft SQL data base and show it's table. I'm having some problems
can anyone help me?
Omar Abid

Remember that MS SQL 2005 Express has a default state where it does
not allow connections. You have to enable the transport protocols
first before you can connect to it.

-Joe
 
You don't actually say what problems you're having, so I'm going to
assume you can't get anything to work. So as a demo just start a new
Windows Application and add something like this to a new form's code:

' Typed in the message so watch out!

' Add this to the top of the class
Imports System.Data.SqlClient

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
' This is your connection string fromwww.connectionstrings.com
' Be sure to change the necessary parts before running
Dim conn As New SqlConnection("Data
Source=myServerAddress;Initial Catalog=myDataBase;User
Id=myUsername;Password=myPassword;")
Using (conn)
conn.Open()
Dim com As SqlCommand = conn.CreateCommand()
Using (com)
com.CommandType = CommandType.Text
' Again change "MyTable" to whatever the table name
should be
com.CommandText = "Select * From MyTable"
Dim da As New SqlDataAdapter(com)
Using (da)
Dim dt As New DataTable("MyTable")
Using (dt)
da.Fill(dt)
Dim dgv As New DataGridView()
dgv.Dock = DockStyle.Fill
dgv.DataSource = dt
Me.Controls.Add(dgv)
End Using
End Using
End Using
End Using
End Sub

Hope that helps.

Thanks,

Seth Rowe

Ok, this help me a lot but i want to open a data base that i don't
know the name
i want that the program browse the tables of the base
Thanks
 
Ok, this help me a lot but i want to open a data base that i don't
know the name
i want that the program browse the tables of the base

Do you know the information for the connection string? If you can
connect to the database you can just query the schema tables and get a
list of names.

Thanks,

Seth Rowe
 
Do you know the information for the connection string? If you can
connect to the database you can just query the schema tables and get a
list of names.

Thanks,

Seth Rowe




- Afficher le texte des messages précédents -

Hi,
Now i can access to any table in my data base, but i want that the
user choose a table.
So i need to make a code that allow me to browser all the database
tables.
Omar Abid
 
Hi,
Now i can access to any table in my data base, but i want that the
user choose a table.
So i need to make a code that allow me to browser all the database
tables.
Omar Abid

Like I mentioned in my last post, what you need to do to get the table
names is query the schema tables. Use something like this as:

Select Distinct TABLE_NAME
From INFORMATION_SCHEMA.COLUMNS

You'll need to mess around with the Where statement to get rid of
system tables, etc, but I don't have Sql Server available right know
to help.

Thanks,

Seth Rowe
 
Hi,
Now i can access to any table in my data base, but i want that the
user choose a table.
So i need to make a code that allow me to browser all the database
tables.
Omar Abid

Like I mentioned in my last post, what you need to do to get the table
names is query the schema tables. Use something like this as:

Select Distinct TABLE_NAME
From INFORMATION_SCHEMA.COLUMNS

You'll need to mess around with the Where statement to get rid of
system tables, etc, but I don't have Sql Server available right know
to help.

Thanks,

Seth Rowe
---------------------------------------------------

Here ya go.


Dim cn As New SqlConnection(connectionString)
Try
cn.Open()
'The Restrictions are: table_catalog, table_schema,
' table_name, table_type.
'For my case, the table_catalog is the database name.
'table_Schema is the owner.
'Table_name is nothing because I want all tables.
'table_type is "BASE TABLE".
Dim restrictions() As String = New String() _
{My.Settings.DatabaseName, "dbo", Nothing, "BASE TABLE"}
Dim dt As DataTable = cn.GetSchema("Tables", restrictions)

'Uncomment this if you want to see the columns you can
' access from the GetSchema command.
'I'm leaving it in here for future reference.
'For Each col As DataColumn In dt.Columns
' Debug.Print(col.ColumnName.ToString)
'Next

For Each rw As DataRow In dt.Rows

'Uncomment this if you want to see the values
' for each of these columns.
'I'm leaving it in here for future reference.
'Debug.Print("Table_Catalog = {0}, Table_Schema = {1},
Table_Name = {2}, Table_Type = {3}", _
' rw.Item("TABLE_CATALOG"), rw.Item("TABLE_SCHEMA"), _
' rw.Item("TABLE_NAME"), rw.Item("TABLE_TYPE"))

'sysdiagrams shows up if you have a database diagram;
exclude it here
If rw.Item("TABLE_NAME").ToString.ToUpper <> "SYSDIAGRAMS"
Then
Me.Add(rw.Item("TABLE_NAME").ToString)
End If
Next

'sort the list of tables
Me.Sort()

Catch
MessageBox.Show("Error opening connection to database.")
Finally
cn.Close()
End Try
End Sub


Robin S.
 
Like I mentioned in my last post, what you need to do to get the table
names is query the schema tables. Use something like this as:

Select Distinct TABLE_NAME
From INFORMATION_SCHEMA.COLUMNS

You'll need to mess around with the Where statement to get rid of
system tables, etc, but I don't have Sql Server available right know
to help.

Thanks,

Seth Rowe
---------------------------------------------------

Here ya go.

Dim cn As New SqlConnection(connectionString)
Try
cn.Open()
'The Restrictions are: table_catalog, table_schema,
' table_name, table_type.
'For my case, the table_catalog is the database name.
'table_Schema is the owner.
'Table_name is nothing because I want all tables.
'table_type is "BASE TABLE".
Dim restrictions() As String = New String() _
{My.Settings.DatabaseName, "dbo", Nothing, "BASE TABLE"}
Dim dt As DataTable = cn.GetSchema("Tables", restrictions)

'Uncomment this if you want to see the columns you can
' access from the GetSchema command.
'I'm leaving it in here for future reference.
'For Each col As DataColumn In dt.Columns
' Debug.Print(col.ColumnName.ToString)
'Next

For Each rw As DataRow In dt.Rows

'Uncomment this if you want to see the values
' for each of these columns.
'I'm leaving it in here for future reference.
'Debug.Print("Table_Catalog = {0}, Table_Schema = {1},
Table_Name = {2}, Table_Type = {3}", _
' rw.Item("TABLE_CATALOG"), rw.Item("TABLE_SCHEMA"), _
' rw.Item("TABLE_NAME"), rw.Item("TABLE_TYPE"))

'sysdiagrams shows up if you have a database diagram;
exclude it here
If rw.Item("TABLE_NAME").ToString.ToUpper <> "SYSDIAGRAMS"
Then
Me.Add(rw.Item("TABLE_NAME").ToString)
End If
Next

'sort the list of tables
Me.Sort()

Catch
MessageBox.Show("Error opening connection to database.")
Finally
cn.Close()
End Try
End Sub

Robin S.- Masquer le texte des messages précédents -

- Afficher le texte des messages précédents -

Hi,
I tried tried and then your code after so changement works
Thanks a lot for resolving my probelm
If you need me : (e-mail address removed)
Thank you friend
OMAR ABID
 
Back
Top