在 vb.net 中列出 ms sql 数据

发布于 2024-08-24 22:12:38 字数 966 浏览 3 评论 0原文

我现在代码中没有错误,但它似乎不起作用。唯一有效的是当我尝试列出所有数据时。

但是当我尝试缩小要列出的数据范围时。我没有得到好的结果。这是我的代码:

 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

上面的代码并不是全部。我省略了那些不相关的内容。我该怎么做才能完成这项工作?请帮忙,谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瑶笙 2024-08-31 22:12:38

问题很可能是:

WHERE (SCHOOL='" & TextBox1.Text & "')"

部分。如果忽略对基本安全原则的公然无知(有空时请阅读 SQL 注入),这是一个 我的标准做法

  • 学校不等于学校。

是:

  • TRIM。不确定 VB 语法,但说 & TextBox1.Text.Trim () - 去掉两者上的空格 人们可能会输入但看不到。
  • 不要使用“=”,通常使用“LIKE”(除非有人更改)LINKE 不会区分“School”和“school”,然后

使用 SQL 进行计算。 . SQL 管理器。

The problem is most likely the:

WHERE (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.

  • 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.
  • Dont use "=", use "LIKE". NORMALLY (unless somone changed that) LINKE will not differentiate between "School" and "school"

Take the SQL and work it out in... SQL Manager.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文