我想使用此查询从SQL Server检索类别。我的数据库中有数据,此查询在SQL Server中运行良好。但是,当我将其与oledb命令一起使用时,它不会从服务器返回任何数据。有什么问题吗?
public Category getcategoryByCategoryName(string categoryName)
{
Category _category = null;
using (OleDbConnection con = new OleDbConnection(_connectionString))
{
string sql = "select * from Categories where CategoryName=?";
OleDbCommand cmd = new OleDbCommand(sql,con);
cmd.Parameters.AddWithValue("@cName",categoryName);
try
{
con.Open();
OleDbdataReader rdr = cmd.ExecuteReader();
if (rdr.HasRows)
{
while (rdr.Read())
{
_category.Id = Convert.ToInt32(rdr["Id"]);
_category.CategoryName = rdr["CategoryName"].ToString();
}
rdr.Close();
}
}
catch (Exception ex)
{
_category = null;
}
}
return _category;
}