Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
how to insert data in data base allready created
ex.
i am new memer so how insert my data in data base (conectivity)
Posted
Comments

Here's an example of an Insert operation in VB.NET using a query with parameters:

VB
Dim cn As SqlConnection
cn = New SqlConnection("server=localhost;uid=sa;pwd=;database=medisam")
cn.open

Dim Sqlstr as string
dim cmdInsert  as new sqlcommand(Sqlstr , cn)
Sqlstr="Insert Into asset(code , serial_number , trade_mark , callibration_date , class , category , subcategory) Values (@code , @serial_number , @trade_mark , @callibration_date , @class , @category , @subcategory )"
cmdInsert.Parameters.Add("@code",Data.SqlDbType.nvarchar).value=Textbox1.text
cmdInsert.Parameters.Add("@serial_number",Data.SqlDbType.nvarchar).value=Textbox2.text
cmdInsert.Parameters.Add("@trade_mark",Data.SqlDbType.nvarchar).value=Textbox3.text
cmdInsert.Parameters.Add("@callibration_date",Data.SqlDbType.nvarchar).value=Textbox4.text
cmdInsert.Parameters.Add("@class",Data.SqlDbType.nvarchar).value=Textbox5.text
cmdInsert.Parameters.Add("@category",Data.SqlDbType.nvarchar).value=Textbox6.text
cmdInsert.Parameters.Add("@subcategory",Data.SqlDbType.nvarchar).value=Textbox7.text


cmdInsert.ExecuteNonQuery()
cn.close


Cheers,
Edo
 
Share this answer
 
v2
Comments
ridoy 9-Sep-13 13:00pm    
5ed!
Joezer BH 10-Sep-13 2:01am    
Tx!
VB
Imports System.Data
Imports System.Data.OleDb

Public Class Form1
    Dim s, ins As String
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        Dim con As New OleDbConnection(s)
        con.Open()
        Dim cmd As New OleDbCommand
        ins = "insert into stuinfo values('" & TextBox1.Text & "','" & TextBox2.Text & "','" & ComboBox1.Text & "','" & ComboBox2.Text & "','" & DateTimePicker1.Value.Date & "', '" & TextBox4.Text & "')"
     
        cmd.CommandText = ins
        cmd.Connection = con
        cmd.ExecuteNonQuery()
        
        cmd.Dispose()
        MsgBox("success...................")
        con.Close()

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
         s = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Dell\Desktop\dell lab\cr\cr\bin\Debug\crdata.mdb"
       
    End Sub
end class

this may help u
 
Share this answer
 
v3
It depends on what type of data base you are using i.e MS Access, SQL Server, Oracle and so on.
Because depend on that your connection String and some part of code will change.
To refer Connection Strings for different types of data base see the following link :
http://www.connectionstrings.com/[^]
Code Example to Insert Data In SQL Database :
VB
Dim _objConnection As SqlClient.SqlConnection
_objConnection = New SqlClient.SqlConnection("Your Connection String goes here")
_objConnection.Open()
Dim _mCommand As String
_mCommand = "Insert Into TableName(Column1, Column2) Values (@Column1, @Column2)"
Dim _objCommand As New SqlClient.SqlCommand(_mCommand, _objConnection)
_objCommand.Parameters.AddWithValue("@Column1", "Value To Insert")
_objCommand.Parameters.AddWithValue("@Column2", "Value To Insert")
_objCommand.ExecuteNonQuery()
_objConnection.Close()
_objCommand.Dispose()

Code Exaple To Insert Data Into Access Database :
VB
Dim _objConnection As OleDb.OleDbConnection
_objConnection = New OleDb.OleDbConnection("Your Connection String goes here")
_objConnection.Open()
Dim _mCommand As String
_mCommand = "Insert Into TableName(Column1, Column2) Values (@Column1, @Column2)"
Dim _objCommand As New OleDb.OleDbCommand(_mCommand, _objConnection)
_objCommand.Parameters.AddWithValue("@Column1", "Value To Insert")
_objCommand.Parameters.AddWithValue("@Column2", "Value To Insert")
_objCommand.ExecuteNonQuery()
_objConnection.Close()
_objCommand.Dispose()

For other type of database Google is there. :)
I hope it will help you. :)
 
Share this answer
 
v2
VB
imports system.data.oledb


Public Class Form1

dim cnn as new oledbconnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/database directory")
dim cmd as oledbcommand
dim Sql as String 
dim da as new oledbDataAdapter
dim ds as DataSet

sub form1_load


cnn.open()

da= new oledbDataAdapter(Sql,cnn)
da.fill(Sql."the table name")
cnn.close()

end sub
sub btn insert
sql=insert into tables/or tabel1,table2 values( & TextBox1.Text & "','" & TextBox2.Text & "')
end sub




'that as much i could help  
 
Share this answer
 
Интересная статья! С огромным уважением, Ваш давний читатель Любомир Валериевич Верстак из города Плехово. Завтра снова зайду. Пока!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900