You must Sign In to post a response.
  • Category: .NET

    search method : to find whether a specified name is present in the database or not

    The forum thread has not been reviewed by Editors yet. Readers are advised to use their best judgement before accessing this thread.
    This thread will be reviewed shortly.
    If you think this thread contain inappropriate content, please report to webmaster.
    i have a database of books in a library.

    if a user wants to know whether a particular book is availabe or not ,

    the user will specify the name of the book. so that the search method should search for the specified book n return the result.

    please suggest solution for this in C#.

    thanx in advance,
    shruthi
  • #365
    Hi Shruthi,
    Yes you can find the books(column) from the database....

    make your design like user to enter the book name...and then in your code there is a need for query that s all for the solution...here i will give some hints to use query to search in the database...

    Your select query..

    SELECT * FROM LIBRARY WHERE BOOKNAME LIKE 'HARRYPOTTER%'

    here you will get your results and display...if no results just display as no books found like that...

    here some tips are...

    user may know the half of the book name so if somebody enters potter that also we should find..

    so in your code make the query as common as

    SELECT * FROM LIBRARY WHERE BOOKNAME LIKE '%HARRYPOTTER%'

    both sides the % symbol will give you the pure result...

    here i have given query for sqlserver make sure about your database...

    then if you have your column values in caps letter make sure lcase(bookname)

    i hope u got my points...

    feel free to ask me if still you have any doubts

    regards,
    siva

  • #385
    Shruthi,

    This might help. It is C# code:

    public DataSet SearchForBook(string sBook)
    {
    SqlConnection conn = new SqlConnection("server=server;database=db;user id=uid;password=pwd;");
    SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Books WHERE Books LIKE '%'" + sBook + "%'", conn);
    DataSet ds = new DataSet();
    da.Fill(ds, "Books");

    return ds;
    }

    // Call it like this.
    public void fooDS()
    {
    DataSet ds = new DataSet();
    ds = SearchForBook("Harry Potter");
    if(ds.Tables[0].Rows.Count > 0)
    {
    // found.
    }
    else
    {
    // Not found.
    }
    }

    public int SearchForBook(string sBook)
    {
    SqlConnection conn = new SqlConnection("server=server;database=db;user id=uid;password=pwd;");
    SqlCommand cmd = new SqlCommand("SELECT * FROM Books WHERE Books LIKE '%'" + sBook + "%'", conn);
    int nCount = cmd.ExecuteNonQuery();

    return nCount;
    }

    // Call it like this.
    public void fooCount()
    {
    int nCount = SearchForBook("Harry Potter");
    if(nCount > 0)
    {
    // found.
    }
    else
    {
    // Not found.
    }
    }

    Hope this helps.

    Regards,

    Ajay Singala [Synergetics].
    .NET MVP.

  • #166173
    try this

    select count(*) as a_count from librar where book_name=name

    or

    select count(*) into a_count from librar where book_name=name

    here a_count value 0 means not available.
    a_count value is there means book is available.
    value 3 means three books r there

    Thanks
    Shiva

  • #216988
    heloo shruthi..
    if you use storeprocedure then the query will be..
    select * from booktable where bookname=@bookname
    then ...in .cs file you hav 2 write..
    protected string bookname;
    public string get_bookname
    {
    get { return bookname; }
    set { bookname = value; }
    }
    public DataSet bookdetails()
    {
    cn.Open();
    DataSet ds = new DataSet();
    SqlCommand cmd = new SqlCommand("storeprocedurename", cn);
    cmd.CommandType = CommandType.StoredProcedure;
    SqlParameter prm = new SqlParameter();

    prm = cmd.Parameters.Add("@bookname", SqlDbType.varchar,1000);
    prm.Direction = ParameterDirection.Input;
    prm.Value = bookname;
    SqlDataAdapter adp = new SqlDataAdapter(cmd);
    adp.Fill(ds);
    cn.Close();
    return ds;
    }
    .............................

  • #216997
    heloo shruthi..
    if you use storeprocedure then the query will be..
    select * from booktable where bookname=@bookname
    then ...in .cs file you hav 2 write..
    protected string bookname;
    public string get_bookname
    {
    get { return bookname; }
    set { bookname = value; }
    }
    public DataSet bookdetails()
    {
    cn.Open();
    DataSet ds = new DataSet();
    SqlCommand cmd = new SqlCommand("storeprocedurename", cn);
    cmd.CommandType = CommandType.StoredProcedure;
    SqlParameter prm = new SqlParameter();

    prm = cmd.Parameters.Add("@bookname", SqlDbType.varchar,1000);
    prm.Direction = ParameterDirection.Input;
    prm.Value = bookname;
    SqlDataAdapter adp = new SqlDataAdapter(cmd);
    adp.Fill(ds);
    cn.Close();
    return ds;
    }
    .............................


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.