3
Answers

how to get the multiple rows from db using c#

priya santhi

priya santhi

10y
2k
1
hiii..
       i want to get multiple rows from database...
         SqlCommand cmd = new SqlCommand("SELECT * FROM dateshort WHERE from_date BETWEEN '" + from + "' AND '" + to + "' ", mycon);
         using this query  i want to fetch the in between dates ...
but i don't konw how to implement in c#...
   
Answers (3)
0
Ahmed JS

Ahmed JS

NA 103 382 10y
Hi,
   use this code

DateTime from =Convert.ToDateTime(from);
  DateTime to =Convert.ToDateTime(to);
  SqlCommand cmd = new SqlCommand("SELECT * FROM dateshort WHERE from_date BETWEEN '" + from.ToString("dd/MMM/yyyy") + "' AND '" + to.ToString("dd/MMM/yyyy") + "' ", mycon);



Please Mark This answer if it is helpful to you.
0
Vikram Agrawal

Vikram Agrawal

NA 592 5.9k 10y
Hi priya,
If at all you want to to retrieve data from database into your application you can do it as following:

SqlConnection con=new SqlConnection("CONNECTIONSTRING");
SqlCommand cmd=new SqlCommand("Select * From myTable Where DateColumn Between '" + fromDate + "' AND '" + toDate + "'",con);
con.Open();
SqlDataReader reader=cmd.ExecuteReader();

Now u will get all filtered record in reader object and you can use this as per your need.

Thanks.

Please Mark This answer if it is helpful to you.
0
Ranjit Powar

Ranjit Powar

NA 8.1k 496.5k 10y
try this

        SqlDataAdapter da=new  SqlDataAdapter("SELECT * FROM dateshort WHERE from_date BETWEEN '" + from + "' AND '" + to + "' ", mycon);
        DataTable dt=new DataTable();
        da.Fill(dt);
        
        Now you can get all selected rows in data table.