How to 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;
    }
Ebook Download
View all
Learn
View all