如何在我的Form Vb Net中从没有事件的数据库MySQL中获取值

时间:2015-02-05 05:54:18

标签: mysql vb.net

我有问题从我的数据库中获取值idsiswa,而我的表单上没有任何事件, 这是我的代码:

GetIdNilai = {DRnilai.Item("idsiswa")}

StrSQL = "SELECT * from nilai WHERE idsiswa = '" & GetIdNilai.ToString & "' and kodepelajaran = '" & KODEPEL & "' AND semester='" & cSEMESTER & "' AND tahun = '" & THNPEL & "' AND kodeulangan='" & KODEUL & "' order by idsiswa "

CMDSiswa.CommandText = StrSQL
DRsiswa = CMDSiswa.ExecuteReader()
DataGridView1.Rows.Clear()

While DRsiswa.Read
NOMOR = NOMOR + 1
DATA = {NOMOR.ToString, DRsiswa.Item("idsiswa"), DRsiswa.Item("nama"), 0, cSEMESTER, THNPEL}
DataGridView1.Rows.Add(DATA)
End While

它不起作用,谢谢

1 个答案:

答案 0 :(得分:0)

如果您只是绑定到数据源,这将更加简单。此外,永远不要 EVER 使用这样的字符串连接来构建您的查询。下面的代码演示了如何处理这两个问题:

GetIdNilai = {DRnilai.Item("idsiswa")}

StrSQL = _ 
  "SELECT idsiswa, nama, 0, @Semester, @THNPEL" &
  "FROM nilai " &
  "WHERE idsiswa = @IDNilai AND kodepelajaran = @KODEPEL " &
     " AND semester = @Semester AND tahun = @THNPEL AND kodeulangan = @KODEUL " &
   "ORDER BY idsiswa"

CMDSiswa.CommandText = StrSQL  
'I'm guessing at parameter types here. Use the actual column types and lengths from your database 
CMDSiswa.Parameters.Add("@IDNilai", MySqlDbtype.Int32).Value = CInt(GetIdNilai)
CMDSiswa.Parameters.Add("@KODEPEL", MySqlDbType.VarChar, 50).Value = KODEPEL
CMDSiswa.Parameters.Add("@Semester", MySqlDbType.VarChar, 1).Value = cSEMESTER
CMDSiswa.Parameters.Add("@THNPEL", MySqlDbType.VarChar, 50).Value = THNPEL
CMDSiswa.Parameters.Add("@KODEUL", MySqlDbType.VarChar, 2).Value = KODEUL

DRsiswa = CMDSiswa.ExecuteReader()
DataGridView1.DataSource = DRsiswa