DataBase connection and adding data in tables

b0gdan

New member
Joined
May 14, 2007
Messages
2
Programming Experience
1-3
Hi i am used with vb progr. language but i`m new in this strange vb.net environment. I have a sql server database and i want to add data in some tables. i have a form for this....the problem is that i don`t know how to connect to that database and how to add data in tables e.g name, age etc.

Thanks!
 
'import namespaces
Imports System.Data
Imports System.Data.SqlClient

'create connection instance
Dim cnn as new SqlConnection("integrated security=SSPI;data source=server name;persist security info=False;initial catalog = database name")
")
'put this rest of the code in any event handler
Dim sqlString as string = "INSERT INTO table_name (columns) VALUES (values_to_insert)"

Dim objCommand as new SqlCommand(sqlString, cnn)

try
cnn.open()
objCommand.ExecuteNonQuery()
catch ex as Exception
MessageBox.Show("(Error: " + ex.Message + ")
End Try

'close sql connection
objCommand.dispose
cnn.Close
 
Use the GUI, it's why it is there!!!!!!!! The wizards create your connection string(s), create your datatables and datasets and allow you to manipulate them and code from them.

I really don't understand why people using VS2005 (or even 2003) still will not use the GUI driven wizards. Why would Microsoft put them there otherwise?!?!
 
Every developer has their own disposition in developing applications. Microsoft provided connection wizards for additional options and it doesn't mean necessarily that you have to use it every time you need a database connection.
 
Every developer has their own disposition in developing applications. Microsoft provided connection wizards for additional options and it doesn't mean necessarily that you have to use it every time you need a database connection.

I'm not denying that :)

The OP of this thread has said he is new to VB.net. As a new developer in VS2005 I suggested he use the wizards as it will create all the code and will get him started quicker and easily than having to write all the code himself.

I know a lot of new users have found it very useful and more helpful to use the wizards, then examine all the generated code to work out and learn what each bit does, and then start from scratch writing the code themselves on another form.

That's my opinion, it wasn't aimed directly at you and I'm sorry if you took it as offensive.
 
Every developer has their own disposition in developing applications. Microsoft provided connection wizards for additional options and it doesn't mean necessarily that you have to use it every time you need a database connection.

Yeah, but not doing so is about as moronic as arguing that sometimes its easier to write the code that lays out a form and all its controls, and all their properties using Notepad or saying that its simpler to have just one class in a project with one sub that runs a huge select case statement for every line of code the program runs.

If you have a project that uses a visually designed, auto-genned dataset then getting a connection is as simple as grabbing whatever tableadapter is lying around and borrowing its connection.. However, you wouldnt even need to do this, because you should put your query into that tableadapter, and let it manage the connection for you. Suddenly your program's button click handler goes from 20 lines of messy, poorly encapsulated, bad OO, unreadable code to just a single line of something meaningful. Those who say otherwise are usually the ones who never quite grasped the concept of defensive programming, OO, code maintainabiltiy and modularity in the face of cut and paste an old bit of VB6 code.. Blame tradition ;)
 
Hi i am used with vb progr. language but i`m new in this strange vb.net environment. I have a sql server database and i want to add data in some tables. i have a form for this....the problem is that i don`t know how to connect to that database and how to add data in tables e.g name, age etc.

Thanks!

Take a read of the DW2 link in my signature, section on Creating a Simple Data Application
 
No :) Its pretty alright with me Arg81 you didn't offended me in anyway. I am sorry myself if i offended you instead. God Bless :)
 
@ cjard & Arg81:

I'm glad others feel the same way I do. I had several "discussions" with others on this forum who said that a good programmer should be able to write all that code themselves without the use of the wizards. I guess I could have but I've got better things to do. :)

@ b0gdan:

The walkthroughs cjard has linked to are very good, I would suggest starting there as explaining data access in full is far too complicated to handle in a forum.
If you prefer videos, many can be found here: Visual Web Developer Express - Easy to Learn. The videos are for the Express versions but will apply to all versions.
 
@ cjard & Arg81:

I'm glad others feel the same way I do. I had several "discussions" with others on this forum who said that a good programmer should be able to write all that code themselves without the use of the wizards. I guess I could have but I've got better things to do. :)

Absolutely.. using code to write code is sometimes the best way of writing a lot of good code quickly. For those who are skeptical, and consider themselves elite, ask them if they understand what template metaprogramming is in C++, and if they do then they should think on the conceptual differences between that an a wizard, and then pipe down a bit ;)
If they dont, maybe they arent so elite after all ;)

People often take a dim view of wizards while failing to acknowledge that pretty much everything above banging the hardware with 0s and 1s is a layer of abstraction designed to make something easier to use and get more done with less effort; i.e. a wizard ;)
 
Back
Top