Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.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# 即使在检查查询是否正确后,我的表适配器仍返回null_C#_Visual Studio_Model View Controller_Dataset - Fatal编程技术网

C# 即使在检查查询是否正确后,我的表适配器仍返回null

C# 即使在检查查询是否正确后,我的表适配器仍返回null,c#,visual-studio,model-view-controller,dataset,C#,Visual Studio,Model View Controller,Dataset,我使用一个创建的查询从一个键返回一行值,我已经检查了查询执行器,但是当运行程序时,它返回null。请帮帮我,我开始失去理智了 我尝试过调试,但问题来自fill数据查询,它返回null // Add ref. to the Dataset ViewMedia viewMediaSetInstance = new ViewMedia(); // Create an empty Table ViewMedia.ViewMediaDataT

我使用一个创建的查询从一个键返回一行值,我已经检查了查询执行器,但是当运行程序时,它返回null。请帮帮我,我开始失去理智了

我尝试过调试,但问题来自fill数据查询,它返回null

        // Add ref. to the Dataset
        ViewMedia viewMediaSetInstance = new ViewMedia();
        // Create an empty Table
        ViewMedia.ViewMediaDataTable viewMediaTable = new ViewMedia.ViewMediaDataTable();
        // Create the Adapter we are going to use to populate the Table
        Model.ViewMediaTableAdapters.ViewMediaTableAdapter MediaTableAdapter = new Model.ViewMediaTableAdapters.ViewMediaTableAdapter();

        //Use query
        MediaTableAdapter.findByPublishYear(viewMediaTable, publishYear);

        //It doesn't find the row
        //I don't seem to find a solution even when the query returns the values in the executer.
        //viewMediaTable.Rows.Count == null so doesn't get inside
        if (viewMediaTable.Rows.Count > 0)
        {
            DataRow selectedUser = viewMediaTable.Rows[0];
            media = new Media(Int32.Parse(selectedUser["MediaID"].ToString()), selectedUser["Title"].ToString(), Int32.Parse(selectedUser["PublishYear"].ToString()));
            return media;
        }
        else
        {
            return null;
        }

我希望返回要在书本显示中显示的数据行。

编辑:发现错误,我应该从查询中获取所有列(使用*),然后在代码中获取我需要的内容,否则返回空值。

这是不可靠的;对于所有内容,您都有具有正确类型属性的强类型数据行,但您将它们视为具有字符串列名的通用数据行,其中包含您正在处理和解析的对象。它从未打算以这种方式使用

您的代码应该更像这样:

    ViewMedia viewMediaSetInstance = new ViewMedia();
    ViewMedia.ViewMediaDataTable viewMediaTable = new ViewMedia.ViewMediaDataTable();
    Model.ViewMediaTableAdapters.ViewMediaTableAdapter MediaTableAdapter = new Model.ViewMediaTableAdapters.ViewMediaTableAdapter();

    MediaTableAdapter.findByPublishYear(viewMediaTable, publishYear);

    //don't need to access the .Rows property to get the count
    if (viewMediaTable.Count > 0)
    {
        //don't need to access the .Rows property to get the data
        ViewMediaRow selectedUser = viewMediaTable[0];
        media = new Media(selectedUser.MediaID, selectedUser.Title, selectedUser.PublishYear);
        return media;
    }
    else
    {
        return null;
    }
我也不同意您在代码注释中的断言,
datatable.Rows.Count
为空。数据表的
.Count
属性是一个整数,无论使用的是强类型还是弱类型的数据表;它永远不能为空

如果启用了创建返回数据表的方法,则不需要创建数据集。通常,这些方法被称为
GetDataByXXX
,而fill方法被称为
FillByXXX
(您已经调用了fill方法findByPublishYear)。它在TA向导的以下屏幕上配置:

图片由nullskull.com提供

这可以将代码简化为以下内容(添加对Model.ViewMediaTableAdapters的引用):

这里最关键的是,不要通过
.Rows
集合(
viewmediatatable.Rows[0]
)访问第一行,因为这将返回一个基本类型
DataRow
。通过强类型数据表(
viewMediaDataTable[0]
)上的默认属性索引器直接访问它,因为这将返回ViewMediaRow类型的对象,而ViewMediaRow类型的对象又为datatable中的所有列提供了正确命名的类型化属性

viewMediaDataTable[0].PublishYear; //ViewMediaRow is a strongly typed class that has this property as an int
viewMediaDataTable.Rows[0].PublishYear; //"DataRow does not contain a definition for 'PublishYear'"

现在,如果行中的任何数据项为null,并且null时列的设置为“throw exception”,那么您将看到类型为的异常-当您有一些列的类型不能为null(例如int),但由于数据库查询没有为该行返回值而为null时,就会发生这种情况。您的代码之所以会遇到这个问题,是因为som int列(例如)是DBNull.Value,而当您尝试访问一个不是DBNull的int列时,datarow唯一要做的就是抛出一个异常。通过在查询中指定
SELECT*
,可以使查询返回该列的值;通过不选择列,datatable将始终将其视为null。如果数据库中的数据值也为null,则仍然会遇到strongtypingexception。在这些情况下,请使用
IsXXXNull()
方法确定属性是否为null,然后再尝试访问它(导致strongtypingexception)

非主题注释为什么要使用
Int32.Parse(selectedUser[“MediaID”].ToString()
而不是
(int)selectedUser[“MediaID”]
viewMediaDataTable[0].PublishYear; //ViewMediaRow is a strongly typed class that has this property as an int
viewMediaDataTable.Rows[0].PublishYear; //"DataRow does not contain a definition for 'PublishYear'"