Results 1 to 4 of 4

Thread: Database Connection using VS.NET????

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2003
    Location
    michigan
    Posts
    70

    Database Connection using VS.NET????

    HI,

    I am trying to connect to a database using Visual Studio .NET. I have created the SQlconnection string and when I am trying to create the DataAdapter using SQLDATAADAPTER tool it aks me to specify the query now my question is If I want to specify a condition in that like " suppose I want to select details from a table where the id of the table is given in a textbox". The query wud be like 'select * from table where tblID = txtId.Text' It doesn;t work if I specify like this.....Can anyone tell me how to specify this.....ur help will be appreciated.

  2. #2
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618
    The simplest way is

    select * from table where tblID = '" & txtId.Text & "'"
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

  3. #3
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    while this approach will work most of the time, I would consider using Parmeters when working with working with Commands

    VB Code:
    1. Private conn As New SqlConnection("integrated security=SSPI;data source=local;initial catalog=Customers")
    2.     Private cmdSelect As New SqlCommand("SELECT * FROM Employees WHERE EmployeeID=@EID", conn)
    3.     Private daEmployees As New SqlDataAdapter(cmdSelect)
    4.     Private dtEmployees As New DataTable("Employees")
    5.  
    6.     Private Sub btnGet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGet.Click
    7.         Dim parm As New SqlParameter("@EID", SqlDbType.Int, 4, "EmployeeID")
    8.         parm.Value = Me.txtLoop.Text
    9.         Me.cmdSelect.Parameters.Add(parm)
    10.         Me.daEmployees.Fill(dtEmployees)
    11.     End Sub
    This will ensure your value always get's passed into the server with your select command.

  4. #4
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618
    Absolutely paramaters are better. I was just showing him the simpliest way to do it without trying to confuse him.
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

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