Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL Insert

Status
Not open for further replies.

wwechopper1

Programmer
Feb 19, 2007
5
US
What SQLCommand method executes an SQL Insert statement?
 
Have a look at this:
You have not really given enough information to get an answer here, most importantly how are you connecting the database?

By the way, this was the second result from Google for the search string 'SQL insert C#'.

Hope it helps,

Alex

Ignorance of certain subjects is a great part of wisdom
 
There are many ways of doing it (client side, stored procedure, etc.).

Here's one way, though not best (SQL injection if you're using web). Add try/catch to trap errors.

Code:
SqlConnection oConn = new SqlConnection();
            oConn.ConnectionString = "your database/logon credentials";
            oConn.Open();

            SqlCommand cmd = new SqlCommand();
            cmd.Connection = oConn;
            cmd.CommandText = "insert into myTable some data";
            cmd.ExecuteNonQuery();
 
mjjks,

Sorry, Im new at this. How do i change what table the code is accessing. and do i have to change anything in this line

cmd.CommandText = "insert into myTable some data";
 
You want to replace "insert into myTable some data" with the SQL statement you want to execute.



Ignorance of certain subjects is a great part of wisdom
 
Like I said, im new at this, what would the SQL Statement be?
Thanks
 
The SQL Statement to insert to your table? Something like "insert into myTable some data" only a little more like "insert into myTable select \"" + Convert.ToString(formControl.Value) + "\""



Ignorance of certain subjects is a great part of wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top