使用.net core mvc将空值插入对象列表时出错?

问题描述 投票:0回答:1

我在将选择查询结果保存到模型类时遇到问题。

我试图将excel表中的值与本地数据库中的值进行匹配。我一直收到错误“数据是空的。这个方法或属性不能在Null值上调用”或类似的东西。我不认为我可以传入null,因为我运行SQL查询以将所有空值替换为0.我也不在乎是否将null传递给这些属性,因此要么使字段可为空,要么删除空值工作......我想?

以下是我的代码:

 //initialize spreadsheet package,
        //load excel file, create dictionary of
        //naic/industry key/value pairs
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");
        ExcelFile ef = ExcelFile.Load("C:/Data Sources/NAICS sheets/3-Digit-NAICS-Codes.xls");
        Dictionary<object, object> naics = new Dictionary<object, object>();

        //Connection string for local MSSQL database
        SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=gov_world;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");


        //Create array of client objects returned from query
        Client[] allRecords = null;
        string sql = "SELECT v_company, client_pk, v_naic_1, v_naic_2, v_naic_3, v_naic_4, v_naic_5 FROM  dbo._clients";

        //Iterate through elements of excel sheet, eventually inputting first column value
        //as key and second column value as value in dictionary
        foreach (ExcelWorksheet sheet in ef.Worksheets)
        {


            foreach (ExcelRow row in sheet.Rows)
            {
                object[] industry = new object[2];
                var count = 0;

                //populate industry array with all values of row
                foreach (ExcelCell cell in row.AllocatedCells)
                {
                    if (cell.Value != null)
                    {
                        industry[count] = cell.Value;
                    }
                    count++;
                }
                count = 0;

                //add values of first 2 cells to naics dictionary object
                naics.Add(industry[0], industry[1]);
                //Console.WriteLine(industry[0] + " | " + industry[1]);

                //reinitialize for every record
                industry[0] = 0;
                industry[1] = 0;

            }
        }


        //Set values from query to client model object, uses
        //to populate allRecords Array
        using (var command = new SqlCommand(sql, con))
        {
            con.Open();
            using (var reader = command.ExecuteReader())
            {
                var list = new List<Client>();
                while (reader.Read())
                    list.Add(new Client {
                        //Solution: write as v_company = reader[0] as string;
                        v_company = reader.GetString(0),
                        client_pk = reader.GetInt32(1),
                        v_naic_1 = reader.GetString(2),
                        v_naic_2 = reader.GetString(3),
                        v_naic_3 = reader.GetString(4),
                        v_naic_4 = reader.GetString(5),
                        v_naic_5 = reader.GetString(6)

                    });
                allRecords = list.ToArray();

            }
        }


        Console.WriteLine(allRecords[0].v_naic_1);
        Console.ReadLine();

我可以想出匹配,此时我只想“list.Add(新客户端......)工作,以便while循环完成。

c# asp.net-core ado.net sqldatareader
1个回答
1
投票

您需要为所有值检查IsDBNull

if(!reader.IsDBNull(0))
{
  employee.FirstName = sqlreader.GetString(0);
}

或者你可以用它作为

reader[0] as string

最好使用列名而不是索引值,您可以从列名获取列索引,如下所示:

int colIndex = reader.GetOrdinal(fieldname);

您可以通过阅读this问题的所有答案,找到有关如何从阅读器读取数据的更多信息以及更好的方法。

© www.soinside.com 2019 - 2024. All rights reserved.