我在这里查看了许多问题/答案,但似乎都没有帮助
我有以下方法从 mySQL 数据库中选择数据
我已经突出显示了错误发生的行。
传递的字符串 sql 是SELECT * FROM tblDtCategory;
OpenDbConnection(dbConn)) 是打开连接并返回任何问题,这运行正常
我不明白为什么会出现此错误,查看适配器一切似乎都很好,而且我没有尝试向数据库添加或更新,因此它不会是不匹配的类型...
public DataTable Select(string sql)
{
MySqlConnection dbConn = new MySqlConnection(DbConnection);
MySqlDataAdapter adapter = new MySqlDataAdapter();
DataTable retTable = new DataTable();
if (OpenDbConnection(dbConn))
{
try
{
adapter.SelectCommand = new MySqlCommand(sql, dbConn);
adapter.Fill(retTable); // error here
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return null;
}
finally
{
CloseDbConnection(dbConn);
}
}
else
return null;
return retTable;
}
显示它必须已连接到数据库的图像
显示应该返回的图像
**注意记录:3-newcat-1 是通过将 select 语句更改为 insert 语句从我的应用程序中添加的 - 没问题(!)
堆栈跟踪:
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.Int32.Parse(String s, IFormatProvider provider)
at MySql.Data.Types.MySqlInt32.MySql.Data.Types.IMySqlValue.ReadValue(MySqlPacket packet, Int64 length, Boolean nullVal)
at MySql.Data.MySqlClient.NativeDriver.ReadColumnValue(Int32 index, MySqlField field, IMySqlValue valObject)
at MySql.Data.MySqlClient.Driver.ReadColumnValue(Int32 index, MySqlField field, IMySqlValue value)
at MySql.Data.MySqlClient.ResultSet.get_Item(Int32 index)
at MySql.Data.MySqlClient.MySqlDataReader.GetFieldValue(Int32 index, Boolean checkNull)
at MySql.Data.MySqlClient.MySqlDataReader.GetValue(Int32 i)
at MySql.Data.MySqlClient.MySqlDataReader.GetValues(Object[] values)
at System.Data.ProviderBase.DataReaderContainer.CommonLanguageSubsetDataReader.GetValues(Object[] values)
at System.Data.ProviderBase.SchemaMapping.LoadDataRow()
at System.Data.Common.DataAdapter.FillLoadDataRow(SchemaMapping mapping)
at System.Data.Common.DataAdapter.FillFromReader(DataSet dataset, DataTable datatable, String srcTable, DataReaderContainer dataReader, Int32 startRecord, Int32 maxRecords, DataColumn parentChapterColumn, Object parentChapterValue)
at System.Data.Common.DataAdapter.Fill(DataTable[] dataTables, IDataReader dataReader, Int32 startRecord, Int32 maxRecords)
at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataTable dataTable)
at Downtime_Aquisition_Administrator.MySQLServer.Select(String sql) in d:\VS2012\Projects\Downtime Acquisition\Downtime Aquisition Administrator\Downtime Aquisition Administrator\frmMain.cs:line 1996