VB.NET > SQL Database

K

Ken Briscoe

Hi all,
Longtime listener, first time caller...anyways, I'm just starting out
(literally...I've done the obligatory "Hello, World" and messed around a
little more). What I want to do is create an app that will accept 2
character strings, and when 'OK' is clicked, have it dump one string into
one field in a SQL table (SQL server is the same machine as I'll be using
and writing the program with), and dump the other string into a different
field in the same table. The database has 3 tables in it, and all are empty
awaiting data. I'm fairly competent with SQL statements, but as I said, I'm
VERY new to Visual Basic, and especially the .NET framework. What I'm stuck
on is the code to make the text boxes dump the data into the database. I
don't even know how to tell it to establish a connection to the SQL Server.
(This program won't ever be run on another computer, it's just for my own
purposes, so I don't need to have a selection for servers or databases.) I'm
hoping someone would be so kind as to get me started here or point me to a
place on the web or book that deals with getting the data into the database.
Basically, what I'm doing is writing a data entry program. At this point, I
don't even need to manipulate the data with the program, just get it into
the database. Any help is appreciated! Thank you!

KB
 
S

Stephen Muecke

Ken,

1. Create a connection and sets its ConnectionString property
Dim conn as New SQLConnection
conn.ConnectionString = <the connection string to your database>

2. Create a command to insert the data
I'll assume your using a stored procedure with 2 parameters named @Text1 and
@Text2 and the datatypes are VarChar(100)
Dim cmd as New SqlCommand
With cmd
.Connection = conn
.CommandType = CommandType.StoredProcedure
.CommandText = <name of your stored procedure>
With .Parameters
.Add("@Text1", SqlDbType.VarChar, 100).Value = TextBox1.Text
.Add("@Text2", SqlDbType.VarChar, 100).Value = TextBox2.Text
End With
End With

3. Open the connection and execute the command
Try
conn.Open
cmd.ExecuteNonQuery
Catch ex as Exception
..........
Finally
conn.Close
End Try

Read the help files on SqlConnection and SqlCommand

Stephen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top