Writing SQL statement in C#
-
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. :)
-
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. :)
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
-
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. :)