Click here to Skip to main content
15,921,841 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am looking to create an application which connects to SQL Server. How can I do this?
Posted
Updated 16-May-10 9:44am
v2

You learn to not SHOUT and read. Connecting to a database is in every beginners C# book and all over the internet.
 
Share this answer
 
Hello,
I feel this code must solve your query
using System.Data.SqlClient; //use this namespace

SqlConnection con = new SqlConnection();
//setting the connection string property
con.ConnectionString = "Data Source=.;Initial Catalog=RJIT;Integrated Security=True";
//open the connection
                    con.Open();
//query
string mycommand = "insert into receipt values(@student_id,@amount_paid,@student_name)";

       SqlCommand cmd = new SqlCommand(mycommand, con);
                    
 cmd.Parameters.AddWithValue("@student_id", comboBox1.Text);
 cmd.Parameters.AddWithValue("@amount_paid", txtreceiptstudamount.Text);
  cmd.Parameters.AddWithValue("@student_name", txtreceiptstudname.Text);

                    int x = cmd.ExecuteNonQuery();
                    if (x != 0)
                    {
//show message box when data is inserted 
                       MessageBox.Show("Data added");
                                  

                    }
//close the connection
                    con.Close();

Do rate my answer once you find it useful
Thanks & Regards
Radix :rose:
 
Share this answer
 
SqlConnection your connection name = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\your Database_name.mdf;Integrated Security=True;User Instance=True");




this is the connection string for sql server database 2005
 
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