furjaw

I have a user running my application that uses an SQL DataBase Table.
He has requested that I add a column called "CaseNbr" to the table.

"CaseNbr" is not currently in the DataBase. I have to add the column to the SQL table programmatically because he has his own database with a lot of data in it and he is in another location.
There is only one program that accesses the table and it is very simple.
Using a DataGridView the user selects (or adds) a row (Patient record) then the program simply passes that Patient's info to the main form of the application.

To add the coumn, I wrote the program below as a new project:

Imports System.Data.SqlClient
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim sqlConnection As New System.Data.SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\patient.mdf;Integrated Security=True;User Instance=True")
Dim command As New System.Data.SqlClient.SqlCommand
command.CommandText = "ALTER TABLE Patient ADD CaseNbr Varchar(50)')"
sqlConnection.Open()
command.ExecuteNonQuery()
sqlConnection.Close()
End Sub
End Class

I got the following error when I executed:

An attempt to attach an auto-named database for file C:\Documents and Settings\Owner\My Documents\Visual Studio 2005\Projects\Add Column\Add Column\bin\Debug\patient.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.



Re: Visual Basic Express Edition How to add a column to an existing database table

Feng Chen - MSFT

Hi,

It seems that you are using SQL server express, please make sure the file patient.mdf is already exists under the App_Data folder and it is accessible. Please check your connection string as well, e.g.

Thanks!