向 OleDbCommand 添加 Where 子句 [英] Adding a Where Clause to a OleDbCommand

查看:33
本文介绍了向 OleDbCommand 添加 Where 子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向以下代码行添加 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆