-
Jul 6th, 2020, 10:04 AM
#1
Thread Starter
Enjoy the moment
[RESOLVED] VISUAL STUDIO 2017 is this the right way to use 2 diffrent methods search?
Code:
Dim SqlQuery As String = "Select FullName,Cellular From Customers Where FullName LIKE '%' + @FullName + '%'
Or Cellular Like '%' + @Cellular + '%' Order By FullName"
Using cmd As New SqlCommand(SqlQuery, Cnn)
Cnn.Open()
cmd.Parameters.AddWithValue("@FullName", TxtNameCustomer.Text)
cmd.Parameters.AddWithValue("@Cellular", TxtNameCustomer.Text)
Using adapter As New SqlDataAdapter(cmd)
DataGridCustomers.Visible = True
adapter.Fill(tble)
End Using
Cnn.Close()
End Using
tnx
salsa
-
Jul 6th, 2020, 10:08 AM
#2
Re: VISUAL STUDIO 2017 is this the right way to use 2 diffrent methods search?
Looks reasonable. You could also have the Connection in a Using block, which would remove the need to call close. You don't need the call to conn.Open, either, since you are using a dataadapter. That will open the connection if necessary, and will leave the connection in the same state it found it.
Of course, that means that it doesn't hurt to open the connection yourself, since it will have to be opened by somebody. You could do it, or you could let the dataadapter handle it. I kind of prefer the way you are doing it, myself, because things like datareaders won't open the connection themselves, so I'm always forgetting that step. The close is more problematic, because an error at the right place could leave the connection open...ish. That may not be an issue, depending on the code around the code you showed. A Using block for the Connection object would take care of that, as would some other constructs.
My usual boring signature: Nothing
-
Jul 6th, 2020, 10:17 AM
#3
Thread Starter
Enjoy the moment
Re: VISUAL STUDIO 2017 is this the right way to use 2 diffrent methods search?
Using block for the Connection object would take care of that, as would some other constructs.
what do you mean?
can you give me an example?
-
Jul 6th, 2020, 10:20 AM
#4
Thread Starter
Enjoy the moment
Re: VISUAL STUDIO 2017 is this the right way to use 2 diffrent methods search?
-
Jul 6th, 2020, 10:23 AM
#5
Thread Starter
Enjoy the moment
Re: VISUAL STUDIO 2017 is this the right way to use 2 diffrent methods search?
you mean like this?
Code:
Dim SqlQuery As String = "Select FullName,Cellular From Customers Where FullName LIKE '%' + @FullName + '%'
Or Cellular Like '%' + @Cellular + '%' Order By FullName"
Using cmd As New SqlCommand(SqlQuery, Cnn)
cmd.Parameters.AddWithValue("@FullName", TxtNameCustomer.Text)
cmd.Parameters.AddWithValue("@Cellular", TxtNameCustomer.Text)
Using adapter As New SqlDataAdapter(cmd)
DataGridCustomers.Visible = True
adapter.Fill(tble)
End Using
End Using
-
Jul 6th, 2020, 10:54 AM
#6
Re: VISUAL STUDIO 2017 is this the right way to use 2 diffrent methods search?
Originally Posted by salsa31
can you give me an example?
You've already got an example in your own code. You're creating the command object with a Using statement. You should be doing that with ALL disposable objects, including the connection. There's no need to explicitly close the connection because that happens when it is disposed. In fact, there's no need to open it either, because Fill will open and close it automatically. There's also no need to create the command object because the data adapter will do that. In fact, the data adapter will create the connection too.
vb.net Code:
Using adapter As New SqlDataAdapter(query, connectionString)
adapter.Fill(table)
'...
End Using
-
Jul 6th, 2020, 11:44 AM
#7
Re: VISUAL STUDIO 2017 is this the right way to use 2 diffrent methods search?
Originally Posted by salsa31
Code:
Dim SqlQuery As String = "Select FullName,Cellular From Customers Where FullName LIKE '%' + @FullName + '%'
Or Cellular Like '%' + @Cellular + '%' Order By FullName"
Using cmd As New SqlCommand(SqlQuery, Cnn)
Cnn.Open()
cmd.Parameters.AddWithValue("@FullName", TxtNameCustomer.Text)
cmd.Parameters.AddWithValue("@Cellular", TxtNameCustomer.Text)
Using adapter As New SqlDataAdapter(cmd)
DataGridCustomers.Visible = True
adapter.Fill(tble)
End Using
Cnn.Close()
End Using
tnx
salsa
I'm pretty sure your where clause is going to ignore any indexes you might have on those columns. Do you really need it that way?
Please remember next time...elections matter!
-
Jul 6th, 2020, 12:15 PM
#8
Re: VISUAL STUDIO 2017 is this the right way to use 2 diffrent methods search?
I was thinking that, too, but when it comes to using LIKE, do you have much of a choice?
I'd be inclined to get rid of that first wildcard, though. For example, as it stands, any name with an 'A' in it would be returned by typing 'A', which seems overly broad. Names that start with 'A' makes sense, but getting every name that has an 'A' somewhere in it just seems like there will be a whole lot of chaff.
My usual boring signature: Nothing
-
Jul 6th, 2020, 12:18 PM
#9
Thread Starter
Enjoy the moment
Re: VISUAL STUDIO 2017 is this the right way to use 2 diffrent methods search?
thank you
i fixed
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
|