简体   繁体   English

在vb.net中列出MS SQL数据

[英]List ms sql data in vb.net

I got no errors in the code now, but it doesn't seem to work. 我现在在代码中没有错误,但是似乎没有用。 The only thing that works is when I try to list all the data. 唯一有效的方法是当我尝试列出所有数据时。

But when I try to narrow the data that is to be listed. 但是,当我尝试缩小要列出的数据时。 I get no good results. 我没有好结果。 Here is my code: 这是我的代码:

 If ComboBox1.SelectedItem = "School" Then
            Dim connectionString As String = "Data Source=SENBONZAKURA\SQLEXPRESS;Initial Catalog=testing;User ID=SenbonZakura\Rew; Trusted_Connection=True;"

            Dim selectCommand As String

            Dim connection As New SqlConnection(connectionString)

            selectCommand = "select * from student  WHERE (SCHOOL='" & TextBox1.Text & "')"

            Me.dataAdapter = New SqlDataAdapter(selectCommand, connection)



            Dim commandBuilder As New SqlCommandBuilder(Me.dataAdapter)

            Dim table As New DataTable()

            Me.dataAdapter.Fill(table)

            Me.BindingSource1.DataSource = table

            Dim data As New DataSet()

            DataGridView1.DataSource = Me.BindingSource1
END IF

The code above is not the whole thing. 上面的代码不是全部。 I've omitted those that are not relevant. 我忽略了那些不相关的内容。 What do I do to make this work?Please help, thanks. 我该怎么做才能工作?请帮助,谢谢。

The problem is most likely the: 问题很可能是:

WHERE (SCHOOL='" & TextBox1.Text & "')" 在哪里(SCHOOL ='“&TextBox1.Text&”')“

part. 部分。 Ifgnoring the blatant ignorance of basic securtiy principles (read up on SQL Injection when you ahve the moment), this is a full comparison. 如果忽略了基本安全性原则的公然无知(请立即阅读SQL注入),这是一个完整的比较。

  • School is not equal to school. 学校不等于学校。
  • Leating/trailing spaces are evil. 闲逛/拖尾是邪恶的。

My standard practice would be: 我的标准做法是:

  • TRIM. 修剪。 Not sure about the VB syntax, but say & TextBox1.Text.Trim () - get rid of spaces on both ends that people just may enter and not see. 不确定VB语法,但是说&TextBox1.Text.Trim()-消除人们可能会进入而看不到的两端的空格。
  • Dont use "=", use "LIKE". 不要使用“ =”,请使用“ LIKE”。 NORMALLY (unless somone changed that) LINKE will not differentiate between "School" and "school" 正常情况下(除非有人更改),LINKE不会区分“学校”和“学校”

Take the SQL and work it out in... SQL Manager. 使用SQL并在... SQL Manager中进行处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM