Start New Discussion within our Software Development Community

How to connect queries from SQL Server 2005 to VB.Net Windows application

Be sure you use parameterized queries when building your applications! Often people vary the queries' command text with user input. Here is an example of using the Sql* classes and parameterized SQL:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    'Insert an image
    Using conn As New System.Data.SqlClient.SqlConnection("Data Source=apex2006sql;Initial Catalog=Scott;Integrated Security=True;")
      Using cmd As New SqlClient.SqlCommand("Insert Into Picture (Name, CreateDate, Picture) Values (@Name, @CreateDate, @Picture)", conn)
        cmd.Parameters.Add(New SqlClient.SqlParameter("@Name", SqlDbType.VarChar)).Value = "Picture 1"
        cmd.Parameters.Add(New SqlClient.SqlParameter("@CreateDate", SqlDbType.VarChar)).Value = DateTime.Today
        cmd.Parameters.Add(New SqlClient.SqlParameter("@Picture", SqlDbType.Image)).Value = IO.File.ReadAllBytes("C:\picture.bmp")
      End Using
    End Using
  End Sub

How to connect queries from SQL Server 2005 to VB.Net Windows application

Dim conn as new sqlclient.sqlconnection
                   Dim command as new sqlclient.sqlcommand
                   Dim dataadapter as new sqlclient.sqldataadapter 
                   Dim dt as new Datatable
                   Dim sql as string
                   conn.connectionstring("put your data source here")                           
                   sql= "your sql statement here"

this is for an insert or update statement.
if u want a select query replace the command.executenonquery with this

adapter.selectcommand= command

Hope this is helpful.
Let me know if you have any questions.

This article has been dead for over six months. Start a new discussion instead.