Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

VISUAL BASIC 2010 EXPRESS & SQL SERVER 2008 EXPRESS

Two great tools that go great together VB.Net and SQL Server are powerful, industry-standard development tools from Microsoft. Its sometimes surprising to realize that anyone can download and use these programs for free. Getting them installed and working together took longer than I expected. Google searches showed that many people were even questioning whether a VB Express program could could access a SQL Server Express database. It can, but theres a trick to it. So if youre trying to get started with your first VB+SQL program, heres a small app to start you off. I hope it saves you a couple hours! ______________________________________________ First, download and install Visual Basic 2010 Express and Sql Server 2008 Express from www.microsoft.com/express.
SQL SERVER

Next, start Sql Server Management Studio, and log in. (Start All Programs Microsoft SQL Server 2008 SQL Server 2008) Create a new database by right-clicking Databases and selecting New Database

Name the new database MyTestDB and click OK.

The Weeknd - _What You Need_ ... .mp4 Madonna - Justify My Love (vi... .mp4 Taio Cruz - There She Goes.mp4 The White Stripes - I Just Do

Expand the Databases folder to find your new database:

Click New Query and run this script to create an empty table named tblTest: CREATE TABLE [MyTestDB].[dbo].[tblTest] ( [tstNum_PK] [int] IDENTITY(1,1) NOT NULL , [tstData] [varchar] (100) NOT NULL ) ON [PRIMARY] Visual Basic Now start Visual Basic and create a new Windows Form Application named DatabaseTest. (Start All Programs Microsoft Visual Studio 2010 Express Visual Basic 2010)

On your VB Form, add two TextBoxes and two Buttons. For simplicity, well leave the their properties at their default settings and change them in the code.

Heres the trick: with Visual Basic 2010 Express, you have to connect directly to the database file. You may be used to connecting to remote database servers you cant do that here. When you created MyTestDB above, SQL Server created a file on your PC named MyTestDB.mdf. Find it, and if the full path name is different from:
C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\MyTestDB.mdf

Then youll need to change two lines in this sample code to match your location. Add the code below to your program. Now youre ready to run. Start the program and type something into the first text box. Then click Add to DB.

Unless you see an error message, a new record was just written to your database. To read and display all the records in this table, click Show DB.

The 1 at the beginning is the Primary Key for your record a field you generally want to include, but would rarely display. Thats it. If you got all the way through, you now have a working (though barebones) program to begin developing your own applications. Good luck!

Imports System.Data.SqlClient Public Class Form1 'at program startup, set the control properties Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Button1.Text = "Add to DB" Button2.Text = "Show DB" TextBox2.Multiline = True End Sub 'write a record to the database table Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'create the DB connection Dim sqCon As New SqlClient.SqlConnection("Server=.\SQLExpress;Att achDbFilename=C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\MyTestDB .mdf;Database=MyTestDB; Trusted_Connection=Yes;") Dim sqCmd As New SqlClient.SqlCommand sqCmd.Connection = sqCon

sqCmd.CommandText = "INSERT INTO [MyTestDB].[dbo].[tblTest] VALUES ('" & TextBox1.Text & "')"

'open the connection sqCon.Open() 'execute the SQL command sqCmd.ExecuteNonQuery() 'close the connection sqCon.Close() End Sub 'read and display all records from the database table Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim sqCon As New SqlClient.SqlConnection("Server=.\SQLExpress;Att achDbFilename=C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\MyTestDB .mdf;Database=MyTestDB; Trusted_Connection=Yes;") Dim sqCmd As New SqlClient.SqlCommand Dim sdrRow As SqlClient.SqlDataReader

'define the DB connection and search string sqCmd.Connection = sqCon sqCmd.CommandText = "SELECT * FROM tblTest" 'open the DB connection sqCon.Open() 'read the entire table sdrRow = sqCmd.ExecuteReader() 'extract and display each field 'clear the text box TextBox2.Text = "" Do While sdrRow.Read() 'get the primary key TextBox2.Text = TextBox2.Text & sdrRow.GetValue(0) & vbTab 'get the string Loop TextBox2.Text = TextBox2.Text & sdrRow.GetValue(1) & vbCrLf 'close up and return sdrRow.Close() sqCon.Close()

End Sub End Class

Print preview for Datagrid form: Dim abs = form1.datagrid1.currentcell.RowIndex e.graphics.drawstring(form1.grid.item(form1.colum n1.headertext,abs).value.tostring,systemfonts. brushesh.black,200,300)

When it is ready to print a page, the PrintDocument raises its PrintPage event. The program's event handler draws the string "Landscape" on page 1 and "Portrait" on page 2.
Private Sub m_PrintDocument_PrintPage(ByVal sender As _ Object, ByVal e As _ System.Drawing.Printing.PrintPageEventArgs) Handles _ m_PrintDocument.PrintPage Dim txt As String If m_NextPage = 1 Then ' Print in landscape. txt = "Landscape" Else ' Print in portrait. txt = "Portrait" End If ' Print some text. Dim the_font As New Font("Times New Roman", _ 60, FontStyle.Bold, GraphicsUnit.Point) Dim layout_rect As New RectangleF( _ e.MarginBounds.X, e.MarginBounds.Y, _ e.MarginBounds.Width, e.MarginBounds.Height) Dim string_format As New StringFormat string_format.Alignment = StringAlignment.Center string_format.LineAlignment = StringAlignment.Center

e.Graphics.DrawString(txt, the_font, _ Brushes.Black, layout_rect, string_format) e.Graphics.DrawRectangle(Pens.Black, e.MarginBounds) 'There are two pages. m_NextPage += 1 e.HasMorePages = (m_NextPage = 2) End Sub

You might also like