2016-07-30 4 views
-2

C#から選択クエリを実行すると、影響を受ける行の数を調べたいと思います。データセットまたはデータテーブルにデータを挿入する必要はありません。どのように多くの行がCからの選択クエリによって影響を受けているか調べるには#

string constr = ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString; //get connection string Web.config 

SqlConnection con = new SqlConnection(constr); 

//define Select Query 
SqlCommand slct = new SqlCommand("select * From [dbo].[userdetail] where 1=1", con); 
int noRows; 

//open connection and execute query 
con.Open(); 
noRows = slct.ExecuteNonQuery(); 
con.Close(); 

//define Select Query 
SqlCommand select = new SqlCommand("select * From [dbo].[userdetail] where 1=0", con); 

//open connection and execute query 
con.Open(); 
noRows = select.ExecuteNonQuery(); 
con.Close(); 
+1

その場合、 'SELECT COUNT(*).....'を使用し、 'select.ExecuteScalar()'を使って行の数を戻す必要があります。 –

+0

mデータベース検索されたデータセットの行を数えることができます – Jonny

+0

"影響を受ける"を定義する必要があります。あなたが「選択」を意味するならば、それは上記のコメントのような結果を数えることを意味します。 – KernelMode

答えて

0

あなたの現在のクエリを使用ExecuteReader()ため、以下のようなスカラーのクエリを使用してExecuteScalar()機能に

select count(*) From [dbo].[userdetail] 

SqlCommand cmd = new SqlCommand("select count(*) From [dbo].[userdetail]", con); 

// open connection and execute query 
con.Open(); 
int noRows = (int)cmd.ExecuteScalar(); 
を使用し、それ以外の

int count = 0; 

while(rdr.Read()) 
{ 
    count++; 
} 

カウンターを保つ下記言ってループを読みながらまず第一に

関連する問題