Click here to Skip to main content
15,889,034 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to insert data in sql data base eg.insert into tablename(name) values('mahesh') where id=102; how to fire such query in c# .net
Posted
Updated 27-Apr-11 9:48am
v2

I hope that my code will helpful with you.
Please modify it to your requirement.

#region  Hàm thêm Product vào chi tiết order
    public static void InsertCTOrder(int OrderID, int ProID, int Quantity, string UnitPrice,int ktra,SqlConnection sqlconn)
    {
        string strInSert = "INSERT INTO OderDetail VALUES (@OrderID, @ProID, @Quantity, @UnitPrice)";
        SqlCommand sqlcmd = new SqlCommand(strInSert, sqlconn);
        sqlcmd.Parameters.AddWithValue("@OrderID", OrderID);
        sqlcmd.Parameters.AddWithValue("@ProID", ProID);
        sqlcmd.Parameters.AddWithValue("@Quantity", Quantity);
        sqlcmd.Parameters.AddWithValue("@UnitPrice", UnitPrice);
        try
        {
            sqlconn.Open();
            sqlcmd.ExecuteNonQuery();
            sqlconn.Close();
            ktra = 0;
        }
        catch (Exception )
        {
            sqlconn.Close();
            ktra = 1;           
        }
    }
 
Share this answer
 
See http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcommand.executenonquery.aspx[^]

This is a very basic a question. If you google for "C# database insert" or "C# ole db" you'll get lots of hits.

[Edit]
~~~~~~~~~~~

This is in response to your comment. See this article:

OLE DB - First steps[^]

I wrote the first version almost 10 years ago and the code syntax uses the now obsolete MC++ syntax, but it should give you some ideas.

Also, see:
http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson03.aspx[^]
 
Share this answer
 
v3
Comments
Member 7685573 27-Apr-11 16:00pm    
it is not getting what i want
Nish Nishant 27-Apr-11 16:06pm    
See my updated answer.
walterhevedeich 27-Apr-11 21:12pm    
5 for the informative links, and for the patience to the OP. :)
Nish Nishant 28-Apr-11 8:07am    
Thanks :-)

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