从数据网格更新数据库 [英] Update a Database from a Datagrid
问题描述
大家好,
马丁,
我在获取更新和删除以返回到我的SQL数据库时遇到问题.添加新记录是可以的.但是,如果我尝试删除或更改记录,然后使用按钮进行更新,则会出现此错误:
不针对不返回任何键列的SelectCommand,不支持UpdateCommand的动态SQL生成信息.
我尝试使用SQL命令构建器中包含的update命令,但它也给我一个错误.我认为我编码错了.这是我的代码:
要打开记录集并将其放置在datagrid中,我具有以下子例程:
Hello All,
Martin,
I am having a problem getting updates and deletions to go back to my SQL database. Adding a new record is fine. But if I try to delete or change a record and then use a button to update I get this error:
Dynamic SQL generation for the UpdateCommand is not supported against a SelectCommand that does not return any key column information.
I have tried to use the update command that is part of the SQL command builder but it gives me an error as well. I think that I have coded it wrong. Here is my code:
To open the recordset and place it in the datagrid I have the following subroutine:
尝试 |
指定连接字符串.将给定值替换为 |
Northwind SQL Server示例的有效连接字符串 |
您的系统可访问的数据库. |
昏暗的连接字符串As String == |
安全性 = SSPI ;持久安全性 信息 = False ;" + _ |
最初的 目录 = ;数据 = " |
基于指定的查询创建新的数据适配器. |
Me.dataAdapter = > 新建 SqlDataAdapter(selectCommand,connectionString) |
创建一个命令生成器以生成SQL更新,插入和 |
基于selectCommand的删除命令.这些用于 |
更新数据库. |
作为新的SqlCommandBuilder(Me.dataAdapter)的Dim commandBuilder |
填充一个新的数据表并将其绑定到BindingSource. |
昏暗表为新的DataTable() |
table.Locale = 系统 .Globalization.CultureInfo.InvariantCulture |
Me.dataAdapter.Fill(table) |
Me.BindingSource1.DataSource = |
调整DataGridView列的大小以适合新加载的内容. |
DataGrid1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells) |
捕获(例如SqlException) |
MsgBox(``连接错误.联系您的系统管理员以验证对SQL数据库的访问'',``访问错误'') |
结束尝试 |
End Sub |
Try |
' Specify a connection string. Replace the given value with a |
' valid connection string for a Northwind SQL Server sample |
' database accessible to your system. |
Dim connectionString As String = _ |
"Integrated Security=SSPI;Persist Security Info=False;" + _ |
"Initial Catalog=DCP;Data Source=localhost" |
' Create a new data adapter based on the specified query. |
Me.dataAdapter = New SqlDataAdapter(selectCommand, connectionString) |
' Create a command builder to generate SQL update, insert, and |
' delete commands based on selectCommand. These are used to |
' update the database. |
Dim commandBuilder As New SqlCommandBuilder(Me.dataAdapter) |
' Populate a new data table and bind it to the BindingSource. |
Dim table As New DataTable() |
table.Locale = System.Globalization.CultureInfo.InvariantCulture |
Me.dataAdapter.Fill(table) |
Me.BindingSource1.DataSource = table |
' Resize the DataGridView columns to fit the newly loaded content. |
DataGrid1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells) |
Catch ex As SqlException |
MsgBox("Connections Error. Contact your system administrator to verify access to SQL database", , "Access Error") |
End Try |
End Sub |
推荐答案
复杂,难怪您会头昏眼花的:D,请尝试以下操作
http://msdn.microsoft.com/zh-cn/vbasic/bb643827.aspx
its too complicated, no wonder you getting dizzy :D, try this one
http://msdn.microsoft.com/en-us/vbasic/bb643827.aspx
这篇关于从数据网格更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!