0

我有这样一段代码,执行后我在我的数据库表中得到 N 行,并且从方法返回 N-1 行数据。无法理解我做错了什么并找到类似问题的任何示例。我是因为 sql 查询而丢失数据,还是我在代码中犯了错误?请帮忙。

    private String sqlCommandSample = "insert into [dbo].[SomeTable] " +
                         "(Title, Descript) " +
                         "output inserted.Title " +
                         "select Item.value('title[1]', 'nvarchar(max)'), Item.value('description[1]', 'nvarchar(max)') " +
                         "from @Xml.nodes('nodes/node') as Result(Item) " +
                         "where not exists (select * from [dbo].[SomeTable] " +
                         "where ([Title] = Item.value('Title[1]', 'nvarchar(200)')))";

    public async Task<List<String>> FillTableAsync(String sqlCommandString, GetArticleLink getArticleLink)
    {
        using (var sqlConnection = new SqlConnection(ConnectionString))
        {
            await sqlConnection.OpenAsync();

            using (var sqlCommand = new SqlCommand(sqlCommandString, sqlConnection))
            {
                using (var sqlReader = await sqlCommand.ExecuteReaderAsync())
                {
                    var listOfLinks = new List<String>();

                    if (await sqlReader.ReadAsync())
                    {
                        while (await sqlReader.ReadAsync())
                        {
                            listOfLinks.Add(await GetLink(sqlReader));
                        }
                    }
                    return listOfLinks;
                }
            }
        }
    }

    private async Task<String> GetLink(DbDataReader reader)
    {
        return await reader.GetFieldValueAsync<String>(0);
    }
4

1 回答 1

4
 if (await sqlReader.ReadAsync())
 {
     while (await sqlReader.ReadAsync())
     ...

在这里你刚刚输了一行。while够了,不需要if

于 2013-09-14T11:26:42.537 回答