2012-05-08 29 views
0

我coppied一些代碼HERE但我得到一個錯誤:「不正確的語法附近‘)’ 但我看不出什麼毛病此代碼DataAdaptor.Fill錯誤

Dim conString As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\users.mdf;Integrated Security=True;User Instance=True" 
     Dim con As New SqlConnection(conString) 
     con.Open() 
     Try 
      Dim dataAdapter1 As New SqlDataAdapter(_ 
       New SqlCommand("SELECT subject, info, username, status FROM(problems) WHERE (username =" & userName & ")", con)) 
      Dim ds As New DataSet("DataSetMyProbs") 
      ds.Tables.Add("UserProbs") 
      dataAdapter1.Fill(ds.Tables("UserProbs")) '//This is where i get the error' 
      Me.bsMyProblems.DataSource = ds.Tables("UserProbs") 
      Dim dataAdapter2 As New SqlDataAdapter(_ 
       New SqlCommand("SELECT dep, pcid, username, status, extraInfo FROM(deployments) WHERE (username = " & userName & ")", con)) 
      ds.Tables.Add("UsersDepl") 
      dataAdapter2.Fill(ds.Tables("UserDepl")) 
      Me.bsMyDepl.DataSource = ds.Tables("UserDepl") 
     Catch ex As Exception 
      MessageBox.Show(ex.ToString) 
     Finally 
      con.Dispose() 
     End Try 

回答

1

我假設,因爲你的查詢,可能是你的參數username字符串你可能需要把?對於字符串單引號,也給出了空間之間的「FROM」和表名

Dim dataAdapter1 As New SqlDataAdapter(_ 
      New SqlCommand("SELECT subject, info, username, status FROM [problems] WHERE (username ='" & userName & "')", con)) 
+0

感謝,只有一件事... 我不是現在得到的錯誤,但我加載了錯誤的查詢,我真的不明白這一點。我討厭SQL,mysql更容易 –

+0

??你能幫助我嗎?爲什麼它總是加載1個特定用戶,即使我在查詢中發送其他用戶。這可能是綁定導航器還是其他東西 –

+0

當您在查詢中發送其他用戶時,您是否使用IN?或者只是用不同的參數再次調用函數? – Nick

0

問題。是在SQL語句中

它應該是:

SELECT subject, info, username, status FROM [problems] ....