【问题标题】:'System.Data.DataRowCollection' does not contain definition for "Length"“System.Data.DataRowCollection”不包含“长度”的定义
【发布时间】:2011-10-17 15:13:34
【问题描述】:

我是初学者,有一个基本问题。我最近将此代码添加到我的方法中

    if (dt.Rows.Length > 0)

但是,我返回错误:“System.Data.DataRowCollection”不包含“Length”的定义,并且找不到接受“System.Data.DataRowCollection”类型的第一个参数的扩展方法“Length”(您是否缺少 using 指令或程序集引用?),我不是说要为我编写此代码(除非您想 :),)但是如果有人能指出我正确的方向,那对您来说将是很棒的好运.这是一些帮助的代码。

     using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Configuration;

public class Database
{
    private string serverPCICUSTOM, serverPCI;
    private string ConnectionStringPCICUSTOM, ConnectionStringPCI;
    private SqlConnection connectionPCICUSTOM, connectionPCI;
    private string trackingNumber;
    private string soptype;
    private string orderNumber;

         public bool UpdateOrderToShipped(string order)
    {
        orderNumber = order;
        string batch = ConfigurationManager.AppSettings["SuccessfulOrderBatch"];
        string statement = "UPDATE SOP10100 SET BACHNUMB = '"+ batch +"' WHERE SOPNUMBE = @SOPNUMBE";
        SqlCommand comm = new SqlCommand(statement, connectionPCI);
        comm.Parameters.Add("SOPNUMBE", orderNumber);
        try
        {
            comm.Connection.Open();
            comm.ExecuteNonQuery();
            comm.Connection.Close();
        }
        catch(Exception e)
        {

            comm.Connection.Close();
            KaplanFTP.errorMsg = "Database error: " + e.Message;
        }

        statement = "SELECT SOPTYPE FROM SOP10100 WHERE SOPNUMBE = @SOPNUMBE";
        comm.CommandText = statement;
        SqlDataAdapter da = new SqlDataAdapter(comm);
        DataTable dt = new DataTable();
        da.Fill(dt);
        if (dt.Rows.Length > 0)      //error here
        {
        comm.Connection.Open();
            soptype = dt.Rows[0]["SOPTYPE"].ToString();
        }
        else
        {
        }

            return true;
    }

【问题讨论】:

  • 哇,这么简单!!谢谢大家的帮助!
  • 不客气,你也可以在我的答案上打勾并指出我的答案:)

标签: c# .net asp.net visual-studio-2010


【解决方案1】:

检查一下:

使用

if (dt.Rows.count> 0)

而不是

if (dt.Rows.length> 0)

希望我的回答能帮助您解决问题。

【讨论】:

    【解决方案2】:
    【解决方案3】:

    你应该试试:

    if (dt.Rows.Count > 0)
    

    【讨论】:

      【解决方案4】:

      MSDN 是您最大的盟友。

      DataRowCollection.Count

      【讨论】:

        【解决方案5】:

        使用Count,而不是Length

        if (dt.Rows.Count > 0)
        

        一般来说,每当你有一个集合时,都会调用属性Count来获取项目的数量。对于许多集合类型都是如此,包括 DataRowCollection、List、Dictionary 或任何其他实现 ICollection<T> 接口(或非泛型等效项)的集合类型。那是因为Count 属性直接来自接口。

        数组是个例外。数组中的项数可以通过Length获取。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-01-27
          相关资源
          最近更新 更多