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.
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
'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
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(@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
'now close the connection
public class GrossmontBank
//class variables (global - accessible throughout this class)
//scanner object to be used throughout
private static Scanner input = new Scanner(System.in);