开发者

LinQ to SQL : InvalidOperationException in a Windows Service

开发者 https://www.devze.com 2022-12-10 17:12 出处:网络
I have implemented a small Windows Service which runs every 9 minutes and writes the data which comes througth a webservice to the db.

I have implemented a small Windows Service which runs every 9 minutes and writes the data which comes througth a webservice to the db.

Do do the db work I use Linq To SQL

using (var db = new DataClasses1DataContext())
            {
  开发者_开发百科              var currentWeather = this.GetWeatherData();

                //////TODO Add the data correct

                var newEntry = new WeatherData()
                {
                    test = currentWeather.dateGenerated.ToShortTimeString()
                };

                //var test = db.WeatherDatas.First();

                db.WeatherDatas.InsertOnSubmit(newEntry); // this throws Invalid Operation Exception
                db.SubmitChanges();
            }

Why does it throw this exception? the same codeblock in a console programm runs nice

alt text http://img687.imageshack.us/img687/7588/unbenanntxb.png


Have you set up the connection string in app.Config correctly?

IIRC, the default constructor on an L2S DataContext reads the connection string from the config file. If the connection string points to the the wrong database (or one that doesn't exist) you may very well receive an exception.

This could also explain why this piece of code works when executed in a different context.

0

精彩评论

暂无评论...
验证码 换一张
取 消