向 OleDbCommand 添加 Where 子句

编程入门 行业动态 更新时间:2024-10-18 06:05:57
本文介绍了向 OleDbCommand 添加 Where 子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在尝试向以下代码行添加 where 子句.

I am trying to add a where clause to the following line of code.

这是因为我从下拉组合框中获取数据表.现在我想根据用户名过滤该表,以便只有用户才能看到他们的记录.

the reason for this is because i get the datatable from a dropdown combobox. now i want to filter that table on user name, so that only the user can see their records.

我需要有关如何将 where 子句写入此代码的帮助.

i need help on how to write the where clause into this code.

如果您需要更多信息,我很乐意添加.

if you need any more information i will gladding add it.

感谢您的帮助.

OleDbCommand cmd = new OleDbCommand(String.Concat("Select * From ", comboBox1.Text), con);

评论后

我添加了sql注入保护.

i added the sql injection protection.

OleDbCommand cmd = new OleDbCommand(String.Concat("Select * From 
@Companydetails where Research_ID = @Researcher_ID"), con);

        cmd.Parameters.AddWithValue("@Companydetails", comboBox1.Text);
        cmd.Parameters.AddWithValue("@Researcher_ID", usernumber_lab.Text);

但现在它给了我一个错误说:

but now it is giving me a error saying:

附加信息:查询中的语法错误.不完整的查询子句.

Additional information: Syntax error in query. Incomplete query clause.

还有什么我需要添加来完成这个查询吗?

is there something else i need to add to finnish this query off?

推荐答案

请试试这个

string sql = String.format("Select * From {0} where id = {1}", comboBox1.Text, id);
OleDbCommand cmd = new OleDbCommand(sql,con);

这篇关于向 OleDbCommand 添加 Where 子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-26 03:46:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1128957.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:子句   OleDbCommand

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!