How do I retrieve the result of an ADO.NET SqlCommand?

前端 未结 3 1554
说谎
说谎 2020-12-11 07:03

Ok either I\'m really tired or really thick at the moment, but I can\'t seem to find the answer for this

I\'m using ASP.NET and I want to find the amount of rows in

3条回答
  •  暖寄归人
    2020-12-11 07:29

    You need to open the connection This might work :

    SqlConnection sqlConnection1 = new SqlConnection("Your Connection String");
    SqlCommand cmd = new SqlCommand();
    SqlDataReader reader;
    
    cmd.CommandText = "select count(*) from topics";
    cmd.CommandType = CommandType.Text;
    cmd.Connection = sqlConnection;
    sqlConnection1.Open();
    
    reader = cmd.ExecuteReader();
    // Data is accessible through the DataReader object here.
    
    sqlConnection1.Close();
    

    Similar Question: C# 'select count' sql command incorrectly returns zero rows from sql server

提交回复
热议问题