本文介绍了我想在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();
            }

推荐答案

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


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数据库中检索表中的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 17:47