sql server - VB.NET Insert DataGridView contents into Database -
problem:
i need dump contents of datagridview sql server database table. i've got datagridview loading fine, no problems there. i'm not familiar enough vb.net understand how data db table.
code: (so far)
dim connection new data.sqlclient.sqlconnection dim dataadapter new data.sqlclient.sqldataadapter dim command new data.sqlclient.sqlcommand dim dataset new data.dataset connection.connectionstring = "server= server; database= db; integrated security=true" command.commandtext = "insert <table> (col1, col2, col3, col4) values (@name, @property, @value, @date)" dataadapter.insertcommand.parameters.add("@servername", sqldbtype.varchar) dataadapter.insertcommand.parameters.add("@property", sqldbtype.varchar) dataadapter.insertcommand.parameters.add("@value", sqldbtype.varchar) dataadapter.insertcommand.parameters.add("@capturedate", sqldbtype.datetime) integer = 0 datagridview.rows.count - 1 dataadapter.insertcommand.parameters(0).value = dgvserverconfig.rows(i).cells(0).value dataadapter.insertcommand.parameters(1).value = dgvserverconfig.rows(i).cells(1).value dataadapter.insertcommand.parameters(2).value = dgvserverconfig.rows(i).cells(2).value dataadapter.insertcommand.parameters(3).value = dgvserverconfig.rows(i).cells(3).value next connection.open() command.connection = connection dataadapter.selectcommand = command
what missing here? nothing getting inserted table. appreciated. said, i'm not familiar vb take easy on me.
well, need execute command, not add dataadapter
also, coded now, don't need dataadapter @ all.
dim connection new data.sqlclient.sqlconnection dim command new data.sqlclient.sqlcommand connection.connectionstring = "server= server; database= db; integrated security=true" command.commandtext = "insert <table> (col1, col2, col3, col4) values (@name, @property, @value, @date)" command.parameters.add("@servername", sqldbtype.varchar) command.parameters.add("@property", sqldbtype.varchar) command.parameters.add("@value", sqldbtype.varchar) command.parameters.add("@capturedate", sqldbtype.datetime) connection.open() command.connection = connection integer = 0 datagridview.rows.count - 1 command.parameters(0).value = dgvserverconfig.rows(i).cells(0).value command.parameters(1).value = dgvserverconfig.rows(i).cells(1).value command.parameters(2).value = dgvserverconfig.rows(i).cells(2).value command.parameters(3).value = dgvserverconfig.rows(i).cells(3).value command.executenonquery() next
however calls database insert 1 row @ time. think better @ sqldataadapter.update method resolves insert/update work 1 call.
using sqldataadapter.update method, requires save in global variable adapter used @ moment in have filled datagridview , add sqlcommandbuilder generates insertcommand, updatecommand , deletecommand
' @ form loading' dim adapter new oledbdataadapter() adapter.selectcommand = new oledbcommand("select col1, col2,col3,col4 table", connection) dim builder oledbcommandbuilder = new oledbcommandbuilder(adapter) connection.open() dim mytable datatable = new datatable adapter.fill(mytable) datagridview.datasource = mytable .... ' @ grid save' dim mytable = ctype(datagridview.datasource, datatable) adapter.update(mytable)
Comments
Post a Comment