Uploaded by dushi dushi

Steps to create a database connection

advertisement
Steps to create a database connection
1. Create the interface using visual studio C#
2. Open Microsoft SQL create new database  create new table  use the relevant
data fields (use a primary key)
3. Connect database with your interface
In visual studio;
Tools Connect database Select data source  Copy and paste the server name 
select database
4. Go to your form;
Add code lines to the beginning
Establish the connection under the form
Add button code to execute the query
Code lines to establish the connection
At the beginning of the code
using System.Data.SqlClient;
under the form
static string connectionstr=” copy and paste connection string here”;

Click on the dbase in solution explorer -> Copy the connection string from the properties
SqlConnection cnn=new SqlConnection(connectionstr);
Button code
cnn.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO student
VALUES(@nic,"+"@name,@age)",cnn);
cmd.Parameters.AddWithValue("@nic", txtbxnic.Text);
cmd.Parameters.AddWithValue("@name", txtbxname.Text);
cmd.Parameters.AddWithValue("@age", txtbxage.Text);
cmd.ExecuteNonQuery();
MessageBox.Show("New Record Added!", "Database Updated",MessageBoxButtons.OK,
MessageBoxIcon.Information);
cnn.Close();
Download