没有计算数据表对象

时间:2016-03-29 05:11:00

标签: c# asp.net datatable

我想检查查询中是否有任何记录。

所以我尝试的是

 DataTable dtmkeylength = new DataTable("select count(lease_no) from XXACL_PROSPECTIVE_DATA_SAVE where mkey = " + Request.QueryString["userid"].ToString() + "");

        if (dtmkeylength.Rows.Count > 0)
        {
            HidMode.Value = "M";
            HidMKey.Value = dtmkeylength.Rows[0]["Mkey"].ToString();
        }

下面的数据表对象在数据库中有2条记录,但它仍然没有进入 IF 条件。

为什么?

5 个答案:

答案 0 :(得分:2)

在您的查询中

添加“as NNN”:

...select count(lease_no) as result...

所以你可以通过名字来引用它。

然后,在查询时,您可以输入:

dtmkeylength.Rows[0]["result"]

我希望能为你解决这个问题:)

修改

var userId = Request.QueryString["UserId"];

if(string.IsNullOrEmpty(userId)){
  throw new Exception("No UserID = no fun!");
}

DataTable dtmkeylength = new DataTable("select count(lease_no) from XXACL_PROSPECTIVE_DATA_SAVE where mkey =" + Request.QueryString["userid"].ToString() + "");

        if (dtmkeylength.Rows.Count > 0)
        {
            HidMode.Value = "M";
            HidMKey.Value = dtmkeylength.Rows[0][0].ToString();
        }

答案 1 :(得分:1)

我认为更好的方法是使用ExecuteScalar,因为您只使用计数。

using (SqlConnection conn = new SqlConnection(connString))
{
    String sql = "select count(lease_no) from XXACL_PROSPECTIVE_DATA_SAVE where mkey = @mkey";
    SqlCommand cmd = new SqlCommand(sql, conn);
    cmd.Parameters.Add("@mkey", (int) Request.QueryString["userid"]);

    try
    {
        conn.Open();
        int rowCount = (int) cmd.ExecuteScalar();

        if (rowCount  > 0)
        {
            HidMode.Value = "M";
            HidMKey.Value = dtmkeylength.Rows[0]["Mkey"].ToString();
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}

另请注意参数化查询 - @mkey作为参数提供,并且未使用字符串连接(这可能导致Sql注入)

答案 2 :(得分:1)

试试这个:

static void Main(string[] args)
        {
            string param = "VINET";//your param here
            string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
            using(SqlConnection  conn = new SqlConnection(conStr))
            {
                conn.Open();
                //modify your command on below line
                SqlCommand cmd = new SqlCommand("select count(OrderId) from Orders where CustomerID='" + param + "'");
                cmd.Connection = conn;
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                conn.Close();
                if(ds.Tables[0].Rows.Count > 0)
                {
                    //do other staff
                }
            }
        }   

*请按照注释行

修改
下面是填充数据表的方式的调试屏幕截图。如您所见,dt正在检测命令作为表名而不是命令 enter image description here

答案 3 :(得分:0)

即使行数为零,也会始终返回行。

int number = dtmkeylength.Rows[0].Field<int>(0);

if (number > 0)
{
...
}

答案 4 :(得分:0)

System.Data.DataTable dtmkeylength = new System.Data.DataTable(); 没有构造函数接受select语句,这就是为什么它是空的,没有选择任何东西!

可用的构造函数重载是以下三种:

System.Data.DataTable dtmkeylength = new System.Data.DataTable("TableName);

或者

System.Data.DataTable dtmkeylength = new System.Data.DataTable("TableName", "tableNameSpace");

或者

DataTable

检查thisthis如何使用<?php $FavPeople = array ( 'Sofia', 'Love of my life', 'Jesus', 'Savior of my soul', 'Austin', 'Favorite Nephew' ); function randomPerson() { global $FavPeople ; $total = count($FavPeople); $ranNumber = rand(0, $total-1); echo $FavPeople[$ranNumber] . '</br>'; } randomPerson(); ?>

的示例