在 MS Access 上使用 WHERE 子句传递组合框文本值来读取列
我正在开发一个用 C# 编写的 Windows 应用程序,并使用 MS Access 2003 作为我的数据库。我面临奇怪的问题,当我编写查询
SELECT * FROM mytable WHERE columnname='"+combobox.text+"'
并使用 oledbadapter 执行时,数据集可视化工具无法识别 where
子句并返回空表。而
SELECT * FROM mytable WHERE columnname='"+integervalue+"
将返回过滤后的列。
这是我的代码片段:
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;
}
输出:当我在 where 子句中传递文本值时,返回空表。
I am working on a windows application written in C# and using MS Access 2003 as my database. 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. 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经解决了这个问题。这是我的 access 2003 表列中有空格的情况。因此,在选择where子句中的列时,使用trim()来清除空格。
有时愚蠢的错误会让你渴望任何东西:-)
:-)
I have resolved this issue. it was the case of empty spaces in my access 2003 table columns. so used a trim() to clear the spaces while selecting the columns in where clause.
Sometimes silly mistakes makes u crave like anything :-)
:-)