1,105,363 Community Members

connect SQL Server 2005 to VB.Net

Member Avatar
jayanthsept5
Newbie Poster
1 post since Dec 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
__avd
Posting Genius (adatapost)
6,703 posts since Oct 2008
Reputation Points: 1,811 [?]
Q&As Helped to Solve: 1,272 [?]
Skill Endorsements: 61 [?]
Moderator
 
0
 

You need to know that there is a class library called ADO.NET. Take a look at this article - http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.aspx

Member Avatar
sknake
Senior Poster
3,957 posts since Feb 2009
Reputation Points: 1,620 [?]
Q&As Helped to Solve: 747 [?]
Skill Endorsements: 25 [?]
Featured
 
0
 

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;")
      conn.Open()
      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")
        cmd.ExecuteNonQuery()
      End Using
    End Using
  End Sub
Member Avatar
TomW
Posting Whiz
342 posts since Sep 2009
Reputation Points: 73 [?]
Q&As Helped to Solve: 48 [?]
Skill Endorsements: 3 [?]
 
0
 

Here are some tutorials for working with VB.Net & Sql Server:
Data Walkthroughs

Here is a link dedicated soley to connection strings:
Connection Strings

Member Avatar
G_Waddell
Practically a Posting Shark
821 posts since Nov 2009
Reputation Points: 131 [?]
Q&As Helped to Solve: 137 [?]
Skill Endorsements: 13 [?]
 
0
 

Here is a good site for finding out what connections you need to use to connect a datasource (e.g. SQL Server, Excel, Oracle etc) to your code.

www.connectionstrings.com

Member Avatar
azdonald
Newbie Poster
14 posts since Dec 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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"
                   conn.open()
                   command.connection=conn
                   command.commandtext=sql
                   command.executenonquery

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

adapter.selectcommand= command
            adapter.fill(dt)

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

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article