从访问数据库中检索数据

时间:2013-05-20 05:29:58

标签: c#-4.0

我想从访问

中检索图像的路径
DataTable myTable = new DataTable();
OleDbConnection myConnection = new OleDbConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

OleDbCommand myCommand = new OleDbCommand();
myCommand.CommandText = "SELECT ImageName AS 'ImageName', ImagePath AS 'Path' FROM [AImages]  WHERE ID='" + _ID + "'";
myCommand.CommandType = CommandType.Text;
myCommand.Connection = myConnection;

OleDbDataAdapter myAdapter = new OleDbDataAdapter();

myAdapter.SelectCommand = myCommand;
myAdapter.Fill(myTable);

但在最后一行中发生了如下错误:条件表达式中的数据类型不匹配。

1 个答案:

答案 0 :(得分:0)

我怀疑问题在于您传递ID的方式,也许您可​​以尝试这样做:

// note the ID=?
myCommand.CommandText = "SELECT ImageName AS 'ImageName', ImagePath AS 'Path' FROM [AImages]  WHERE ID=?";
myCommand.CommandType = CommandType.Text;

// now a parameter
var pId = new OleDbParameter {Value = _ID};
myCommand.Parameters.Add(pId);

我希望这会有所帮助。