简体   繁体   English

通过传递组合框textvalue的MS Access使用WHERE子句读取列

[英]Reading columns using WHERE clause on MS Access passing combobox textvalue

I am working on a windows application written in C# and using MS Access 2003 as my database. 我正在使用C#编写的Windows应用程序上工作,并使用MS Access 2003作为数据库。 I am facing strange problem, when I write the query 我写查询时遇到奇怪的问题

SELECT * FROM mytable WHERE columnname='"+combobox.text+"' 

and execute using oledbadapter, the dataset visualizer doesn't recognise the where clause and returns empty table. 并使用oledbadapter执行,数据集可视化器将无法识别where子句并返回空表。 Whereas 鉴于

SELECT * FROM mytable WHERE columnname='"+integervalue+"

will return the filtered column. 将返回过滤的列。

Here is my code snippet: 这是我的代码段:

private void viewinfo_Click(object sender, EventArgs e){
    dataGridView1.Visible = true;
    OleDbCommand cmd;
    string schoolname = cmbschoolname.Text;
    OleDbDataAdapter da = new OleDbDataAdapter("select  * from tblmedic where medicalschool ='"+combobox.text+"'", conn);
    DataTable dt = new DataTable();
    da.Fill(dt);
    dataGridView1.DataSource = dt;
}

output: returns empty table when I pass the text value in the where-clause. 输出:当我在where子句中传递文本值时,返回空表。

I have resolved this issue. 我已经解决了这个问题。 it was the case of empty spaces in my access 2003 table columns. 这是我的Access 2003表列中有空白的情况。 so used a trim() to clear the spaces while selecting the columns in where clause. 因此在选择where子句中的列时使用了trim()来清除空格。

private void viewinfo_Click(object sender, EventArgs e){
    dataGridView1.Visible = true;
    OleDbCommand cmd;
    string schoolname = cmbschoolname.Text;
    OleDbDataAdapter da = new OleDbDataAdapter("select  * from tblmedic where medicalschool ='"+combobox.text.ToString().Trim()+"'", conn);
    DataTable dt = new DataTable();
    da.Fill(dt);
    dataGridView1.DataSource = dt;
}

Sometimes silly mistakes makes u crave like anything :-) 有时候愚蠢的错误会让你渴望任何东西:-)

:-) :-)

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

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