views:

223

answers:

6

Hello: I am teaching myself Linq to Sql in C#. Because I am using a SqlCE database I had to use SqlMetal to generate the dbml file. This went fine, and I added the dbml file to my program. I can't find out how to generate a DataContext for the database, but I can query the database, but can not insert a row. Here is an example of what does not work:

Journal is the Database, Exercise is the only table in the database.

string con = Properties.Settings.Default.JournalConnectionString;
            Journal db = new Journal(con);

            Exercise ne = new Exercise();

            ne.Date = Convert.ToDateTime("2009-10-25T14:35:00");
            ne.Length = Convert.ToDouble(3.0);
            ne.Elapsed = "00:53:35";

            db.SubmitChanges();

Can someone suggest what I am doing wrong with the insert? Thank you very much.

+2  A: 

You never invoked DataContext.InsertOnSubmit:

db.Exercises.InsertOnSumbit(ne);
db.SubmitChanges();

If you want to insert multiple Exercises use DataContext.InsertAllOnSubmit:

// exercises is IEnumerable<Exercise>
db.Exercises.InsertAllOnSubmit(exercises);
db.SubmitChanges();
Jason
+1 @Jason, I keep forgetting they changed Add to InsertOnSubmit
Stan R.
A: 

You should add the line

db.Exercises.InsertOnSubmit(ne);

before doing SubmitChanges.

Paul
+2  A: 

You forgot to add the record, to the table.

db.Exercises.InsertOnSubmit(ne);

Since you are learning this, its also a good idea to use using

       using(Journal db = new Journal(con))
       {

          Exercise ne = new Exercise();

          ne.Date = Convert.ToDateTime("2009-10-25T14:35:00");
          ne.Length = Convert.ToDouble(3.0);
          ne.Elapsed = "00:53:35";

          db.Exercises.InsertOnSubmit(ne);  //add this line to add rec to table
          db.SubmitChanges();
        }
Stan R.
I redid the SqlMetal commands /dbml and /code, and it worked. Thanks for the answer, and the tip on using.
Mark W
A: 

You need to tell the context to insert your object. For example:

db.Exercises.InsertOnSubmit(ne);
db.SubmitChanges();
Scott Anderson
A: 

To generate the DataContext, you need to run two different commands with SqlMetal.exe.

Here's a batch file I've been using that will hopefully help you out:

set tool_path="C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\sqlmetal"
set project_root="C:\Documents\...\Solution\Your_Project_Name"

%tool_path% /server:.\SQLEXPRESS /database:Your_Database_Name /dbml:%project_root%\Models\Your_Dbml_Name.dbml
if errorlevel 1 goto BuildEventFailed

%tool_path% /server:.\SQLEXPRESS /database:Your_Database_Name /language:csharp /namespace:Your_Namespace.Models /code:%project_root%\Models\Your_DataContext_Class_Name.cs
if errorlevel 1 goto BuildEventFailed

goto BuildEventOK

:BuildEventFailed
exit 1

:BuildEventOK

You'll need to modify .\SQLEXPRESS to map to your database server.

The problem with your insert attempt is that you're creating the object you want to insert, but you're not actually telling LinqToSql to insert it. You need to call InsertOnSubmit() on your data context.

DanM
I did this, but I still do not have a DataContext. I don't know if this is because of the Compact Edition SQL or what.
Mark W
+1  A: 

May want to check out PLINQO as well. It really adds a LOT of features to LINQ to SQL and takes away a lot of the pain points.

http://www.plinqo.com/

Eric J. Smith