4
Reply

C# and Local Databases

Char Jaya

Char Jaya

Jun 4 2010 3:21 PM
2.8k

Hi, I am sort of new to C# when it comes to using databases with it. I am writing a little program and it requires a database that is local to the program. I'm using VS2008 and the application uses .NET 3.5 framework.
So I added a Service-based Database (I called it Programs.mdf) to my solution and it automatically added a dataset as well. (ProgramsDataSet.xsd). Then I created a table in this database from the Server Explorer view (tblPrograms).
I was able to bind the info from the tblPrograms into a DataGrid. What I am now trying to do is add new rows to tblPrograms. I have tried many things and nothing as worked. Also I dont really want to use query strings to manipulate the database. When I bound the tblPrograms into the DataGrid, it created a TableAdapter. I want to use this TableAdapter to update, add and delete data from the database. I have tried a couple of different ways to but nothing has worked:
First Way

 
private void button1_Click(object sender, EventArgs e)
{
   this.tblProgramsTableAdapter.Insert("test cat", "test name");
   this.tblProgramsTableAdapter.Update(this.programsDataSet.tblPrograms);
}

Second Way
 

private void button1_Click(object sender, EventArgs e)
{
    SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM tblPrograms", this.tblProgramsTableAdapter.Connection);
    SqlCommandBuilder sqlCmd = new SqlCommandBuilder(sda);
    DataSet ds = new DataSet();
    sda.Fill(ds);
    DataRow dr = ds.Tables[0].NewRow();
    dr["Cat"] = "test cat";
    dr["Name"] = "test name";
    ds.Tables[0].Rows.Add(dr);
    sda.Update(ds);
}

Both of these sections of code manipulates the code in the TableAdapter. I mean like, it does add rows, delete and update data but I am not able to push the changes made in the TableAdapter into the database. The data in the database itself does not get updated. Can someone please help me with this issue? This has slowed down my development of this piece of software to almost a halt.
Thanks very much in advanced.
- Char

Answers (4)