Results 1 to 3 of 3

Thread: a simple database query

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    183

    a simple database query

    Just looking for a simple example of how to query a database and determine if a value is in it. I want to return true if it is there and false if it is not.... I mucked around with one example here is what I have so far.... any ideas??



    /*Check to see if the company is in the database*/
    private bool CheckCompany(string CompanyName){
    string strConnection = "server=localhost; uid=sa; pwd=; database=Company";

    string strCommand = "SELECT * from CompanyNames WHERE name = 'CompanyName' ";
    SqlDataAdapter dataAdapter = new SqlDataAdapter(strCommand,strConnection);

    DataSet dataSet = new DataSet();
    dataAdapter.Fill(dataSet,"Companies");
    SqlCommandBuilder bldr = new SqlCommandBuilder(dataAdapter);
    DataTable dataTable = dataSet.Tables[0];

    }

  2. #2
    Hyperactive Member SoftwareMaker's Avatar
    Join Date
    Mar 2001
    Location
    Elbonia with Dilbert and Wally
    Posts
    322
    After that, check for the no. of rows in your datatable. If more than 0, your value exists

    if (dataTable.Rows.Count > 0)
    {
    return true;
    }
    William T
    Software Architect / Chief Software Developer
    Softwaremaker.Net Pte Ltd
    http://www.Softwaremaker.net

    *** Things are always the darkest before they go pitch black ***

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    183
    perfect! thanks!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width