我想在sql server数据库中检索表中的行数 [英] i want to retrieve number of rows in table in sql server database

查看:108
本文介绍了我想在sql server数据库中检索表中的行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我输入了此代码,但没有用

i put this code but it did not work

string num = "select count(*) from GalleryFactory where GalleryCatName='" + lblLabel1.Text + ".";
            SqlCommand str5 = new SqlCommand(num, con);
            
            string s4 = str5.ExecuteReader().ToString();
            int nums = Convert.ToInt32(s4);
            if (nums >= 1)
            {
                string str2 = "delete from GalleryFactory where GalleryCatName='" + lblLabel1.Text + "'";
                SqlCommand str12 = new SqlCommand(str2, con);
                string s2 = str12.ExecuteNonQuery().ToString();
                con.Close();
            }

推荐答案

尝试执行以下操作以获取行数:

Try this to get the number of rows:

// incluse these namespaces
using System.Data;
using System.Data.SqlClient;

string select = "select* from GalleryFactory";
SqlDataAdapter da = new SqlDataAdapter(select, con);
DataTable dt = new DataTable();
da.fill(dt);
 
int count= dt.Rows.Count;

if(count >=1 )
{
string str2 = "delete from GalleryFactory where GalleryCatName='" + lblLabel1.Text + "'";
                SqlCommand str12 = new SqlCommand(str2, con);
                string s2 = str12.ExecuteNonQuery().ToString();
                con.Close();
}




编写如下代码.

Hi,

Write your code as below.

string num = "select count(*) as RowsCount from GalleryFactory where GalleryCatName='" + lblLabel1.Text + ".";
            SqlCommand str5 = new SqlCommand(num, con);
            SqlDataAdapter da = new SqlDataAdapter(str5);
            DataSet ds = new DataSet();
            da.Fill(ds);
            int nums=0;
            if(ds.Table[0].Rows.Count>0)
            {
               nums = Convert.ToInt32(ds.Table[0].Rows[0]["RowsCount"].ToString()); 
            }
            
            if (nums >= 1)
            {
                //Below query will delete all the rows matching with GalleryCatName
                //If you want to delete only duplicate row the update delete query
                string str2 = "delete from GalleryFactory where GalleryCatName='" + lblLabel1.Text + "'";
                SqlCommand str12 = new SqlCommand(str2, con);
                string s2 = str12.ExecuteNonQuery().ToString();
                con.Close();
            }


这篇关于我想在sql server数据库中检索表中的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆