0% found this document useful (0 votes)
6 views

How To Add To Database

Uploaded by

Altaf Hussain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

How To Add To Database

Uploaded by

Altaf Hussain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

//1.

Address of SQL server and Database

string ConnectionString = @"Data Source=(LocalDB)\


MSSQLLocalDB;AttachDbFilename=C:\Users\A R F T\OneDrive\Desktop\MY DATABASE\
MyDb.mdf;Integrated Security=True;Connect Timeout=30";

//2. Estabish Connection

SqlConnection con = new SqlConnection(ConnectionString);

//3. Open Connection

con.Open();

//4. Prepare Query


string FirstName=textBox1.Text;
string SecondName=textBox2.Text;
string Query = "INSERT INTO Names(FirstName,SecondName) VALUES
('"+FirstName+"','"+SecondName+"')";

//5. Execute Query

SqlCommand cmd = new SqlCommand(Query, con);


cmd.ExecuteNonQuery();

//6. Close Connection


con.Close();

MessageBox.Show("Data Has Been Saved!");


textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";

You might also like