Simple VB.Net DB Question: Simple INSERT into DB from Form
Posted on 2007-08-01
Ok, here is what I think is probably another easy DB/VB.Net 2005 question.
I have a form that has 2 textbox's (textbox1 and textbox2). I want to input the information typed into these textbox's into a SQL DB. I have done the following for my code (buiding on what I learned on may last post yesterday:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim str As String
Const dbct As String = "Data Source=fakedbname;Initial Catalog=fakedb;Integrated Security=SSPI;"
Dim cn As SqlClient.SqlConnection ' Usually would be in the Function, but it's alone here.
Dim str2 As SqlClient.SqlCommand 'The actual SQL Command object.
Dim cnn As New SqlClient.SqlConnection 'The SQL CONNECITON. THIS IS NEW TO YOU.
namevar = TextBox1.Text
datevar = TextBox2.Text
str = "insert inv(ComputerName, datescanned) values ('" & namevar & "','" & datevar & "')"
cn = New System.Data.SqlClient.SqlConnection(dbct) ' Make the conneciton.
str2 = New SqlClient.SqlCommand ' Not sure why I need to do this twice.
str2.CommandText = str 'Making the string.
str2.Connection = cn 'Making the connection.
(********This is where I am assuming the INSERT Command will occur********(
Catch ex As Exception
Would I use a DATAWRITER or some other "function" to enter the data. I'm a VB6 person and in the past I would just use the ADODB.RECORDSET and run the insert. I'm trying to figure out how to do this now in VB.Net 2005.
Again, this is probably a pretty easy solution, but I'm willing to give 500 points for a good/quick answer.