none
Finding Record in a dataset

    Question

  • I have an excel sheet which has more than 200 rows of data. There is one column call "Station ID". I need to check all these 200 station Ids across a table in database. If the station ID is avaible, I should remove from the excel sheet.

    To do that I would not like to check each Station Id in the database with Select statement. If I do, I have hit the databse 200 times. Instead of this, can I take all the records from the table in a dataset and then searching the station Id within the dataset?

    This is my code

    String Sql_Query = "Select * From Stations ";
    SqlDataAdapter DtStation = new SqlDataAdapter (Sql_Query, mySqlCon);
    DataSet ds = new DataSet();
    DtStation .Fill(DS, "Stations");

    mySqlCon.Close();

    Now, can I look specific stationID in this Dataset?

    Please give me some help


    Dreaming a world without any war in anywhere
    Friday, February 25, 2011 12:38 AM

Answers

  • Hello Rajeeshun,

    Try the following code:

    public DataRow[] GetResults(columnName, columnValue)
    {
    	foreach(DataTable dt in ds.Tables)
    	{
    		if(dt.Rows != null && dt.Rows.Count != 0)
    			return dt.Select(string.Format("{0} like '%{1}%'", columnName, columnValue));
    	}
    
    	return null;
    }
    


    Cornel Croitoriu - Senior Software Developer & Entrepreneur

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    CWS SoftwareBiz-Forward.comCroitoriu.NET

    • Proposed as answer by Cornel Croitoriu Friday, February 25, 2011 8:24 AM
    • Marked as answer by Rajeeshun Monday, February 28, 2011 2:58 PM
    Friday, February 25, 2011 8:23 AM

All replies

  • Hello Rajeeshun,

    Try the following code:

    public DataRow[] GetResults(columnName, columnValue)
    {
    	foreach(DataTable dt in ds.Tables)
    	{
    		if(dt.Rows != null && dt.Rows.Count != 0)
    			return dt.Select(string.Format("{0} like '%{1}%'", columnName, columnValue));
    	}
    
    	return null;
    }
    


    Cornel Croitoriu - Senior Software Developer & Entrepreneur

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    CWS SoftwareBiz-Forward.comCroitoriu.NET

    • Proposed as answer by Cornel Croitoriu Friday, February 25, 2011 8:24 AM
    • Marked as answer by Rajeeshun Monday, February 28, 2011 2:58 PM
    Friday, February 25, 2011 8:23 AM
  • Hello Rajeeshun,

    Which version of .net are you using . If you are using version 3.5 and above you can use LINQ queries to get the desired result. Otherwise you can go for normal datatable select query.


    ran
    Friday, February 25, 2011 8:33 AM
  • Hello Rajeeshun,

    Try the following code:

     

    public DataRow[] GetResults(columnName, columnValue)
    
    {
    
    	foreach(DataTable dt in ds.Tables)
    
    	{
    
    		if(dt.Rows != null && dt.Rows.Count != 0)
    
    			return dt.Select(string.Format("{0} like '%{1}%'", columnName, columnValue));
    
    	}
    
    
    
    	return null;
    
    }
    
    

     


    Cornel Croitoriu - Senior Software Developer & Entrepreneur

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    CWS SoftwareBiz-Forward.comCroitoriu.NET


    Thanks Conrnel for the code help

    I can understand the most of the part, but having difficulties to uderstand "("{0} like '%{1}%'", " line. If you have time, can you please explain?

     


    Dreaming a world without any war in anywhere
    Friday, February 25, 2011 8:47 PM
  • Hey Rajeesh,

    String.format method accepts parameters and the parameters are "("{0} like '%{1}%'"

    {0} = CloumnName

    {1}= ColumnValue

    The columnname and coloumnvalue field values are automatically inserted to the respective fields.

    Raneesh


    ran
    Saturday, February 26, 2011 8:46 AM
  • Thanks Raneesh for the explanation
    Dreaming a world without any war in anywhere
    Monday, February 28, 2011 2:57 PM