使用MySQLDataAdapter输入字符串的格式不正确

时间:2013-07-19 10:59:01

标签: c# mysql string-formatting

我在这里查看了一些问题/答案,但似乎都没有帮助

我有以下方法从mySQL数据库中选择数据

我已突出显示错误发生的行。

传递的字符串sql是 SELECT * FROM tblDtCategory;

OpenDbConnection(dbConn))是打开连接并返回任何问题,运行正常

我无法理解为什么我收到这个错误,看看适配器一切似乎没问题,而且我不是要添加或更新数据库所以它不能是不匹配的类型... < / p>

    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;
    }

图像显示它必须已连接到数据库

enter image description here

图像显示应返回的内容 enter image description here

**注意记录: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

1 个答案:

答案 0 :(得分:0)

确定发现问题 - 学校男生错误lol

进入mySQL服务器并再次运行安装程序,但选择了UTF-8而不是文本格式的下拉选项之一,现在它可以正常工作

..我觉得很愚蠢:)