|
-
Jun 29th, 2004, 12:28 PM
#1
Thread Starter
Lively Member
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.
-
Jun 29th, 2004, 12:41 PM
#2
Frenzied Member
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.
-
Jun 29th, 2004, 01:06 PM
#3
Hyperactive Member
while this approach will work most of the time, I would consider using Parmeters when working with working with Commands
VB Code:
Private conn As New SqlConnection("integrated security=SSPI;data source=local;initial catalog=Customers")
Private cmdSelect As New SqlCommand("SELECT * FROM Employees WHERE EmployeeID=@EID", conn)
Private daEmployees As New SqlDataAdapter(cmdSelect)
Private dtEmployees As New DataTable("Employees")
Private Sub btnGet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGet.Click
Dim parm As New SqlParameter("@EID", SqlDbType.Int, 4, "EmployeeID")
parm.Value = Me.txtLoop.Text
Me.cmdSelect.Parameters.Add(parm)
Me.daEmployees.Fill(dtEmployees)
End Sub
This will ensure your value always get's passed into the server with your select command.
-
Jun 29th, 2004, 01:30 PM
#4
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|