我知道这是一个热门话题,但在将数据网格中的数据更改为SQL时,我似乎在努力处理表名。下面是我的代码填充数据网格,我很遗憾它比一般的例子更加定制,但对于那里的专业人士来说应该仍然很简单:)
Private Sub loadNotify(ByVal select_date As String, ByVal select_ref As String, ByVal select_status As String)
myConn.Open()
Try
'*********************************************************
sqlstr = "SELECT cust_id, emp_name, notification, viewed, stamp, Entity_1.Cat_1 FROM notifications LEFT JOIN Phocas_Sales.dbo.Entity_1 ON Mill_CRM.dbo.notifications.cust_id = Phocas_Sales.dbo.Entity_1.LinkField WHERE viewed = 'false'"
'If select_date <> "" And select_date > 0 Then sqlstr &= " AND MONTH(stamp) = " & select_date
'If select_ref <> "" And select_ref <> "all" Then sqlstr &= " AND reference = '" & select_ref & "'"
'If select_status <> "" And select_status <> "all" Then sqlstr &= " AND status = '" & select_status & "'"
sqladapter = New SqlClient.SqlDataAdapter(sqlstr, myConn)
sqladapter.TableMappings.Add("Table", "blah")
Dim ds As New DataSet
sqladapter.Fill(ds)
' get the table from the data set
dtable = ds.Tables("blah")
' clear the DataGrid control
dgNotify.Rows.Clear()
' display items in the ListView control
For i As Integer = 0 To dtable.Rows.Count - 1
Dim drow As DataRow = dtable.Rows(i)
' only rows that have not been deleted
If drow.RowState <> DataRowState.Deleted Then
' define the datagrid items and arrangement
Dim row As String() = New String() {
drow("viewed").ToString,
drow("cust_id").ToString(),
StrConv(drow("Cat_1").ToString, VbStrConv.ProperCase),
drow("notification").ToString(),
StrConv(drow("emp_name").ToString, VbStrConv.ProperCase),
drow("stamp").ToString()
}
dgNotify.Rows.Add(row)
End If
Next
'dgNotify.DataSource = ds
dgNotify.AutoResizeColumns()
Catch ex As Exception
ErrHandle("CRM", ex.Message)
End Try
myConn.Close()
End Sub
以下是应该保存它的代码:
Private Sub dgNotify_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles dgNotify.CellEndEdit
cmdbuilder = New SqlCommandBuilder(sqladapter)
sqladapter.Update(ds, "blah")
Debug.WriteLine("*** INSERT ***")
Debug.WriteLine(cmdbuilder.GetInsertCommand.CommandText)
Debug.WriteLine("*** UPDATE ***")
Debug.WriteLine(cmdbuilder.GetUpdateCommand.CommandText)
Debug.WriteLine("*** DELETE ***")
Debug.WriteLine(cmdbuilder.GetDeleteCommand.CommandText)
结束子
我确实希望详细说明保存代码但是已将其删除以找到问题,到目前为止我收到了错误Unable to find the TableMapping['blah']
我猜你不能跨不同的子程序访问映射名称?如果是这种情况,我的表格是什么以及如何从保存例程中访问它?
我尝试使用sqladapter.Update(ds,“Table”)并得到另一个错误“我不了解多个基表的动态SQL生成”,但这只会让我更加困惑如何保存。
有人能告诉我这个问题有多简单吗?
Public Class frmMain
Private myConn As New SqlConnection(My.Settings.Mill_SQL_Connect)
Private sqlstr As String = String.Empty
Private sqladapter As SqlDataAdapter
Private cmdbuilder As SqlCommandBuilder
Private dtable As DataTable
Private ds As New DataSet
Private changes As DataSet
Private Sub frmMain...
提前致谢, 詹姆斯