我有以下例程(有效)但由于它使用的手型字符串而更新很麻烦:
Private Sub ListDefaults()
Dim conn As New SqlConnection( _
"server=bas047\AUTODESKVAULT;Database=DWGDetails;Integrated Security=SSPI")
'Dim conn As New SqlConnection( _
'"server=bas047\AUTODESKVAULT;Database=DWGDetails;Integrated Security=SSPI")
Try
'clear columns
If Not Me.DataGridView1.DataSource Is Nothing Then
Me.DataGridView1.Columns.Clear()
Me.DataGridView1.DataSource = Nothing
End If
conn.Open()
Dim cmd As SqlCommand = conn.CreateCommand()
Dim DLcmd As SqlCommand = conn.CreateCommand()
Dim ILcmd As SqlCommand = conn.CreateCommand()
'Dim srctbl As String = String.Empty
If RadioButton1.Checked = True Then 'A3
shtsize = "A3"
cmd.CommandText = "SELECT [AttributeName],[IsDefaultValue],[DefaultValue] FROM " & _
Chr(34) & "DefaultValues(Borders SB-A3_993-5.2(block))" & Chr(34)
srctbl = "DefaultValues(Borders SB-A3_993-5.2(block))"
ElseIf RadioButton2.Checked = True Then 'A2
shtsize = "A2"
cmd.CommandText = "SELECT [AttributeName],[IsDefaultValue],[DefaultValue] FROM " & _
Chr(34) & "DefaultValues(Borders SB-A2_992-5.2(block))" & Chr(34)
srctbl = "DefaultValues(Borders SB-A2_992-5.2(block))"
ElseIf RadioButton3.Checked = True Then 'A1
shtsize = "A1"
cmd.CommandText = "SELECT [AttributeName],[IsDefaultValue],[DefaultValue] FROM " & _
Chr(34) & "DefaultValues(Borders SB-A1_991-5.2(block))" & Chr(34)
srctbl = "DefaultValues(Borders SB-A1_991-5.2(block))"
ElseIf RadioButton4.Checked = True Then 'A0
shtsize = "A0"
cmd.CommandText = "SELECT [AttributeName],[IsDefaultValue],[DefaultValue] FROM " & _
Chr(34) & "DefaultValues(Borders SB-A0_990-5.2(block))" & Chr(34)
srctbl = "DefaultValues(Borders SB-A0_990-5.2(block))"
End If
'Populate datagridview1
da = New SqlDataAdapter(cmd.CommandText, conn.ConnectionString)
ds = New DataSet
da.Fill(ds, srctbl)
DataGridView1.DataSource = ds.Tables(0)
DataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.DisplayedCells)
'should enable setting of the date and drawn by entries
'Dim X As Integer
'For X = 0 To DataGridView1.Rows.Count
' If UCase(DataGridView1.Rows(X).Cells(0).Value) = "DRAWN BY" Then
' MessageBox.Show("Found Drawn by!")
' End If
'Next
Catch ex As System.Data.SqlClient.SqlException
MessageBox.Show("There was an error in executing the SQL." & vbLf & "Error Message:" & ex.Message, "SQL")
Finally
'cleanup and reset!
conn.Close()
End Try
End Sub
我认为这是Linq的理想候选人。这是我到目前为止似乎有效的方法:
Private Sub ListDefaultsMk1()
Try
Using db As New DWGDetailsDataContext
If RadioButton1.Checked = True Then 'A3
shtsize = "A3"
DataGridView1.DataSource =
From c In db.DefaultValues_Borders_SB_A3_993_5_2_block__s()
Select New With {c.AttributeName, c.IsDefaultValue, c.DefaultValue}
ElseIf RadioButton2.Checked = True Then 'A2
shtsize = "A2"
DataGridView1.DataSource =
From c In db.DefaultValues_Borders_SB_A2_992_5_2_block__s()
Select New With {c.AttributeName, c.IsDefaultValue, c.DefaultValue}
ElseIf RadioButton3.Checked = True Then 'A1
shtsize = "A1"
DataGridView1.DataSource =
From c In db.DefaultValues_Borders_SB_A1_991_5_2_block__s()
Select New With {c.AttributeName, c.IsDefaultValue, c.DefaultValue}
ElseIf RadioButton4.Checked = True Then 'A0
shtsize = "A0"
DataGridView1.DataSource =
From c In db.DefaultValues_Borders_SB_A0_990_5_2_block__s()
Select New With {c.AttributeName, c.IsDefaultValue, c.DefaultValue}
End If
End Using
Catch ex As Exception
MessageBox.Show("There was an error in executing the SQL." & vbLf & "Error Message:" & ex.Message, "SQL")
End Try
End Sub
我的程序的下一步是从datagridview获取(编辑过的)值(在手写SQL代码的情况下,共享DataSet“ds”)并填充另一个表但我无法弄清楚如何使用Linq做到这一点。
有人可以提供协助吗?
提前致谢,
亚历。
编辑:这是我根据评论
将其缩减的内容 Private Sub ListDefaultsMk1()
Try
Using db As New DWGDetailsDataContext
If RadioButton1.Checked = True Then 'A3
shtsize = "A3"
DataGridView1.DataSource = (From c In db.DefaultValues_Borders_SB_A3_993_5_2_block__s Select c).ToList()
ElseIf RadioButton2.Checked = True Then 'A2
shtsize = "A2"
DataGridView1.DataSource = (From c In db.DefaultValues_Borders_SB_A2_992_5_2_block__s Select c).ToList()
ElseIf RadioButton3.Checked = True Then 'A1
shtsize = "A1"
DataGridView1.DataSource = (From c In db.DefaultValues_Borders_SB_A1_991_5_2_block__s Select c).ToList()
ElseIf RadioButton4.Checked = True Then 'A0
shtsize = "A0"
DataGridView1.DataSource = (From c In db.DefaultValues_Borders_SB_A0_990_5_2_block__s Select c).ToList()
End If
End Using
Catch ex As Exception
MessageBox.Show("There was an error in executing the Linq2SQL Query!" & vbLf & "Error Message:" & ex.Message, "SQL")
End Try
End Sub
答案 0 :(得分:0)
Linq2Sql和DataSet是两个完全不同的野兽,你可以在网格中使用它们的原因是因为网格可以消耗更多类型而不仅仅是数据集。
您可以使用,例如ToList()
在技术上确定linq查询结果,然后将List绑定到网格。之后所有的编辑都会反映在List上,您可以根据自己的意愿将其转换为其他格式(除了表格,数据集中的表格(必须是数据集)之外,您没有提供有关数据应该去哪里的更多信息)在数据库等)。