Mark W
Mark W

Reputation: 811

Linq to Sql problem

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.

Upvotes: 0

Views: 1813

Answers (6)

Eric J. Smith
Eric J. Smith

Reputation: 971

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/

Upvotes: 1

devuxer
devuxer

Reputation: 42354

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.

Upvotes: 0

Stan R.
Stan R.

Reputation: 16065

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();
        }

Upvotes: 2

Scott Arrington
Scott Arrington

Reputation: 12503

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

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

Upvotes: 0

Paul
Paul

Reputation: 36319

You should add the line

db.Exercises.InsertOnSubmit(ne);

before doing SubmitChanges.

Upvotes: 0

jason
jason

Reputation: 241641

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();

Upvotes: 2

Related Questions