简体   繁体   English

没有计数数据表对象

[英]Not getting count for datatable object

I want to check if there exist any record in the query. 我想检查查询中是否存在任何记录。

So what I tried is 所以我尝试的是

 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();
        }

The below datatable object has count of 2 records into the database, but still it is not going inside IF condition. 下面的datatable对象在数据库中有2条记录,但是仍然不在IF条件内。

WHY ? 为什么呢?

in your query add the "as NNN" thing: 在查询中添加“ as NNN”:

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

so you can reference it by name. 因此您可以按名称引用它。

Then, when you query, you can type: 然后,当您查询时,可以键入:

dtmkeylength.Rows[0]["result"]

I hope that fixes it for you :) 我希望可以为您修复它:)

EDIT 编辑

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();
        }

I think a better approach is to use ExecuteScalar , since you are using only the count. 我认为更好的方法是使用ExecuteScalar ,因为您只使用了count。

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);
    }
}

Also note the parameterized query - @mkey is provided as a parameter and string concatenation is not used (this may lead to Sql Injection) 还要注意参数化查询- @mkey作为参数提供,不使用字符串连接(这可能导致Sql Injection)

Try this: 尝试这个:

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
                }
            }
        }   

*Please follow commented lines *请按照评论行

Edit 编辑
Below is a debug screen shot of your way of populating data table. 下面是您填充数据表方式的调试屏幕快照。 As you can see, dt is detecting the command as table name and NOT as command. 如您所见,dt将命令检测为表名,而不是命令检测为命令。
在此处输入图片说明

It WILL always return rows even if the row count is zero. 即使行数为零,它也将始终返回行。

try 尝试

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

if (number > 0)
{
...
}

There is no constructor for DataTable that takes a select statement, thats why it is empty, nothing selected yet! 没有使用选择语句的DataTable构造函数,这就是为什么它为空,而未选择任何东西的原因!

The available constructor overloads available are the below three: 可用的构造函数重载包括以下三个:

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

Or 要么

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

Or 要么

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

Check this and this example of how to use a DataTable 检查以及示例如何使用DataTable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM