Windows Develop Bookmark and Share   
 index > Windows Forms General > insert values in to database using ado.net -sql server 2005 & visual basic 2005
 

insert values in to database using ado.net -sql server 2005 & visual basic 2005

Hi,

I am doing one project usingvisual basic2005 express edition. so i want to know how to insert values in to sql server database using ado.net.

i want to get the values from textboxes and then insert in to database. pls help me.

Sinu

sinusoftware  Sunday, September 16, 2007 7:32 PM

You can do something like this

Code Snippet


Private
Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)

Dim connstr As String = ("Persist Security Info=False;Integrated Security=SSPI;Initial Catalog=Northwind;server=localhost")

Dim conn As SqlClient.SqlConnection = New SqlClient.SqlConnection(connstr)

Dim sql As String = "INSERT INTO test(custName) VALUES(@custName)"

Dim cmd As SqlClient.SqlCommand = New SqlClient.SqlCommand(sql, conn)

cmd.Parameters.Add("@custName", SqlDbType.VarChar, 255)

cmd.Parameters("@custName").Value = Me.textBox1.Text

conn.Open()

Try

cmd.ExecuteNonQuery()

Catch

End Try

End Sub

Zhi-Xin Ye  Tuesday, September 18, 2007 3:23 AM

You can do something like this

Code Snippet


Private
Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)

Dim connstr As String = ("Persist Security Info=False;Integrated Security=SSPI;Initial Catalog=Northwind;server=localhost")

Dim conn As SqlClient.SqlConnection = New SqlClient.SqlConnection(connstr)

Dim sql As String = "INSERT INTO test(custName) VALUES(@custName)"

Dim cmd As SqlClient.SqlCommand = New SqlClient.SqlCommand(sql, conn)

cmd.Parameters.Add("@custName", SqlDbType.VarChar, 255)

cmd.Parameters("@custName").Value = Me.textBox1.Text

conn.Open()

Try

cmd.ExecuteNonQuery()

Catch

End Try

End Sub

Zhi-Xin Ye  Tuesday, September 18, 2007 3:23 AM

Can you write that snippet in C# for me?

Thanks in advance. Also, if this was a web form, would it be smarter to use a sqlDataAdapter or just using INSERT with a sqlCommand? This is for a web service and users will need to login to use the data submittal forms.

ChristopherAlexander  Wednesday, April 02, 2008 8:32 PM

You can use google to search for other answers

Custom Search

More Threads

• Separating values of a text box
• Reporting tool for a windows form application ??
• ListView and MS Access
• Problem with inserting controls in FlowlayoutPanel
• Error 3201: "Cannot open backup device ..
• SETUP Forcing desktop short to All Users
• Automatic printing web reports
• WinForms InterOp
• selecting an object
• Form with AcceptButton and a textbox with a CustomSource flyout