为什么这段代码不能从ms-access表中查询预期的行? [英] Why cannot this code query expected rows from ms-access table?

查看:48
本文介绍了为什么这段代码不能从ms-access表中查询预期的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个Access表,即Projects,包括projectTitle和partyID的行,以及ProjectParty,包括title和ID行。

I have two Access tables, namely Projects, including the rows of projectTitle and partyID, and ProjectParty, including the rows of title and ID.

private void btnSearch_Click(object sender, EventArgs e)
       {
           OleDbConnection conn = new OleDbConnection();
           conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=HesabKetab.accdb;Persist Security Info=False;";

           //search in the database
           OleDbCommand oleCmd = new OleDbCommand();
           oleCmd.Connection = conn;
           if (radioBtnByTitle.Checked)
           {
               oleCmd.CommandText = "SELECT * FROM Projects WHERE projectTitle=@projectTitle";
               oleCmd.Parameters.AddWithValue("@projectTitle", txtProjectTitle.ToString());
           }
           else if (radioBtnByParty.Checked)
           {
               oleCmd.CommandText = "SELECT * FROM Projects WHERE partyID=@partyID";
               oleCmd.Parameters.AddWithValue("@partyID", comboParty.SelectedValue.ToString());
           }

           //execute query
           OleDbDataAdapter ole_da = new OleDbDataAdapter(oleCmd);
           DataTable dt= new DataTable();
           try
           {
               conn.Open();
               ole_da.Fill(dt);

           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.ToString());
           }
           dataGridViewDisplaySearchResults.DataSource = dt;
           conn.Close();

       }





在上面的代码中我试图检索_Projects_的值访问数据库表。第二个if成功,它将查询的行加载到DataGridView中。但第一个if(当为true时)不返回预期值。实际上,它不会在DataGridView中加载任何内容。当我尝试基于_projectTitle_进行选择时,我不知道为什么查询不起作用。我试过调试,但我不知道哪些参数传递给select命令。哪里错了?



In the above code I am trying to retrieve the values of the _Projects_ Access database table. The second if is successful and it loads the queried rows into DataGridView. But the first if (when true) does not return the expected values. In fact, it loads nothing into the DataGridView. I have no idea why the query does not work when I try to do the select based on _projectTitle_. I tried debugging but I got no clue which parameters were being passed to the select command. Where am I wrong?

推荐答案

简单:

Simple:
txtProjectTitle.ToString()

不返回TextBox包含的字符串:它返回一个包含控件类型的字符串:System.Windows.Forms.TextBox,Text:后跟文本内容。

所以除非这是你的一个项目的标题......



试试这个:

Does not return the string that the TextBox contains: it returns a string containing the type of the control instead: "System.Windows.Forms.TextBox, Text: " followed by the text content.
So unless this is the title of one of your projects...

Try this:

oleCmd.Parameters.AddWithValue("@projectTitle", txtProjectTitle.Text);


这篇关于为什么这段代码不能从ms-access表中查询预期的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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