Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Mobile Development
  3. Mobile
  4. Writing SQL statement in C#

Writing SQL statement in C#

Scheduled Pinned Locked Moved Mobile
csharpdatabasevisual-studiotutorialquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Deepali Khalkar
    wrote on last edited by
    #1

    I am developing an application on Win mobile 5.0 emulator in C# using Visual studio 2008. Can anyone let me know how to write an SQL statement in C# which returns the row count? Is the parameter required to store the count value? I will be greatefull if someone helps with the code snippet for the same. I have tried the following code for the select statement(the select stmt does not return the row count value) and is working fine. String uname_in='abc'; SqlCeConnection conn; SqlCeCommand comm; SqlCeDataAdapter adap = new SqlCeDataAdapter(); String connstring = @"Data Source=Program Files\signup\signupdb.sdf"; conn = new SqlCeConnection(connstring); conn.Open(); comm = new SqlCeCommand(); comm.Connection = conn; comm.CommandText = "select * from signuptbl where username =@uname"; SqlCeParameter para = new SqlCeParameter(); para.ParameterName = "@uname"; para.Value = uname_in; comm.Parameters.Add(para); adap.SelectCommand = comm; System.Data.DataTable dt = new DataTable(); ap.Fill(dt); psw1 = dt.Rows[0][1].ToString(); What changes should be done in the above code for writing a select stmt the return the number of rows selected? Thanks in advance. :)

    P Z 2 Replies Last reply
    0
    • D Deepali Khalkar

      I am developing an application on Win mobile 5.0 emulator in C# using Visual studio 2008. Can anyone let me know how to write an SQL statement in C# which returns the row count? Is the parameter required to store the count value? I will be greatefull if someone helps with the code snippet for the same. I have tried the following code for the select statement(the select stmt does not return the row count value) and is working fine. String uname_in='abc'; SqlCeConnection conn; SqlCeCommand comm; SqlCeDataAdapter adap = new SqlCeDataAdapter(); String connstring = @"Data Source=Program Files\signup\signupdb.sdf"; conn = new SqlCeConnection(connstring); conn.Open(); comm = new SqlCeCommand(); comm.Connection = conn; comm.CommandText = "select * from signuptbl where username =@uname"; SqlCeParameter para = new SqlCeParameter(); para.ParameterName = "@uname"; para.Value = uname_in; comm.Parameters.Add(para); adap.SelectCommand = comm; System.Data.DataTable dt = new DataTable(); ap.Fill(dt); psw1 = dt.Rows[0][1].ToString(); What changes should be done in the above code for writing a select stmt the return the number of rows selected? Thanks in advance. :)

      P Offline
      P Offline
      PavanPareta
      wrote on last edited by
      #2

      Hi Deepali Khalkar, In SQL Query you can use count() mehtod for getting selected rows count. see the below example.

      private int GetRowCount()
      {
      int intCount = 0;
      SqlCeConnection objCon = new SqlCeConnection("ConnectionString");
      objCon.Open();
      SqlCeCommand objCmd = new SqlCeCommand("SELECT COUNT(*) AS UserCount FROM Users", objCon);
      intCount = System.Convert.ToInt32( objCmd.ExecuteScalar().ToString());
      objCon.Close();
      objCmd.Dispose();

              return intCount;
      

      }

      hope this may be help... :thumbsup:

      Pavan Pareta

      1 Reply Last reply
      0
      • D Deepali Khalkar

        I am developing an application on Win mobile 5.0 emulator in C# using Visual studio 2008. Can anyone let me know how to write an SQL statement in C# which returns the row count? Is the parameter required to store the count value? I will be greatefull if someone helps with the code snippet for the same. I have tried the following code for the select statement(the select stmt does not return the row count value) and is working fine. String uname_in='abc'; SqlCeConnection conn; SqlCeCommand comm; SqlCeDataAdapter adap = new SqlCeDataAdapter(); String connstring = @"Data Source=Program Files\signup\signupdb.sdf"; conn = new SqlCeConnection(connstring); conn.Open(); comm = new SqlCeCommand(); comm.Connection = conn; comm.CommandText = "select * from signuptbl where username =@uname"; SqlCeParameter para = new SqlCeParameter(); para.ParameterName = "@uname"; para.Value = uname_in; comm.Parameters.Add(para); adap.SelectCommand = comm; System.Data.DataTable dt = new DataTable(); ap.Fill(dt); psw1 = dt.Rows[0][1].ToString(); What changes should be done in the above code for writing a select stmt the return the number of rows selected? Thanks in advance. :)

        Z Offline
        Z Offline
        ziwez0
        wrote on last edited by
        #3

        FYI "SELECT ROWS" should be faster that "Select Count(*)"

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups