Good day!.
I have here a SQL database named Database1 and it has a tables (StudentID, Student name, Age, Gender,)
vb.net
and i want that if i input a records in vb.net it automatic save to sql database.
Help me please.

Recommended Answers

All 4 Replies

hericles has provided a perfect link but mind you that your connectionstring may not be the same as shown in the example. Please see here for more on connectionstrings

hello !
you want to insert records in db , there are some steps you have to follow.
1-you have to import class at the top of your form .here is the code

import system.data.sqlclient
'NOTE:please type this code at the top .

after doing this , you have to understand little bit about the components you will use for inserting records.
--SqlConnection - it is used to connect your application with your database.
--sqlCommand - Represents a Transact-SQL statement or stored procedure to execute against a SQL Server database.
--Connection String -A connection string is a string version of the initialization properties needed to connect to a data store and enables you to easily store connection information within your application or to pass it between applications.
now here is code and step by step description .

'after importing system.data.sqlclient
'we have to connect our application with db , for this we use connection string ,
'Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;
'above is a connection string 
'now start connection
dim myCon as new sqlconnection("Data Source=waqas/sqlexpress;Initial Catalog=myDataBase;Integrated Security=SSPI;")
'here you have to give your server , and your database name ,as i give.
'after connection there are two states of your connection , a open state and close state.
'now we make a insert command to insert records in database.
dim cmd as new sqlcommand
'now set your connection open 
myCon.open()
cmd.connection = myCon
'here we make a command
cmd.commandtext = "insert into table (StudentID,StudentName,Age,Gender) valuse (@StudentID,@StudentName,@Age,@Gender)"
'now give values you want to insert like this
cmd.Parameters .AddWithValue(@StudentID,val(txtStudentid.text))'NOTE:always use val() when you are dealing with integer , decimal, in this case i assume that we have datatype of studentid field in database is int.
cmd.Parameters .AddWithValue(@StudentName,txtStudentName.text)
cmd.Parameters .AddWithValue(@Age,txtAge.text)
cmd.Parameters .AddWithValue(@Gender,txtGender.text)'i assume that student name , age and gender are varchar datatype in my database so i not use val() here
'now execute this query to insert your records
cmd.ExecuteNonQuery()
'now close the connection
myCon.Close()

this code will insert your records in your database ,hope this will helps you to understand the whole procedure of insertion in database , you can further find all information about sqlconnection here http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.aspx and about sqlcommand here http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.aspx and about connection string from here http://www.connectionstrings.com and from here http://msdn.microsoft.com/en-us/library/windows/desktop/ms722656(v=vs.85).aspx .
hope this will helps you and others , if you find any mistake init please do tell me .

Best Regards

commented: so nice of work +0

thank you so much!. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.