c#,如何用查询搜索

时间:2015-04-15 17:26:55

标签: c# mysql

我想做搜索。我用c#connect和MySQL数据库。什么时候会在表之间创建实体关系图。我想使用查询,例如用客户的移动设备查找此特定任务属于特定客户。

我试试这个。

MySqlConnection connection = new MySqlConnection(MyConnectionString);
connection.Open();

try
{
    MySqlCommand cmd = connection.CreateCommand();


    cmd.CommandText = "SELECT *  FROM assignments  WHERE assignments.Customer_ID = customers.Customer_ID;
    //assignments and customers are tables
    MySqlDataAdapter adap = new MySqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    adap.Fill(ds);
    dataGridView5.DataSource = ds.Tables[0].DefaultView;


}
catch (Exception)
{

    //throw;

    MessageBox.Show("Please complete correct the fields");
}

finally
{

    if (connection.State == ConnectionState.Open)
    {

        connection.Clone();

    }
}

任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:0)

您的查询似乎有误。您需要正确加入表格,如下所示:

cmd.CommandText = "SELECT *  FROM assignments 
       inner join Customers on assignments.Customer_ID = customers.Customer_ID";

答案 1 :(得分:0)

String mobile = "";
using (MySqlConnection conn = new MySqlConnection(mySqlConn)) {
            try
            {
                conn.Open();
                string queryString = "SELECT mobile FROM assignments 
       inner join Customers on assignments.Customer_ID = customers.Customer_ID";
                MySqlCommand cmd = new MySqlCommand(queryString, conn);
                using (MySqlDataReader rdr = cmd.ExecuteReader()) {
                    while (rdr.Read())
                    {
                        mobile = rdr[0].ToString();
                    }
                }
            }
            catch (Exception ex) {  }
        }