insert into Database C#,

I try to insert something into database (SQL server 2008). this is what I did so far.

could someone tell me does  my connection string is correct? and what is the next step that actual execute the "insert" to database

thanks in advance

OleDbConnection cn_input = new OleDbConnection();
string ConnStr_input = "Provider = SQLNCLI10; Server = (local); Database = Pb_project; Integrated Security=SSPI; DataTypeCompatibility=80; MARS Connection=True;";

cn_input.ConnectionString = ConnStr_input;

string insert_db = "INSERT INTO [value_tmp] (subject_ID,location,value) VALUES (2013,'London','Tokyo')";

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AshokSr. Software EngineerCommented:
Do you have to use OleDBConnection?

If you are using MS SQL Server 2008, I would suggest you use SQLServerConnection.

Let me know which one you want to use.

There are various ways to do this, but this is just a quick one. This may help you...

        //use System.Data.SqlClient SqlConnection instead of OleDbConnection

        //Depending on your database your connnection string may need to be in different formats
        //You should be able to figure out what your connection string looks like by going to
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = "Data Source=ServerName;Initial Catalog=DatabaseName;Integrated Security=True";

        string insert_db = "INSERT INTO [value_tmp] (subject_ID,location,value) VALUES (2013,'London','Tokyo')";
        SqlCommand cmd = new SqlCommand(insert_db, conn);

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AshokSr. Software EngineerCommented:
using System.Data.SqlClient;


SqlConnection conn = new SqlConnection();
string sConnStr = "Data Source=localhost;Initial Catalog=DBATCH;Persist Security Info=True;User ID=user1;Password=myPassword";
conn.ConnectionString = sConnStr;
// assign values to sDestination, sPAX & sPrice (in string variables)
string sSQL = "INSERT into tblTravelInfo (Destination,  PAX, Price) " +
              "VALUES('" + sDestination + "',  " + sPAX + ", " + sPrice + ");"
SqlCommand cmd = new SqlCommand(sSQL);

// Standard Security Connection String
sConnStr = "Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;"

// Trusted Connection
sConnStr = "Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;"

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.