add data to database
-
i think i have made the connection to the database. i need to know how to write the code to add and ID, surname and forename to the database. don't have a clue how to do this. can you help me? (this is the connection) Imports System.Data.OleDb Public Class cust Public conn As OleDbConnection Public comm As OleDbCommand Public dr As OleDbDataReader Public da As OleDbDataAdapter Private Sub cust_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load conn = New OleDbConnection 'establishes that conn is a new connection conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\PCbank.mdb" 'the name of the connection (name of the database on the end) conn.Open() 'opens the connection comm = New OleDbCommand 'establishes that comm is a new command comm.Connection = conn 'the connection for the command is the connection specified comm.CommandType = CommandType.Text 'the commands are in text End Sub
-
i think i have made the connection to the database. i need to know how to write the code to add and ID, surname and forename to the database. don't have a clue how to do this. can you help me? (this is the connection) Imports System.Data.OleDb Public Class cust Public conn As OleDbConnection Public comm As OleDbCommand Public dr As OleDbDataReader Public da As OleDbDataAdapter Private Sub cust_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load conn = New OleDbConnection 'establishes that conn is a new connection conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\PCbank.mdb" 'the name of the connection (name of the database on the end) conn.Open() 'opens the connection comm = New OleDbCommand 'establishes that comm is a new command comm.Connection = conn 'the connection for the command is the connection specified comm.CommandType = CommandType.Text 'the commands are in text End Sub
-
i think i have made the connection to the database. i need to know how to write the code to add and ID, surname and forename to the database. don't have a clue how to do this. can you help me? (this is the connection) Imports System.Data.OleDb Public Class cust Public conn As OleDbConnection Public comm As OleDbCommand Public dr As OleDbDataReader Public da As OleDbDataAdapter Private Sub cust_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load conn = New OleDbConnection 'establishes that conn is a new connection conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\PCbank.mdb" 'the name of the connection (name of the database on the end) conn.Open() 'opens the connection comm = New OleDbCommand 'establishes that comm is a new command comm.Connection = conn 'the connection for the command is the connection specified comm.CommandType = CommandType.Text 'the commands are in text End Sub
well its jst the simple addiing in data base... here is the code for u.. check it out....and put yr table name wherever specified tablename and make required changes
Imports System Imports System.data.oledb Inherits System.Windows.Forms.Form public class form1 Dim da As New OleDbDataAdapter("select * from tablename", "provider=microsoft.jet.oledb.4.0;data source=|DataDirectory|\PCbank.mdb;") Dim ds As New DataSet Dim cn As OleDbConnection Dim cmd As OleDbCommand Dim dr As OleDbDataReader Dim i As Integer Dim str As String 'under insert button Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click Try cn = New OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=GIVE PATH of yr mdb file;") cn.Open() cmd = New OleDbCommand("insert into tablename(ID,surname,forename) values('" & (TextBox1.Text) & "','" & (TextBox2.Text) & "','" & (TextBox3.Text) & ")", cn) i = cmd.ExecuteNonQuery() MsgBox("record inserted") Catch ex As Exception End Try cn.Close()
-
i think i have made the connection to the database. i need to know how to write the code to add and ID, surname and forename to the database. don't have a clue how to do this. can you help me? (this is the connection) Imports System.Data.OleDb Public Class cust Public conn As OleDbConnection Public comm As OleDbCommand Public dr As OleDbDataReader Public da As OleDbDataAdapter Private Sub cust_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load conn = New OleDbConnection 'establishes that conn is a new connection conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\PCbank.mdb" 'the name of the connection (name of the database on the end) conn.Open() 'opens the connection comm = New OleDbCommand 'establishes that comm is a new command comm.Connection = conn 'the connection for the command is the connection specified comm.CommandType = CommandType.Text 'the commands are in text End Sub
you can use this code also: Public Function Query(ByVal SQL As String, ByVal OLEDBCon As OleDb.OleDbConnection, ByRef DS As DataSet) As Integer Dim DA As New OleDb.OleDbDataAdapter(SQL, OLEDBCon) Query = DA.Fill(DS) DA = Nothing End Function Public Function Execute(ByVal SQL As String, ByVal OLEDBCon As OleDb.OleDbConnection) As Integer Dim com As New OleDb.OleDbCommand(SQL, OLEDBCon) com.ExecuteNonQuery() com = Nothing End Function this is your connection string: Dim con As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; " & _ "Data Source=sample.mdb") code of your button save: dim strInsert as string try strInsert="Insert into table1 (field1,fields2...) Values('" & txt1 & "','" & txt2 & "'...)" con.open Execute(strInsert,con) MsgBox("Record Saved") con.close end try my code is simple.:)
Don't block the drive way of all the newbies in programming. :))