尝试以与数组不兼容的类型访问元素. [英] Attempted to access an element as a type incompatible with the array.

查看:139
本文介绍了尝试以与数组不兼容的类型访问元素.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的人们

我对数组有问题.在我的form_load im中,通过使用OleDbDataReader对象执行sql语句.这是我的代码...

Dear People

Im having a problem with array. On my form_load im executing an sql statement by using an OleDbDataReader object. Here is my code...

public partial class DtposMDIParentSystem : Form
    {
        
        List<string[]> result = new List<string[]>();
        
        public DtposMDIParentSystem()
        {
            InitializeComponent();
        }
        
        private void DtposMDIParentSystem_Load(object sender, EventArgs e)
        {
            //create the database connection
            OleDbConnection aConnection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\AP_AE\Desktop\DTPOS_APP\DataBase\DtposDatabase.accdb;");
            
            //connection = new OleDbConnection(connectionString);
            //create the command object and store the sql query
            OleDbCommand aCommand = new OleDbCommand("SELECT * FROM Food", aConnection);
            try
            {
                aConnection.Open();
                //create the datareader object to connect to table
                OleDbDataReader reader = aCommand.ExecuteReader();
                int i = 0;
                while (reader.Read())
                {
                    result.Add(new string[reader.FieldCount]);
                    reader.GetValues(result[i]);
                }
                reader.Close();
                aConnection.Close();

            }
            catch (InvalidOperationException ex)
            {
                MessageBox.Show("Invalid Masseage = " + ex.Message);
            
            }
}



当我运行我的应用程序时,我会收到一条消息,说
ArrayTypeMismatchException未处理

尝试以与数组不兼容的类型访问元素.


任何帮助都将非常非常好...

在此先感谢


亲切的问候

Roni



When I run my application I get a message saying

ArrayTypeMismatchException was unhandled

Attempted to access an element as a type incompatible with the array.


Any help would be very very greatfull...

thanks in advance


Kind regards

Roni

推荐答案

Lapec,

您的问题在于结果的声明:

Hi Lapec,

your problem is in the declaration of result:

List<string[]>



这行在这里:



and this line here:

reader.GetValues(result[i])



读者正在尝试将某些可能不是字符串的内容存储到字符串数组中.从数据库返回的内容并不是所有的列都是字符串类型的.您可以通过将餐桌食物的所有列都转换为字符串来获得成功,但这可能不适用于您使用的数据库.

尝试像这样去:List<object[]> result = ...
然后,当您访问读取的内容时,您将必须检测类型并进行显式强制转换以访问其中的任何内容.


修改:



The reader is trying to store something into the string array that is most likely not a string. What ever is returned from the DB not all columns are of string type. You might have success by turning all of the columns of table food into strings, but that might not work for the DB your using.

Try to go like this: List<object[]> result = ...
Then later on when you access what was read you''d have to detect the type and make an explicit cast to access whatever is there.


Modification:

List<object[]> result = new List<Object[]>();
...
result.Add(new Object[reader.FieldCount]);
reader.GetValues(result[i]);



干杯


Manfred



Cheers


Manfred


如果与List<>相比,您更愿意处理DataTables,则可以考虑使用以下方法将数据放入DataTable:
If you are more comfortable dealing with DataTables than List<>s, you might consider putting your data into a DataTable using this:
public class DBUtilities
{
    public static DataTable ReaderToTable(DbDataReader reader)
    {
        DataTable newTable = new DataTable();
        DataColumn col;
        DataRow row;
        int i;

        for (i = 0; i < reader.FieldCount; i++)
        {
            col = new DataColumn();
            col.ColumnName = reader.GetName(i);
            col.DataType = reader.GetFieldType(i);

            newTable.Columns.Add(col);
        }

        while (reader.Read())
        {
            row = newTable.NewRow();
            for (i = 0; i < reader.FieldCount; i++)
            {
                row[i] = reader[i];
            }

            newTable.Rows.Add(row);
        }

        return newTable;
    }
}



您可以像这样在代码中使用它:



you would use it in your code like this:

public partial class DtposMDIParentSystem : Form
    {
        
        DataTable result = null;
        
        public DtposMDIParentSystem()
        {
            InitializeComponent();
        }
        
        private void DtposMDIParentSystem_Load(object sender, EventArgs e)
        {
            //create the database connection
            OleDbConnection aConnection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\AP_AE\Desktop\DTPOS_APP\DataBase\DtposDatabase.accdb;");
            
            //connection = new OleDbConnection(connectionString);
            //create the command object and store the sql query
            OleDbCommand aCommand = new OleDbCommand("SELECT * FROM Food", aConnection);
            try
            {
                aConnection.Open();
                //create the datareader object to connect to table
                OleDbDataReader reader = aCommand.ExecuteReader();
                result = DBUtilities.ReaderToTable(reader);

                reader.Close();
                aConnection.Close();

            }
            catch (InvalidOperationException ex)
            {
                MessageBox.Show("Invalid Masseage = " + ex.Message);
            
            }
}


这篇关于尝试以与数组不兼容的类型访问元素.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆