Re: Add a Column to Database
What type of database? MS Access? You can't just add a column to a DataTable and expect it to be added to a database, it's not that simple. You will have to run an ALTER TABLE query against the table. See here.
Re: Add a Column to Database
Quote:
Originally Posted by
ForumAccount
What type of database? MS Access? You can't just add a column to a DataTable and expect it to be added to a database, it's not that simple. You will have to run an ALTER TABLE query against the table. See
here.
its a tab deliminated database
Re: Add a Column to Database
I tried the following but get a syntax error in the executenonquery or Operation not supported on a table that contains data.
vb Code:
ColCreate = New OleDbCommand("ALTER TABLE " & Me.OpenFileDialog1.SafeFileName & " ADD COLUMN Condition varchar(20)", con)
ColCreate.ExecuteNonQuery()
Re: Add a Column to Database
That specific error may well relate to the database type you are using, and there is an ugly way around it, but I think the more relevant question is: Why are you trying to add columns?
There are reasons that you might want to alter the design of an existing database at the running of a program, but they are generally things that happen just one time. If you know the column to add, and this will happen only one time, then go ahead and add it in the database, rather than via code. If you expect to be adding many columns over time, then you have the wrong design. So, rather than dealing with what is going on in this particular case, it seems best to understand what the real problem is, first.
On the other hand, you answered that this was "a tab delimitted" database. That doesn't actually sound like a database, at all, it sounds like a text file, in which case some of the alternatives to adding columns will not work.
Re: Add a Column to Database
Quote:
Originally Posted by
Shaggy Hiker
That specific error may well relate to the database type you are using, and there is an ugly way around it, but I think the more relevant question is: Why are you trying to add columns?
There are reasons that you might want to alter the design of an existing database at the running of a program, but they are generally things that happen just one time. If you know the column to add, and this will happen only one time, then go ahead and add it in the database, rather than via code. If you expect to be adding many columns over time, then you have the wrong design. So, rather than dealing with what is going on in this particular case, it seems best to understand what the real problem is, first.
On the other hand, you answered that this was "a tab delimitted" database. That doesn't actually sound like a database, at all, it sounds like a text file, in which case some of the alternatives to adding columns will not work.
I have created a program that reads a 3rd party database, actually you are correct its a tab delimated file or comma delimated .txt or .csv. These are files the user has and my program works with those files to perform calculations and such.
The reason for wanting to add a column is so the user can add a record of his own for future reference. I would like the record they add to have an additional column so it can be uniquely identified when the file is accessed in the future
Perhaps there is a better way to do this that i havent considered I am would appreciate your feedback and or suggestions . I am still very new to programming as a hobby so i am always open to learning
Thanks:)
Re: Add a Column to Database
If you have write permission to the file, then you should be able to add a column, but I would not be surprised to hear that an ALTER TABLE query would not work against a text file. You can use SQL to read those files, as you appear to know, but I'm not sure that ALTER TABLE is supported, or if it even makes sense for text files.
On the other hand, as long as you have write permission, you can write out the text file, replacing the one that is there. If you are working with lots of datatables or datasets, then it might make more sense to make the files XML files, as you would then be able to read or write the files into and out of datasets or datatables with considerable ease (it takes about one line for each operation). If you are stuck with the format, then go ahead and add the column to the datatable, as you are doing, but you will need to write out the file in a different fashion. I don't have any example of writing datatables to a delimitted file, that I can think of, but there are probably examples on this forum, so you could try searching for one. It would overwrite the file with the new information, though, so it would be a matter of reading in the entire file, making the changes, then writing the whole thing back out, including the changes.
If that won't work, somebody might have a suggestion for a workable ALTER TABLE query for text files. I don't KNOW that it is impossible, it would just surprise me a bit if it was possible.