0


I wrote very simple class, that perfom data access.
It checks if line with that day exist in table and update her or create a new line.

public class DataAccessClass
{
    public static DayWeather GetDayWeather(DateTime date)
    {
        try
        {
            using (var db = new Context())
            {
                var query =
                    (from day in db.DayWeather
                     where ((DateTime)day.DateOfDay).Date == date.Date
                     select new DayWeather((short)day.Temperature, (ushort)day.WindSpeed, (ushort)day.Pressure, (ushort)day.Humidity, day.Cloudiness, day.TypeRecip, (DateTime)day.DateOfDay)).First();
                return query;
            }
        }
        catch (Exception exp)
        {
            if (!EventLog.SourceExists("DataAccessSource"))
            {
                EventLog.CreateEventSource("DataAccessSource", "DataAccessErrorLog");
            }
            EventLog.WriteEntry("DataAccessSource", exp.Message);
            throw new Exception("Problem with data get.");
        }
    }

    public static void SaveDayWeather(DayWeather day)
    {
        try
        {
            using (var db = new Context())
            {
                var existingDay =
                  (from d in db.DayWeather
                   where ((DateTime)day.DateOfDay).Date == day.DateOfDay.Date
                   select d).SingleOrDefault<DayWeather>();
                if (existingDay != null)
                {
                    existingDay.Temperature = day.Temperature;
                    existingDay.WindSpeed = day.WindSpeed;
                    existingDay.Pressure = day.Pressure;
                    existingDay.Humidity = day.Humidity;
                    existingDay.Cloudiness = day.Cloudiness;
                    existingDay.TypeRecip = day.TypeRecip;
                    db.SaveChanges();
                }
                else
                {
                    DayWeather newDay = new DayWeather();
                    newDay.DateOfDay = day.DateOfDay;
                    newDay.Temperature = day.Temperature;
                    newDay.WindSpeed = day.WindSpeed;
                    newDay.Pressure = day.Pressure;
                    newDay.Humidity = day.Humidity;
                    newDay.Cloudiness = day.Cloudiness;
                    newDay.TypeRecip = day.TypeRecip;
                    db.DayWeather.Add(newDay);
                    db.SaveChanges();
                }
            }
        }

It use EF for generate database. The Contex class and class for save look like this:

public class DayWeather
{
    public short Temperature { get; set; }

    public ushort WindSpeed { get; set; }

    public ushort Pressure { get; set; }

    public ushort Humidity { get; set; }

    public string Cloudiness { get; set; }

    public string TypeRecip { get; set; }

    public DateTime DateOfDay { get; set; }

    public DayWeather(short Temperature, ushort WindSpeed, ushort Pressure, ushort Humidity, string Cloudiness, string TypeRecip, DateTime Date)
    {
        this.Temperature = Temperature;
        this.WindSpeed = WindSpeed;
        this.Pressure = Pressure;
        this.Humidity = Humidity;
        this.Cloudiness = Cloudiness;
        this.TypeRecip = TypeRecip;
        this.DateOfDay = Date;
    }

    public DayWeather()
    {

    }
}

internal class Context : DbContext
{
    public DbSet<DayWeather> DayWeather { get; set; }
}

I call this methods by this code:

DataAccessClass.SaveDayWeather(new DayWeather(12, 12, 12, 12, "Yes", "rain", DateTime.Now));
        DayWeather day = DataAccessClass.GetDayWeather(DateTime.Now);
        Console.WriteLine(day.ToString());
        Console.ReadKey();


It should generate new database, but error occurs. In message it write that can`t connect to the SQL Server.

Is somebody know what is wrong?
P.S. Sorry for my bad English.
P.P.S. I added EF by NuGet.

Sam Leach
  • 12,746
  • 9
  • 45
  • 73
Advers191
  • 19
  • 1
  • 1
  • 6
  • 3
    Is your connection string correct? Where is the database? Local? – Sam Leach May 10 '13 at 15:49
  • @SamLeach, I used this tutorial and it works without any connections strings, so i didn`t create her.http://msdn.microsoft.com/en-us/data/jj193542 – Advers191 May 10 '13 at 15:51
  • You have not overrided `ToString()` so `Console.WriteLine(day.ToString());` will not print what you expect. – Sam Leach May 10 '13 at 15:52
  • @SamLeach, He didn`t print anything, just throwing exeption when I try to use database. – Advers191 May 10 '13 at 15:53
  • See section `Where’s My Data?` of that article. SQL Express instance or LocalDb will be used. What is the exact error message? – Sam Leach May 10 '13 at 15:55
  • @SamLeach `When connecting to SQL Server error occurred related to the network or to a specific instance. The server was not found or is not available. Verify that the instance name is correct and that SQL Server to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error detection specified server or instance)` – Advers191 May 10 '13 at 15:58
  • what's your SQL Server version? – anouar.bagari May 10 '13 at 16:00

1 Answers1

0

You can manually specify the connection string as follows

using (var db = new Context("connectionString"))

The default constructor looks up a connection string in the web.config with the same name as the derived context class Context.

If it fails to find one it defaults to

Data Source=.\SQLEXPRESS;

or

Data Source=(LocalDb)\v11.0;

depending on the version of sql server you are using.

Sam Leach
  • 12,746
  • 9
  • 45
  • 73