2011-02-03 59 views
2

我有以下程序(的作品),但它是凌亂的,由於它採用了手工輸入的字符串來更新:如何轉換這種基於字符串的SQL查詢使用Linq

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代碼,共享數據集「的情況下, ds「)並填充另一個表,但我無法弄清楚如何使用Linq來做到這一點。

任何人都可以提供幫助嗎?

在此先感謝,

Alex。

編輯:這就是我這降低到基於註釋

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

LINQ2SQL和數據集是兩個完全不同的東西,你既可以在網格中使用它們的原因是因爲網格可以消耗比僅僅數據集更多的類型。

您可以使用例如ToList()在技術上鎖定linq查詢結果,然後將List綁定到網格。之後,所有編輯都會反映在List上,您可以根據自己的想法將其轉換爲其他格式(除了數據集中的表,數據集中的表(必須是數據集),表在數據庫等)。

+0

感謝mmix,我現在就給我看看我有一些空閒時間 - 而我剛纔還沒有看到你的回覆。 :( – AlexFielder 2011-03-04 09:43:57