Breaking News

Create a sql connection class or module with vb.net and also create database according to your location, table.

Know More :  ASP.Net  VB.Net  C#.Net  Sliverlight  XML  WPF  WCF Ajax
Let's start with visual studio 2008\2005\2003 using language vb.net.Create a new project name Con_Class. Also Sql server 2008 \2005 express  or evaluation Open run command and type "devenv" after press enter. After open visual studio go to file menu and select new project or as shown in figure like this..

Click on project, new project dialog box will be open as shown in figure..


Now  Left pane select vb.net , after select vb.net language go to templates and select windows form application also type the name according to you in the name box click on OK button.Now form1 will be open as shown in figure..






Now go to project menu select add new item   as shown in figure..

After select add new item , add new item dialog box will be open as shown in figure.. also select class and type the name of Class_conn and click on ok button..

Now class will be open as shown in figure..


         
         
         
         
         
         
         
         
         
         

Imports System.Threading.Thread
Imports System.IO
Imports System.Data.SqlClient
Imports System.Data.OleDb
Public Class Class_conn

    Public Sub create_con()
        'Dim conDatabase As SqlConnection = _
        '             New SqlConnection("Server= .\SQLEXPRESS;Trusted_Connection=true;Integrated Security=SSPI;")
        'Dim cmdDatabase As SqlCommand = _
        ' New SqlCommand("create database=Assoc_comp;", conDatabase)
        'conDatabase.Open()
        'cmdDatabase.ExecuteNonQuery()
        'conDatabase.Close()

        'Dim conDatabase1 As SqlConnection = _
        '             New SqlConnection("Server= .\SQLEXPRESS; database=Assoc_comp; Trusted_Connection=true;Integrated Security=SSPI;")
        'Dim cmdDatabase1 As SqlCommand = _
        ' New SqlCommand("create table ;", conDatabase)

        Dim dr As DialogResult = MessageBox.Show("If you are creating connection first time then click on yes" & _
                " Don't create connection more than one times ! May be loss your data !", "Data Server Connection " & _
                "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)

        If dr = Windows.Forms.DialogResult.Yes Then
            Try
                '.\SQLEXPRESS --you can type your sql server
                Dim con As New OleDb.OleDbConnection("Provider=SQLNCLI.1;Data Source=.\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=master")
                'Example for create database
                Dim str As String = "CREATE DATABASE Assoc_comp ON ( NAME = Assoc_comp_dat,    FILENAME = 'D:\AC_backup\Assoc_comp.mdf',    SIZE = 10,    MAXSIZE = 150,    FILEGROWTH = 5 )LOG ON( NAME = Assoc_comp_log,    FILENAME = 'D:\AC_backup\Assoc_comp.ldf',    SIZE = 5MB,    MAXSIZE = 150MB,    FILEGROWTH = 5MB ) "
                Dim cmd As New OleDbCommand(str, con)
                con.Open()
                cmd.ExecuteNonQuery()
                con.Close()

                Dim con2 As New OleDb.OleDbConnection("Provider=SQLNCLI.1;Data Source=.\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=Assoc_comp")
                'Example for  create table
                Dim tabl As String = "create table retail(Serial_num int identity(1,1) not null primary key,Tin_num varchar(20)not null,Invoice_no varchar(50)  not null,Date varchar(20)not null,Month varchar(15)not null,Year varchar(5)not null,Customer_Name varchar(max) not null,Customer_Mobile varchar(13) not null,Customer_Address varchar(max) not null,Product varchar(250) not null,Product_Number varchar(50) not null,Product_Serail_num varchar(50) not null,qty int not null,rate money not null,Total_price money,Vat varchar(max) ,Total_price_with_vat money ,Total_item varchar(11) not null,)"
                Dim cmdt As New OleDbCommand(tabl, con2)
                con2.Open()
                cmdt.ExecuteNonQuery()
                con2.Close()
                'Example for multiple table
                Dim tstock As String = "create table retail_one(Serial_num int identity(1,1) not null primary key,Tin_num varchar(20)not null,Invoice_no varchar(50)  not null,Date varchar(20)not null,Month varchar(15)not null,Year varchar(5)not null,Customer_Name varchar(max) not null,Customer_Mobile varchar(13) not null,Customer_Address varchar(max) not null,Product varchar(250) not null,Product_Number varchar(50) not null,Product_Serail_num varchar(50) not null,qty int not null,rate money not null,Total_price money,Vat varchar(max) ,Total_price_with_vat money ,Total_item varchar(11) not null,)"
                Dim cmdst As New OleDbCommand(tstock, con2)
                con2.Open()
                cmdst.ExecuteNonQuery()
                con2.Close()

            Catch ex As SqlException
                MessageBox.Show(ex.Message)
            End Try

        End If

    End Sub
End Class


Now go to form design and double click on button as shown in figure..


Private Sub ButtonConn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim cl_con As New Class_conn
        cl_con.create_con()
    End Sub

Now open sql server and view your database , table created. Thanks..

Know More :  ASP.Net  VB.Net  C#.Net  Sliverlight  XML  WPF  WCF Ajax

No comments