help! working with sqlserver connection wizard in vbstudio 2010

securonic

Active member
Joined
Aug 29, 2010
Messages
34
Location
Midlands Uk
Programming Experience
Beginner
Hi, i am very new to visual basic and have managed to grasp most of the basic operations of code manipulation. I am currently working with vbstudio 2010 and am having a little difficulty understanding what is done for you as part of the wizard set up of a sql server database conection.

i understand i can manually connect to a database through code ie(con = New SqlConnection("Server=(local)\SQLEXPRESS;Initial Catalog=MyDatabase;Integrated Security=SSPI") blah blah

The thing is, i can see that VB creates a connection string for you as part of the database wizard process so does it create and name an object variable with this connection data already specified??

Assuming this i can then just reference the conection object in my code right? i cannot seem to figure out if it has done this despite actually seeing the string in the connection properties of the server explorer.

I am probably just missing something simple in my nieve ignorance here but a pointer would really help from someone.

Thanks people
 
If youre making a new data connection in "Server Explorer" then no, it has not added anything to your project.

Add a new DataSet object
Either add a new tableadapter to this dataset, or drag a table out of Server Explorer onto the dataset

Now you can see:
A DataTable - a local reprepsatation of the remote table (IT IS NOT THE DATABASE TABLE ITSELF)
A TableAdapter - a device that pushes data between the local and the database

You can reference these in your project:
Dim ta as New MyDataSetTableAdapters.MyTableAdapter
Dim dt as MyDataSet.MyDataTable = ta.GetData()

Or you can do things like drag them ot of the Data Sources window, onto your form, and VS will set this code up for you

Read the DW4 link in my signature, section Creating a Simple Data Application..
 
Hi Cjard,

Thanks for the reply. i am trying to implement a data reader to submit a 'SELECT WHERE' query to a database. just a simple search box for a learning exercise really. i was thinking rather than setting up the connection string manually in the code, VB studio was able to populate a connection string for you using the wizard. i didn't think i needed an adapter for this as i am not copying data to a local table.

Try
con.Open()
' cmd.ExecuteNonQuery() - useless
Dim reader As SqlDataReader = cmd.ExecuteReader()
If reader.Read() Then
' user exists
Else
' user doesn't exist
End If
reader.Close()
Finally
con.Close()
End Try


cheers
 
And.. youre struggling aren't you? Youre trying to learn something and it's taking ages, and not working..
Now, when you bought your new laptop, did you first take apart the pentium chip, or maybe try to learn assembler so you have a better idea of how the chip works, before you move on to higher level programming? Well, of course not.. few people would because that's just plain daft, right?

But that's in essence what youre doing here..
Read the DW4 link in my signature, section Creating a Simple Data Application..

I say this because you will then by following a tutorial, by the makers of the language youre using, that will get you up and familiar with some database related things.. It gets us using the very good designer system built into Visual Studio to do the lame, menial things of data access.. Trust me, connecting to a database and downloading a string of data from it is so inconsequential in the grand scheme of things that it's deferred to automated designers (just like the process of visually drawing a Form) so you can get on with some real programming rather than spending time writing repetitive, trivial code around DataReader

Follow the tutorial, if you get stuck or reach the end and think "where next?" come back..
 
I'm having a problem, I have this VB code, it says there is no errors and no errors arrise when I run the code but the information given is not getting into the database. Can someone help me know what I am missing?

Dim MyConnection As SqlConnection
Dim MyCommand As SqlCommand

'Create a Connection object.
MyConnection = New SqlConnection("server=HP; database=DSDB; Trusted_Connection=yes")
'Open the connection.
MyConnection.Open()
MyCommand = New SqlCommand("INSERT INTO DSDB.dbo.Employees(empID, empFname, empLname, empAddress, empCity, empState, empZip, empPhone) VALUES (5 ,'first', 'last', 'address', 'city', 'state', 'zip', 'phone')")
'Close the connection
MyConnection.Close()
 
Looks like you just picked a random topic to reply to rather than make a new one, but this has at least saved me some bother..

The topic that you posted in that you seem not to have read.. You should read it. It contains all the info you need to solve your problem. Here's a recap, just incase you missed it:

Read the DW4 link in my signature, section Creating a Simple Data Application
;)

ps; I could have told you what the exact problem with your code was, but then that would incline you to carry on with your data access in the way youre doing it currently, which is slow, frsutrating, error prone, bad object orientation and insecure. Instead of advising you the problem with your solution, I've advised you read a set of tutorials by microsoft that will get you doing your data access in a GOOD way. Follow the tutorials and the problem youre having right now, simply will not occur
 
Now you need the line commented as useless :)

VB.NET:
Dim MyConnection As New SqlConnection("server=HP; database=DSDB; Trusted_Connection=yes")
Try
     MyConnection.Open()
     Dim MyCommand As SqlCommand = MyConnection.CreateCommand
     MyCommand.CommandText = "INSERT INTO DSDB.dbo.Employees(empID, empFname, empLname, empAddress, empCity, empState, empZip, empPhone) VALUES (5 ,'first', 'last', 'address', 'city', 'state', 'zip', 'phone')"
     MyCommand.ExecuteNonQuery()
Catch ex As Exception
     MessageBox.Show(ex.Message)
Finally
     MyConnection.Close()
End Try

In addition you can safely ignore the DW4 link.
 
Last edited:
In addition you can safely ignore the DW4 link.

Yes, ignore an entire suite of in-depth tutorials written by the people who made the programming language youre using, and instead go for a few lines of bad advice on an internet forum because it is aimed at fixing your broken solution rather than teaching you the proper way to do things

:rolleyes:
 
Yes, ignore an entire suite of in-depth tutorials written by the people who made the programming language youre using, and instead go for a few lines of bad advice on an internet forum because it is aimed at fixing your broken solution rather than teaching you the proper way to do things

:rolleyes:

Ha well I'm not a prophet and I won't preach to anyone. If they post a broken code i will fix it without mentioning how terrible the code is.

In addition i understand your frustrations. Sometimes you see someone's code as a bad code. But don't worry. It's understandable, because it's beyond their knowledge.
 
I'll always advocate that we who know, have a responsibility to teach and guide people how best to start on an easy path for their own futures.. Suppose youre a father (I don't know if you are yet, or plan to be one day, but I'm sure you'll understand this) will you actively teach your children how to do something, like ride a bicycle, when you can see where they are going wrong.. Or will you just let them fall, hurt themselves, and then put a bandage on their cut knee? In doing this, you fix the problem they have encountered, but they are still not learning the right way to ride a bike.. Much better (in my opinion) that we spend the time to walk with them, guide them as they ride, teach them o they become capable of learning themselves?

Teaching for the future will vastly reduce the amount of work you have to do.. If people often come to you only seeking you to fix the problem with their solution, rather than have you help them design a better solution, then ExpertsExchange will probably be a better site for them to use.. There's something "commercial" about that place, I think caused by the "money" system of "paying" for the answers the OP wants, rather than to be educated how it should be done.. :)
 
Back
Top