Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用对象数组获取ArrayTypeMismatchException_C#_.net_Exception - Fatal编程技术网

C# 使用对象数组获取ArrayTypeMismatchException

C# 使用对象数组获取ArrayTypeMismatchException,c#,.net,exception,C#,.net,Exception,我正在编写一个非常简单的方法,将csv转换为datatable,以便将其插入SQL数据库。除非我尝试用DBNull.Value替换空字符串,否则它会抛出ArrayTypeMismatchException。传入的流是一个简单的逗号分隔文件,其中包含数据 问题代码: public static DataTable StreamToTable(Stream stream, bool headersAvailable, bool convertBlankToDBNull) {

我正在编写一个非常简单的方法,将csv转换为datatable,以便将其插入SQL数据库。除非我尝试用DBNull.Value替换空字符串,否则它会抛出ArrayTypeMismatchException。传入的流是一个简单的逗号分隔文件,其中包含数据

问题代码:

public static DataTable StreamToTable(Stream stream, bool headersAvailable, bool convertBlankToDBNull)
    {

        DataTable data = new DataTable();
        StreamReader reader = new StreamReader(stream);

        if (!reader.EndOfStream)
        {
            if (headersAvailable)
            {
                try
                {
                    //get headers from first line
                    string[] headers = reader.ReadLine().Split(',');

                    //construct headers
                    for (int i = 0; i < headers.Length; i++)
                    {
                        data.Columns.Add(headers[i]);
                    }
                }
                catch (IOException ioEx)
                {
                    return null;
                }
            }

            while (!reader.EndOfStream)
            {
                object[] row = reader.ReadLine().Split(',');

                if (convertBlankToDBNull)
                {
                    for (int i = 0; i < row.Length; i++)
                    {
                        if (row[i].ToString().Equals(""))
                        {
                            row[i] = DBNull.Value; //this is where I get the exception
                        }
                    }
                }
                data.Rows.Add(row);
            }

            return data;
        }
        else return null;
    }
公共静态数据表StreamToTable(Stream-Stream、bool-headers-available、bool-converter-blanktodbnull)
{
DataTable数据=新DataTable();
StreamReader=新的StreamReader(流);
if(!reader.EndOfStream)
{
如果有(可用的标题)
{
尝试
{
//从第一行获取标题
string[]headers=reader.ReadLine().Split(',');
//构造标题
for(int i=0;i

我不知道我做错了什么。。。我应该能够为数组分配任何内容,因为它是一个对象数组,所以怎么会有类型不匹配呢?

您已经发现了

是一个
字符串[]
,它已被安全地强制转换为
对象[]
。即使转换成功,数组仍然是一个
字符串[]
(因为这是
Split()
返回的),不能用作
对象[]

特别是,它仍然不能保存非
string
s的内容

相反,您可以创建一个新的
对象[]
,并用以下
字符串填充它:

object[] row = Array.ConvertAll(
    reader.ReadLine().Split(','),
    s => (object)s
);

非常感谢!你已经向我展示了一些在未来将非常有用的东西,因为我相信我会再次遇到这些!我准备在一个类似的问题上碰壁。这个答案帮助很大!谢谢你的帮助!!