473,769 Members | 1,723 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Restructuring Database table in VB.NET 2003 help please

Hi,

Could someone please xplain how to add a field to an existing SQL table in
VB.Net

I have added the field in the Server Explorer and it shows up when I reload
the program
but I cannot access the field from within my program.

Is there something I need to refresh or do I need to recreate he Dataapdtors
or Datasets ?

hanks in advance
Nov 21 '05 #1
8 2041
Hi,

I am not sure I understand the question. You can change a
database table using the alter table sql command. Here is some sample
code on how to create a database, table, alter table and stored procedure.

Dim conn As SqlConnection

Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load

Dim strConn As String

strConn = "Server = " & Environment.Mac hineName

strConn += "\VSdotNET; Database = ; Integrated Security = SSPI;"

conn = New SqlConnection(s trConn)

conn.Open()

CreateDataBase( )

CreateClientsTa ble()

End Sub

Private Sub CreateDataBase( )

Dim strSQL As String

strSQL = "if Exists (Select * From master..sysdata bases Where Name = 'VET')"

strSQL += "DROP DATABASE VET" & vbCrLf & " CREATE DATABASE VET"

Dim cmd As New SqlCommand(strS QL, conn)

cmd.CommandType = CommandType.Tex t

Try

cmd.ExecuteNonQ uery()

Catch

MessageBox.Show ("Error Creating DB")

Finally

cmd.Dispose()

End Try

End Sub

Private Sub CreateClientsTa ble()

Me.Text = "Creating Clients Table..."

Dim strSQL As String = _

"USE VET" & vbCrLf & _

"IF EXISTS (" & _

"SELECT * " & _

"FROM VET.dbo.sysobje cts " & _

"WHERE Name = 'Clients' " & _

"AND TYPE = 'u')" & vbCrLf & _

"BEGIN" & vbCrLf & _

"DROP TABLE VET.dbo.Clients " & vbCrLf & _

"END" & vbCrLf & _

"CREATE TABLE Clients (" & _

"ID Int NOT NULL," & _

"LastName NVarChar(20) NOT NULL," & _

"FirstName NVarChar(20) NOT NULL," & _

"Address NVarChar(150) NOT NULL," & _

"City NVarChar(20) NOT NULL," & _

"ZipCode NVarChar(5) NOT NULL," & _

"PhoneNumbe r NVarChar(20) NOT NULL," & _

"WorkNumber NVarChar(20)," & _

"CellNumber NVarChar(20)," & _

"Email NVarChar(50) NOT NULL," & _

"Balance Money NOT NULL," & _

"BalanceDat e DateTime NOT NULL," & _

"CONSTRAINT [ID] PRIMARY KEY CLUSTERED" & _

"(ID))"

Dim cmd As New SqlCommand(strS QL, conn)

cmd.CommandType = CommandType.Tex t

Try

cmd.ExecuteNonQ uery()

Catch ex As SqlException

MessageBox.Show (ex.ToString, "Clients")

Finally

cmd.Dispose()

End Try

End Sub

Private Sub MakeClientStore dProcedure()

Dim strSQL As String = _

"USE VET" & vbCrLf & _

"IF EXISTS (" & _

"SELECT * " & _

"FROM VET.dbo.sysobje cts " & _

"WHERE Name = 'ClientInfo' " & _

"AND TYPE = 'p')" & vbCrLf & _

"BEGIN" & vbCrLf & _

"DROP PROCEDURE ClientInfo" & vbCrLf & _

"END"

Dim cmd As New SqlCommand(strS QL, conn)

cmd.CommandType = CommandType.Tex t

Try

cmd.ExecuteNonQ uery()

cmd.CommandText = "Create Procedure ClientInfo" & vbCrLf & _

"@ClientID int " & vbCrLf & _

"AS Select * " & vbCrLf & _

"FROM VET.dbo.Clients Where ID = @ClientID"

cmd.ExecuteNonQ uery()

Catch ex As SqlException

MessageBox.Show (ex.ToString, "Error Creating Stored Procedure")

Finally

cmd.Dispose()

End Try

End Sub

Alter table example
strSql = "ALTER TABLE PetInfo ADD Vet int NULL"

cmdUpdate = New SqlCommand(strS ql, connVet)

connVet.Open()

cmdUpdate.Execu teNonQuery()

connVet.Close()

http://msdn.microsoft.com/library/de...aa-az_3ied.asp
Ken

-----------------------------
"David" <da***@orbitcom s.com> wrote in message
news:ux******** ******@TK2MSFTN GP12.phx.gbl...
Hi,

Could someone please xplain how to add a field to an existing SQL table in
VB.Net

I have added the field in the Server Explorer and it shows up when I reload
the program
but I cannot access the field from within my program.

Is there something I need to refresh or do I need to recreate he Dataapdtors
or Datasets ?

hanks in advance

Nov 21 '05 #2
Ken,

Thanks for the info.

The situation is that I have created and application that uses a SQL
database.
The database initially was created using Microsoft Access.

The SQL database is running on a local instance of MSDE server.

In VB.NET I created the connectivity using the components from the tool box
for a connection
then dataadaptors and datasets.
I load and refresh the datsets and update the dataadapters etc in code.

Now I want to add a new field to one of the tables in the database. I tried
using the Server Explorer and found I could right click the table and go to
design view and add a field.

Though the field shows up in the server explorer, I cannot access it withing
my program.
I am not sure what else must be done once you create the new field to get
your code to "see" it.

As I am new to ADO programming, I would like to be able to alter the
database structure as mentioned above, without needing to write all the
connectivity from scratch.

Thanks for any more comments you may have to assist.
"Ken Tucker [MVP]" <vb***@bellsout h.net> wrote in message
news:%2******** ********@TK2MSF TNGP14.phx.gbl. ..
Hi,

I am not sure I understand the question. You can change a
database table using the alter table sql command. Here is some sample
code on how to create a database, table, alter table and stored procedure.

Dim conn As SqlConnection

Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load

Dim strConn As String

strConn = "Server = " & Environment.Mac hineName

strConn += "\VSdotNET; Database = ; Integrated Security = SSPI;"

conn = New SqlConnection(s trConn)

conn.Open()

CreateDataBase( )

CreateClientsTa ble()

End Sub

Private Sub CreateDataBase( )

Dim strSQL As String

strSQL = "if Exists (Select * From master..sysdata bases Where Name =
'VET')"

strSQL += "DROP DATABASE VET" & vbCrLf & " CREATE DATABASE VET"

Dim cmd As New SqlCommand(strS QL, conn)

cmd.CommandType = CommandType.Tex t

Try

cmd.ExecuteNonQ uery()

Catch

MessageBox.Show ("Error Creating DB")

Finally

cmd.Dispose()

End Try

End Sub

Private Sub CreateClientsTa ble()

Me.Text = "Creating Clients Table..."

Dim strSQL As String = _

"USE VET" & vbCrLf & _

"IF EXISTS (" & _

"SELECT * " & _

"FROM VET.dbo.sysobje cts " & _

"WHERE Name = 'Clients' " & _

"AND TYPE = 'u')" & vbCrLf & _

"BEGIN" & vbCrLf & _

"DROP TABLE VET.dbo.Clients " & vbCrLf & _

"END" & vbCrLf & _

"CREATE TABLE Clients (" & _

"ID Int NOT NULL," & _

"LastName NVarChar(20) NOT NULL," & _

"FirstName NVarChar(20) NOT NULL," & _

"Address NVarChar(150) NOT NULL," & _

"City NVarChar(20) NOT NULL," & _

"ZipCode NVarChar(5) NOT NULL," & _

"PhoneNumbe r NVarChar(20) NOT NULL," & _

"WorkNumber NVarChar(20)," & _

"CellNumber NVarChar(20)," & _

"Email NVarChar(50) NOT NULL," & _

"Balance Money NOT NULL," & _

"BalanceDat e DateTime NOT NULL," & _

"CONSTRAINT [ID] PRIMARY KEY CLUSTERED" & _

"(ID))"

Dim cmd As New SqlCommand(strS QL, conn)

cmd.CommandType = CommandType.Tex t

Try

cmd.ExecuteNonQ uery()

Catch ex As SqlException

MessageBox.Show (ex.ToString, "Clients")

Finally

cmd.Dispose()

End Try

End Sub

Private Sub MakeClientStore dProcedure()

Dim strSQL As String = _

"USE VET" & vbCrLf & _

"IF EXISTS (" & _

"SELECT * " & _

"FROM VET.dbo.sysobje cts " & _

"WHERE Name = 'ClientInfo' " & _

"AND TYPE = 'p')" & vbCrLf & _

"BEGIN" & vbCrLf & _

"DROP PROCEDURE ClientInfo" & vbCrLf & _

"END"

Dim cmd As New SqlCommand(strS QL, conn)

cmd.CommandType = CommandType.Tex t

Try

cmd.ExecuteNonQ uery()

cmd.CommandText = "Create Procedure ClientInfo" & vbCrLf & _

"@ClientID int " & vbCrLf & _

"AS Select * " & vbCrLf & _

"FROM VET.dbo.Clients Where ID = @ClientID"

cmd.ExecuteNonQ uery()

Catch ex As SqlException

MessageBox.Show (ex.ToString, "Error Creating Stored Procedure")

Finally

cmd.Dispose()

End Try

End Sub

Alter table example
strSql = "ALTER TABLE PetInfo ADD Vet int NULL"

cmdUpdate = New SqlCommand(strS ql, connVet)

connVet.Open()

cmdUpdate.Execu teNonQuery()

connVet.Close()

http://msdn.microsoft.com/library/de...aa-az_3ied.asp
Ken

-----------------------------
"David" <da***@orbitcom s.com> wrote in message
news:ux******** ******@TK2MSFTN GP12.phx.gbl...
Hi,

Could someone please xplain how to add a field to an existing SQL table in
VB.Net

I have added the field in the Server Explorer and it shows up when I
reload
the program
but I cannot access the field from within my program.

Is there something I need to refresh or do I need to recreate he
Dataapdtors
or Datasets ?

hanks in advance

Nov 21 '05 #3
David,

Ken gave you the answer "Alter", with a lot of code in a sample, how to
handle these things.

He also gave you this string for the syntax.
http://msdn.microsoft.com/library/de...aa-az_3ied.asp

These commands you handle with
Execute.NonQuer ry, see for that the sample how to use it that Ken gave.

Ken's sample create and delete and checking of existings of tables using
that.

When you know it, it is very easy.

By the way, there is a big difference between ADO and ADONET

I hope this helps,

Cor
Nov 21 '05 #4
Cor,

Are you saying I can just leave all the connectivity as is and then write
the code to add a column to the table and run Execute.Nonquer y to have it
applied to the database. Where do I use Stored procs ?

Then I can remove it from code when the clumn is created ?

After I have created the new cloumn, I will detach the database from the
server so I can copy it to the CD image that is distributed ?

I distribute a copy of the mdf file on CD rom that the user can "Attach" to
their local MSDE
server when they install the program.

What is the Server Explorer doing when it lets you add columns in Design
view ? I imaginged this was doing the same as if I edited the SQL database
in Access.

It seems strange to me to write code to change the structure of the
database. I could understand this if I was writing a Database management
application that I could select new columns or tables at runtime but I do
not undertstand why I would need to program the alterations in code when it
is changing the underlying database structure that will be used in other
programs.

The listviews all currently have databindings via the properties for each
component (no code written). The application does not need to create or
modify the database structure during normal operation. The displays and file
saving are static with regard to waht columns are used.

Hope this makes some kind of sense. As mentioned, I am a database novice and
have used most of the built-in facilities for achieving connectivity instead
of doing it in code. I would like to avoid rewriting heaps of code and
implementing stored procedures that I do not yet understand.

I do understand that if I recreated the database in Access and then removed
and droped down the connection and adaptors etc that I would have access to
the additional column.
"Cor Ligthert" <no************ @planet.nl> wrote in message
news:Ov******** ******@tk2msftn gp13.phx.gbl...
David,

Ken gave you the answer "Alter", with a lot of code in a sample, how to
handle these things.

He also gave you this string for the syntax.
http://msdn.microsoft.com/library/de...aa-az_3ied.asp

These commands you handle with
Execute.NonQuer ry, see for that the sample how to use it that Ken gave.

Ken's sample create and delete and checking of existings of tables using
that.

When you know it, it is very easy.

By the way, there is a big difference between ADO and ADONET

I hope this helps,

Cor

Nov 21 '05 #5
David,

Adding a column to a database should be a one time operation.

By instance by an update (new release) procedure of your program. It should
be a seperated program or very seperated class in your program, that you
than can change by every release.

This means that you have to add those changes as well if needed to your
Stored procedures and whatever other place.

Adding a column to a database should certainly not be a standard operation.

I hope this helps,

Cor
Nov 21 '05 #6
Hi,

If you added the field in the SQL Table, you only have to Refresh your
DataAdapters using that table. If you look at the SelectCommandTe xt of your
DataAdapters, you can see that it doesn't use the "SELECT *" but "SELECT
fiel1, field2, ...".
So for every change to your table tou have to refresh your dataadapter
(evenso if you change the lenght of a field os stuff like that.
You can do that by right-clickin on your datadapter in Design-mode, and than
chose "Configure Data Adapter..."

I hope this is an answer to your question?

Pieter
"David" <da***@orbitcom s.com> wrote in message
news:ux******** ******@TK2MSFTN GP12.phx.gbl...
Hi,

Could someone please xplain how to add a field to an existing SQL table in
VB.Net

I have added the field in the Server Explorer and it shows up when I reload the program
but I cannot access the field from within my program.

Is there something I need to refresh or do I need to recreate he Dataapdtors or Datasets ?

hanks in advance

Nov 21 '05 #7
Pieter,

That's exactly what I was hoping for. Thanks.
"DraguVaso" <pi**********@h otmail.com> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
Hi,

If you added the field in the SQL Table, you only have to Refresh your
DataAdapters using that table. If you look at the SelectCommandTe xt of
your
DataAdapters, you can see that it doesn't use the "SELECT *" but "SELECT
fiel1, field2, ...".
So for every change to your table tou have to refresh your dataadapter
(evenso if you change the lenght of a field os stuff like that.
You can do that by right-clickin on your datadapter in Design-mode, and
than
chose "Configure Data Adapter..."

I hope this is an answer to your question?

Pieter
"David" <da***@orbitcom s.com> wrote in message
news:ux******** ******@TK2MSFTN GP12.phx.gbl...
Hi,

Could someone please xplain how to add a field to an existing SQL table
in
VB.Net

I have added the field in the Server Explorer and it shows up when I

reload
the program
but I cannot access the field from within my program.

Is there something I need to refresh or do I need to recreate he

Dataapdtors
or Datasets ?

hanks in advance


Nov 21 '05 #8
Can someone provide a SQL string that adds the column "myNewCol" of data type
integer to my datatable named "myTable" that I can use in VB.Net!

--
Dennis in Houston
"David" wrote:
Pieter,

That's exactly what I was hoping for. Thanks.
"DraguVaso" <pi**********@h otmail.com> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
Hi,

If you added the field in the SQL Table, you only have to Refresh your
DataAdapters using that table. If you look at the SelectCommandTe xt of
your
DataAdapters, you can see that it doesn't use the "SELECT *" but "SELECT
fiel1, field2, ...".
So for every change to your table tou have to refresh your dataadapter
(evenso if you change the lenght of a field os stuff like that.
You can do that by right-clickin on your datadapter in Design-mode, and
than
chose "Configure Data Adapter..."

I hope this is an answer to your question?

Pieter
"David" <da***@orbitcom s.com> wrote in message
news:ux******** ******@TK2MSFTN GP12.phx.gbl...
Hi,

Could someone please xplain how to add a field to an existing SQL table
in
VB.Net

I have added the field in the Server Explorer and it shows up when I

reload
the program
but I cannot access the field from within my program.

Is there something I need to refresh or do I need to recreate he

Dataapdtors
or Datasets ?

hanks in advance



Nov 21 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3353
by: cooldv | last post by:
i am running a website on Windows 2000 server with ASP 3 webpages and Access 2000 database. (with a hosting company) traffic is slow at this time but expect to grow. lately i have been reading about sql database and sql server, specially this article: http://www.aspfaq.com/show.asp?id=2195 will someone help me understand: 1. with *SQL Server*, do i keep my current Access 2000 database and ASP pages?
24
4208
by: Michael Malinsky | last post by:
I'm attempting to create a database which will take information from one (perhaps two) tables and utilize that information to return queries to a report designed in Excel. The general idea I have is this: The "primary" or "source" table is easy. This is a trial balance (account number as a primary key, description, amount). I'll need table(s) to denote which account numbers belong to which
4
1660
by: Octavio Alvarez | last post by:
Hi! I would like to implement a database which allows me to keep track of changes from users, but I don't know if there is any model already used for this. Let me show you what I mean. Say I have a table t_table1 with 2 columns plus a PK. Normally my table with some data would look like: t_table1 ------------------ PK | col1 | col2
7
1974
by: Andante.in.Blue | last post by:
Hello everyone! I've been working with a problematic legacy database for a while. While I am still fairly new to Access, the more I work with the database, the more problems I've uncovered. Unfortunately, most of these problems lie in the ways of architecture. A lot of the tables are designed with no primary keys, and a number of them using the text names in place of the PK-FK relationships. For instances, I have one table called ...
12
4867
by: Ray | last post by:
I just completed a database and would like to prepare the documentation for it. As I have no experience to do so, can someone advise me the essential elements for the documentation and any example available for reference. Thanks, Ray
0
7717
by: gm | last post by:
Immediately after generating the Access application from the Source Safe project I get: "-2147467259 Could not use ''; file already in use." If Access database closed and then reopened I get: "-2147467259 The database has been place in a state by user 'Admin' on machine ..... that prevents it from being opened or locked."
8
2703
by: rdemyan via AccessMonster.com | last post by:
I've converted my application from A2K format to A2003 format. I tried to follow Allen Browne's protocol in getting my app into A2003 (although I was unable to find informtion on the conversion process). Lots of decompiling and lots of compacting of the original application in A2000. Then the app was opened in A2003 and compacted, decompiled and compacted. Next I imported everything into a blank A2003 database. Then this db was...
7
2388
convexcube
by: convexcube | last post by:
To keep a record of training levels for different tasks, I have 18 option groups with 4 options values each: 0 labelled as "None", 1 labelled as "Trainee", 2 labelled as "Competent" and 3 labelled as "Expert". I would like to develop a detailed report that will show one employees training levels based on the values of training for each of the 18 categories. For example, an employee with these values in a table: Service = 3 Cleaning = 2...
10
4110
by: hedges98 | last post by:
I'm not sure if the title of the thread is relevant but I think it explains my problem... This is going to be loooooong... Basically, I am working on altering/improving an existing database but have been asked to change something which I think requires a whole restructuring of the database (to be honest, the relationships and tables seem a bit weird/needless to me). I've never tried altering the structure of a database before so have a whole...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10211
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9994
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8870
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7408
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3958
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.