tags:

views:

43

answers:

2

I am very new to c# and all I want to do to start off with in insert a row into an access database.

I am using Microsoft Visual C# express 2010, I have added a dataset an am unsure of where to go from here...

I have had a look on google etc.. and nothing worked for me :(

I have been trying with this code:

        DataRow newCustomersRow = timeDataSet.Tables["theTimes"].NewRow();
        newCustomersRow["ID"] = "1";
        newCustomersRow["User"] = "Test Name";
        newCustomersRow["LieuHours"] = "23";
        timeDataSet.Tables["theTimes"].Rows.Add(newCustomersRow);

There are only 3 colums in the table, it is for a bigger project that I am working on.

Please can some one help me?

A: 

In your dataset add a tableadapter, there you can define your insert/delete/update/select methods through the wizard. It let's you inserted a row like this:

    using (timeDataSetTableAdapter ta = new timeDataSetTableAdapter())
{
    ta.Insert(1, "Test Name", "23");
}

It does the same as you were trying to do.

Also, read this: http://msdn.microsoft.com/en-us/library/ms171935%28VS.80%29.aspx

Jeroen
A: 

I am now at this stage

        system.Data.OleDb.OleDbConnection conn = new
        System.Data.OleDb.OleDbConnection();
        System.Data.OleDb.OleDbDataReader reader = null;
        // TODO: Modify the connection string and include any
        // additional required properties for your database.
        conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
        @"Data source= C:\time.mdb";
        try
        {
            conn.Open();

            //OleDbCommand c = new OleDbCommand("insert into theTimes (ID, User, LienHours) values('1', 'test', 'test')", conn);
            //OleDbCommand c = new OleDbCommand("Select Top 1 User FROM theTimes", conn);
            //reader = c.ExecuteReader();
            //NameTxtBox.Text = reader.ToString();

            //c.ExecuteNonQuery();

            MessageBox.Show("That worked");
        }
        catch (Exception)
        {
            MessageBox.Show("Failed to connect to data source");
        }
        finally
        {
            conn.Close();
        }

But still cant get it to insert a row :(

Neil Harwood
This is no Forum. Please edit your question, add this and delete this answer.
Bobby