.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> ADO.NET >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Insert a record into the database using C# code

Posted By:Karthikeyan Anbarasan       Posted Date: January 23, 2011    Points: 15    Category: ADO.NET    URL: http://www.dotnetspark.com  

Insert a record into the database using C# code
 

    private const string CONNECTSTRING = "Provider=Microsoft.Jet.OLEDB.4.0;Data source =";
    private string dbName;
    private string connectString;
 
 public int InsertDatatoDB(string sqlCommandString)
    {
        int flag = 1;
        OleDbConnection myDBObj = new OleDbConnection();
        OleDbCommand command;

        connectString = CONNECTSTRING + dbName;

        try
        {
            myDBObj.ConnectionString = connectString;          // Initialize
            myDBObj.Open();                                    // Open DB
            command = new OleDbCommand(sqlCommandString, myDBObj);   // Set the command
            command.ExecuteNonQuery();                      // Create new table
        }
        catch (Exception ex)
        {
            flag = 0;
        }
        finally
        {
            myDBObj.Close();                                   // Close it
        }
        return flag;
    }

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend