Editing database table fields
I have SQLite database with few tables and C# windows form application. An
application has datagridview control that represents data from database. I
want a user to be able to change database table fields, which is easy by
using sql ALTER TABLE query. The problem comes when the user wants to
insert some data to the database table because I need to know the table's
field name in order to construct a valid INSERT query. I usually just
hard-coded field names, because I never had an option that allows user to
change the table's field name in a database.
My idea is that my INSERT query uses datagridview's column names. So, when
a user changes the table's field name, it also changes the grid's field
names, and every time an application is run, grid's columns are named to
the names same as database table has. That way I always have an access to
the right field name.
Or can I just use an index instead of string to reference a field?
Usually I use this code to insert data.
db = new SQLiteDatabase();
Dictionary<String, String> myData = new Dictionary<String, String>();
myData.Add("NAME", "something");
myData.Add("LAST_NAME", "something");
try
{
db.Insert("TABLE_NAME", myData);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
Thank you
No comments:
Post a Comment