在vb.net 中.........我用下面的代码想把在dataGridView 上直接更新的代码保存在数据库中
   Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Dim sSelectCmd As String
        Dim Fdt As DataSet = New DataSet
        Dim sMemberID As String, sFrom As String
        Dim sParentID As Integer
        Dim sobject As String, sTableAlias As String        sMemberID = DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(0).Value.ToString
        sFrom = DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(1).Value.ToString
        sParentID = DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(2).Value.ToString        sobject = DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(3).Value.ToString
        sTableAlias = DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(4).Value.ToString
        Dim adapter As New SqlDataAdapter()
        sSelectCmd = "Update dicFormObjectCond  set  TableAlias = '" & sTableAlias & "' ,ObjectID ='" & sobject & "' "
        sSelectCmd = sSelectCmd & "  where MemberID = '" & sMemberID & "' and "
        sSelectCmd = sSelectCmd & " FormID = '" & sFrom & "' and ParentID = '" & sParentID & "' "        DBopen()        adapter.SelectCommand = New SqlCommand(sSelectCmd, Conn)        Dim builder As SqlCommandBuilder = New SqlCommandBuilder(adapter)        adapter.Fill(Fdt)        builder.GetUpdateCommand()标   '''到这里为什么就出错了.....
        adapter.Update(Fdt)
    End Sub还有我这样写能不能把我在DataGridView上直接修改的数据保存到数据库中去....报错好象为"Dynamic SQL generation is not supported against a SelectCommand that does not return any base table information."是什么原因了.............该怎样修改哦.....

解决方案 »

  1.   

    VB我不熟,这样吧,给你一段代码,MSDN上的..下面的完整代码示例提供的按钮用于从数据库重新加载数据和向数据库提交更改。 Imports System
    Imports System.Data
    Imports System.Data.SqlClient
    Imports System.Windows.FormsPublic Class Form1
        Inherits System.Windows.Forms.Form    Private dataGridView1 As New DataGridView()
        Private bindingSource1 As New BindingSource()
        Private dataAdapter As New SqlDataAdapter()
        Private WithEvents reloadButton As New Button()
        Private WithEvents submitButton As New Button()    <STAThreadAttribute()> _
        Public Shared Sub Main()
            Application.Run(New Form1())
        End Sub    ' Initialize the form.
        Public Sub New()        Me.dataGridView1.Dock = DockStyle.Fill        Me.reloadButton.Text = "reload"
            Me.submitButton.Text = "submit"        Dim panel As New FlowLayoutPanel()
            panel.Dock = DockStyle.Top
            panel.AutoSize = True
            panel.Controls.AddRange(New Control() {Me.reloadButton, Me.submitButton})        Me.Controls.AddRange(New Control() {Me.dataGridView1, panel})
            Me.Text = "DataGridView databinding and updating demo"    End Sub    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
            Handles Me.Load        ' Bind the DataGridView to the BindingSource
            ' and load the data from the database.
            Me.dataGridView1.DataSource = Me.bindingSource1
            GetData("select * from Customers")    End Sub    Private Sub reloadButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
            Handles reloadButton.Click        ' Reload the data from the database.
            GetData(Me.dataAdapter.SelectCommand.CommandText)    End Sub    Private Sub submitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
            Handles submitButton.Click        ' Update the database with the user's changes.
            Me.dataAdapter.Update(CType(Me.bindingSource1.DataSource, DataTable))    End Sub    Private Sub GetData(ByVal selectCommand As String)        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=Northwind;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.
                Me.dataGridView1.AutoResizeColumns( _
                    DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader)
            Catch ex As SqlException
                MessageBox.Show("To run this example, replace the value of the " + _
                    "connectionString variable with a connection string that is " + _
                    "valid for your system.")
            End Try    End SubEnd Class
      

  2.   

    或者直接进MSDN看,地址如下:ms-help://MS.MSDNQTR.v80.chs/MS.MSDN.v80/MS.VisualStudio.v80.chs/dv_fxmclictl/html/1660f69c-5711-45d2-abc1-e25bc6779124.htm