How to insert data in a sql database from datagrid column
-
I have DataGridColumnStyle-s assigned to my datagrid. The columns are filled with data. How can I insert the data of a single column to a SQL Server database? I have inserted the data of a column into a SortedList, so I want to insert these values using the Insert statement to a CommandText property of the SqlCommand class??? :sigh:
-
I have DataGridColumnStyle-s assigned to my datagrid. The columns are filled with data. How can I insert the data of a single column to a SQL Server database? I have inserted the data of a column into a SortedList, so I want to insert these values using the Insert statement to a CommandText property of the SqlCommand class??? :sigh:
you could do it like this or by using a sql/odbc/oledb datadapter (e.g. system.data.oledb.oledbdataadapter) and set it up. this is a bit more complicated and i used it only once so you should go and google for it. the msdn has some nice explaination about how to use this thing. if you want to do it by yourself, you will have to have an array (or list or anything else) containg the values and the fieldname (a 2-dimensional array would do the job ;) if you got a table consisting of 3 columns (e.g. id, name, postdate) you would need an array like this:
data[0,0] = "id"; data[0,1] = 0; data[1,0] = "name"; data[1,1] = "myname"; data[2,0] = "postdate"; data[2,1] = "2006-05-02";
(alternatively you could use 2 single array, one holding the column names and the other one holding the column values) it would be easy to generate the statement then - it could look likestring sqlquery = "INSERT INTO tablename ("; string comma = ""; for (int i = 0; i < data.GetLength(0)-1; i++) { if (i > 0) comma = "," sqlquery = sqlquery + comma + data[i,0]; } sqlquery = sqlquery + ") VALUES ("; for (int i = 0; i < data.GetLength(0)-1; i++) { if (i > 0) comma = "," sqlquery = sqlquery + comma + "'" + data[i,1] + "'"; } sqlquery = sqlquery + ");";
this code would produce a query likeINSERT INTO tablename (id,name,postdate) VALUES ('0','myname','2006-05-02');
since this can be a lot of work you should make yourself familiar with the dataadapters :) -
you could do it like this or by using a sql/odbc/oledb datadapter (e.g. system.data.oledb.oledbdataadapter) and set it up. this is a bit more complicated and i used it only once so you should go and google for it. the msdn has some nice explaination about how to use this thing. if you want to do it by yourself, you will have to have an array (or list or anything else) containg the values and the fieldname (a 2-dimensional array would do the job ;) if you got a table consisting of 3 columns (e.g. id, name, postdate) you would need an array like this:
data[0,0] = "id"; data[0,1] = 0; data[1,0] = "name"; data[1,1] = "myname"; data[2,0] = "postdate"; data[2,1] = "2006-05-02";
(alternatively you could use 2 single array, one holding the column names and the other one holding the column values) it would be easy to generate the statement then - it could look likestring sqlquery = "INSERT INTO tablename ("; string comma = ""; for (int i = 0; i < data.GetLength(0)-1; i++) { if (i > 0) comma = "," sqlquery = sqlquery + comma + data[i,0]; } sqlquery = sqlquery + ") VALUES ("; for (int i = 0; i < data.GetLength(0)-1; i++) { if (i > 0) comma = "," sqlquery = sqlquery + comma + "'" + data[i,1] + "'"; } sqlquery = sqlquery + ");";
this code would produce a query likeINSERT INTO tablename (id,name,postdate) VALUES ('0','myname','2006-05-02');
since this can be a lot of work you should make yourself familiar with the dataadapters :) -
you could do it like this or by using a sql/odbc/oledb datadapter (e.g. system.data.oledb.oledbdataadapter) and set it up. this is a bit more complicated and i used it only once so you should go and google for it. the msdn has some nice explaination about how to use this thing. if you want to do it by yourself, you will have to have an array (or list or anything else) containg the values and the fieldname (a 2-dimensional array would do the job ;) if you got a table consisting of 3 columns (e.g. id, name, postdate) you would need an array like this:
data[0,0] = "id"; data[0,1] = 0; data[1,0] = "name"; data[1,1] = "myname"; data[2,0] = "postdate"; data[2,1] = "2006-05-02";
(alternatively you could use 2 single array, one holding the column names and the other one holding the column values) it would be easy to generate the statement then - it could look likestring sqlquery = "INSERT INTO tablename ("; string comma = ""; for (int i = 0; i < data.GetLength(0)-1; i++) { if (i > 0) comma = "," sqlquery = sqlquery + comma + data[i,0]; } sqlquery = sqlquery + ") VALUES ("; for (int i = 0; i < data.GetLength(0)-1; i++) { if (i > 0) comma = "," sqlquery = sqlquery + comma + "'" + data[i,1] + "'"; } sqlquery = sqlquery + ");";
this code would produce a query likeINSERT INTO tablename (id,name,postdate) VALUES ('0','myname','2006-05-02');
since this can be a lot of work you should make yourself familiar with the dataadapters :)yeah and maybe take a look at concurrencymanager.
Tom Wright tawright915@gmail.com