SQL类条件不能显示我的搜索数据c#和Ms Access
本文关键字:数据 Ms Access 搜索 我的 条件 不能 显示 SQL | 更新日期: 2023-09-27 18:09:45
我尝试通过MS Access (SELECT mname, mcellno WHERE mcellno LIKE '0922*')进行查询,结果显示我想要的数据。而这个
private Int32 getMemberNo()
{
con.ConnectionString = db.konek();
String sql = "SELECT COUNT(*) AS x FROM members";
Int32 count = 0;
try
{
con.Open();
OleDbCommand cmd = new OleDbCommand(sql, con);
OleDbDataReader rdr = cmd.ExecuteReader();
if (rdr.Read())
{
count = Convert.ToInt32(rdr["x"].ToString());
}
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex);
}
finally
{
con.Close();
}
return count;
}
public String[,] displayMobilePrefix() //THIS FUNCTION
{
con.ConnectionString = db.konek();
String sql = "SELECT mname, mcellno FROM members WHERE mcellno LIKE '" + @mcellno + "*'";
int count = this.getMemberNo();
String[,] ars = new String[2, count];
try
{
con.Open();
OleDbCommand cmd = new OleDbCommand(sql, con);
cmd.Parameters.Add("@mcellno", OleDbType.VarChar, 30).Value = this.mcellno;
OleDbDataReader rdr = cmd.ExecuteReader();
int counter = 0;
while (rdr.Read())
{
ars[0, counter] = rdr["mname"].ToString();
ars[1, counter] = rdr["mcellno"].ToString();
counter++;
}
}
finally
{
con.Close();
}
return ars; //never returns any data
}
它不会返回任何数据。然而,我试图在我的字符串查询上添加一个参数,它什么也不返回。
使用OleDb,您的Access查询必须使用%
而不是*
作为通配符。
String sql = "SELECT mname, mcellno FROM members WHERE mcellno LIKE '" + @mcellno + "%'";