Hello community i am new to vb.net and was wondering if anyone had some turtorials as i am trying to create a tool for my job.

I would like to create a tool to gather data from sql 2008 by searching a clients name and then pulling the clients record and exporting it to a txt file
any info on were to start will be greatly appreciated my coding backround is in java and python i am learning vb as i go so bear with me.

Thank you

1)You will want to start with connecting to the database.
There are many different types of connection providers, you will have to chose the one you like the best.

2)You will have to retrieve and store the information in a data table.
You will find out more about this when researching connecting to a database.
You will need to write queries to return unique values.

3)You will cycle through the table, writing the values to a text file.
You will need to research stream readers/writers.

Tutorials:

Database Connection:
here
Querying Database:
here
Writing Data to Text File:
reader
writer

There are several ways to do this. My preference (because this is where I have the most experience) is to use ADO (newer approaches use OLEDB). To do this try the following:

Create a blank project
Add a project reference to adodb by
Project -> Add Reference
Select .NET tab
Select adodb

Add a button named btnGetData
Copy in the following code

You will have to change a few things for when you run it on your computer. If you are using SQLEXPRESS then you should be ok with the current SERVER name. You will have to change the values for DATABASE and OUTFILE and most definitely you will need to change the names in the "select UserName..." statement.

Imports ADODB
Imports System.IO
Imports System.Data.SqlClient

Public Class Form1

    Const SERVER = ".\sqlexpress"       'the .\ refers to the local computer    
    Const DATABASE = "mydb"             'this is the name of my test database   
    Const OUTFILE = "D:\temp\out.txt"   'this is file that will be written      

    Private Sub btnGetData_Click(sender As System.Object, e As System.EventArgs) Handles btnGetData.Click

        Dim con As New ADODB.Connection     'this connects you to a server and database 
        Dim rec As New ADODB.Recordset      'this is where you get your data            

        'open a connection to the database and select some records

        con.Open("Driver={SQL Server};Server=" & SERVER & ";Database=" & DATABASE & ";Trusted_Connection=yes;")
        rec.Open("select UserName, DateIn from Test1", con, CursorTypeEnum.adOpenForwardOnly)

        'open an I/O channel to a file

        Dim fs As New FileStream("d:\temp\out.txt", FileMode.OpenOrCreate, FileAccess.Write)
        Dim sw As New StreamWriter(fs)

        'write all of the retrieved records to the file

        Do Until rec.EOF
            sw.WriteLine(rec("UserName").Value & "," & rec("DateIn").Value)
            rec.MoveNext()
        Loop

        'close the connections

        sw.Close()
        rec.Close()
        con.Close()

    End Sub

End Class

Edited 4 Years Ago by Reverend Jim

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