Results 1 to 5 of 5

Thread: hi this small exmple to bulid database

Threaded View

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2008
    Posts
    5

    hi this small exmple to bulid database

    i am saeed alhosni from Oman.
    let's start.
    create database with Microsoft access "People.mdb"
    with password =hand
    put the database in Debug folder in new vb project(the next one)
    create table name it"LifePeople" with two cluoms one "hi" second "hii"
    open visual studio 2005.
    start new project
    add new Module(name it "Connection_String"
    write this code
    vb.net Code:
    1. Imports System.Data.Odbc
    2. Imports System.Data.OleDb
    3. Imports System
    4. Imports System.Data
    5. Module Connection_String
    6.    
    7.     Public MyPath As String = Application.StartupPath & "\People.mdb"
    8.     Public MyPassword = "hand"
    9.  
    10.     Public ConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & MyPath & ";Jet OLEDB:Database Password=" & MyPassword
    11.     Public str_sqa, TableName, msg, CardNumber, UserName As String
    12.  
    13.  
    14.     Public VoucherString As String
    15.  
    16.    
    17. End Module
    add new Class "db_operation"
    write this code
    vb.net Code:
    1. Imports System
    2. Imports System.Data
    3. Imports System.Data.OleDb
    4. Public Class db_operation
    5.     'The following example uses the OleDbCommand, along
    6.     'OleDbDataAdapter and OleDbConnection, to select rows from an Access database.
    7.     'The filled DataSet is then returned.
    8.     'The example return a DataSet,
    9.     'and is passed a query string that is an SQL SELECT statement,
    10.     'and a string that is the name of the source database table
    11.     'a connection string which is global variable stored in Connection_String Module
    12.     Function PopulateDataset(ByVal str_sqa As String, ByVal TableName As String) As dataset
    13.         Try
    14.             Dim con As OleDbConnection 'Represents an open connection to a data source
    15.             'The OleDbDataAdapter provides this bridge by using Fill
    16.             'to load data from the data source into the DataSet
    17.             Dim da As OleDbDataAdapter
    18.             Dim ds As dataset
    19.             con = New OleDbConnection(ConnString)
    20.             da = New OleDbDataAdapter(str_sqa, con)
    21.             ds = New dataset
    22.             da.Fill(ds, TableName)
    23.             Return ds
    24.         Catch ex As Exception
    25.             MsgBox(ex.ToString)
    26.         End Try
    27.     End Function
    28.     'This sub routine handles database operation such as add. delete, or update
    29.     Sub db_Operation(ByVal src_sqa As String, ByVal msg As String)
    30.         Try
    31.             Dim con As New OleDbConnection(ConnString)
    32.             Dim str_sqa As String
    33.             'OleDbCommand - Represents an SQL statement
    34.             'or stored procedure to execute against a data source.
    35.             Dim cmd As New OleDbCommand(src_sqa, con)
    36.             con.Open()
    37.             str_sqa = src_sqa
    38.             'ExecuteNonQuery
    39.             'Executes a SQL statement against the Connection
    40.             'and returns the number of rows affected.
    41.             cmd.ExecuteNonQuery()
    42.             con.Close()
    43.             MsgBox(msg)
    44.         Catch ex As Exception
    45.             MsgBox(ex.ToString)
    46.         End Try
    47.     End Sub
    48.  
    49.     Public Function GetFilledTable(ByVal query As String, ByVal connection As OleDbConnection) As DataTable
    50.         Dim command As New OleDbCommand(query, connection)
    51.  
    52.         connection.Open()
    53.  
    54.         Dim reader As OleDbDataReader = command.ExecuteReader(CommandBehavior.KeyInfo Or CommandBehavior.CloseConnection)
    55.         Dim schema As DataTable = reader.GetSchemaTable()
    56.         Dim columns(schema.Rows.Count - 1) As DataColumn
    57.         Dim column As DataColumn
    58.  
    59.         'Build the schema for the table that will contain the data.
    60.         For i As Integer = 0 To columns.GetUpperBound(0) Step 1
    61.             column = New DataColumn
    62.             column.AllowDBNull = CBool(schema.Rows(i)("AllowDBNull"))
    63.             column.AutoIncrement = CBool(schema.Rows(i)("IsAutoIncrement"))
    64.             column.ColumnName = CStr(schema.Rows(i)("ColumnName"))
    65.             column.DataType = CType(schema.Rows(i)("DataType"), Type)
    66.  
    67.             If column.DataType Is GetType(String) Then
    68.                 column.MaxLength = CInt(schema.Rows(i)("ColumnSize"))
    69.             End If
    70.  
    71.             column.ReadOnly = CBool(schema.Rows(i)("IsReadOnly"))
    72.             column.Unique = CBool(schema.Rows(i)("IsUnique"))
    73.             columns(i) = column
    74.         Next i
    75.  
    76.         Dim data As New DataTable
    77.         Dim row As DataRow
    78.  
    79.         data.Columns.AddRange(columns)
    80.  
    81.         'Get the data itself.
    82.         While reader.Read()
    83.             row = data.NewRow()
    84.  
    85.             For i As Integer = 0 To columns.GetUpperBound(0)
    86.                 row(i) = reader(i)
    87.             Next i
    88.  
    89.             data.Rows.Add(row)
    90.         End While
    91.  
    92.         reader.Close()
    93.  
    94.         Return data
    95.     End Function
    96.  
    97. End Class
    add to the form
    -2 textbox
    -one datagridview

    add this code to the form1 load
    vb.net Code:
    1. dim intCtrCard as integer
    2. Try
    3.             Dim get_Data As New db_operation
    4.             str_sqa = "SELECT * From LifePeople"
    5.             TableName = "LifePeople"
    6.             MyTableCard = get_Data.PopulateDataset(str_sqa, TableName).Tables(0)
    7.             intRecordCountCard = MyTableCard.Rows.Count
    8.             Me.datagridview1.DataSource = MyTableCard            
    9. Me.textbox1.Text = MyTableCard.Rows(intCtrCard)(0).ToString      
    10. Me.textbox2.Text = MyTableCard.Rows(intCtrCard)(1).ToString      
    11.  
    12.         Catch ex As Exception
    13.             MsgBox(ex.ToString)
    14.         End Try
    Last edited by Hack; May 7th, 2008 at 09:03 AM. Reason: Added Highlight Tags

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width