C#ODBC mysql(null输出)

时间:2016-11-24 15:22:30

标签: c# odbc

谁能帮助我吗?我得到空输出。虽然数据库中存在数据。

string retrivenp = "select emp_email from E_details where emp_ID ='" + c_c + 
  "'AND emp_name = '" + s_s + "'AND emp_address = '" + n_n + 
  "'AND Date_joining = '" + Calendar1.SelectedDate + "'";

using (OdbcCommand comm1 = new OdbcCommand(retrivenp,con))
{
  using (OdbcDataReader read = comm1.ExecuteReader())
  {                          
    while(read.Read())
    {
      url_path = read.ToString();
      Label1.Text = url_path.ToString();
    }                           
  }
}

1 个答案:

答案 0 :(得分:0)

我认为带有很多引号的字符串在执行时会出现问题,可能会给出错误的查询 使用命令参数更好

 string retrivenp = "select emp_email from E_details where emp_ID = ? AND emp_name = ? AND emp_address = ? AND Date_joining = ?";

               using (OdbcCommand comm1 = new OdbcCommand(retrivenp,con))
                {
                  comm1.Parameters.Add("@p1", OleDbType.Int).Value =  c_c;
                  comm1.Parameters.Add("@p2", OleDbType.Text).Value =  s_s;
                  comm1.Parameters.Add("@p3", OleDbType.Text).Value =  n_n;
                  comm1.Parameters.Add("@p4", OleDbType.Date).Value =   Calendar1.SelectedDate;

                    using (OdbcDataReader read = comm1.ExecuteReader())
                    {                          
                        while(read.Read())
                        {
                            url_path = read.GetString(0);
                            Label1.Text = url_path.ToString();
                        }  
                         read.Close();


                    }
                }