使用LINQ将数据插入数据库

时间:2013-05-05 19:16:17

标签: c# sql linq

我写了一个非常简单的方法。它将类DayWeather中的数据保存到数据库中。方法检查表中是否存在当天的行并更新她或创建新行。

我是通过为LINQ添加新类并将表从Server Inspector移动到构造函数来实现的。它会生成新的类WeatherTBL

方法本身如下:

public static void SaveDayWeather(DayWeather day)
    {
        using (DataClassesDataContext db = new DataClassesDataContext())
        {
            var existingDay =
              (from d in db.WeatherTBL
               where d.DateTime.ToString() == day.Date.ToString()
               select d).SingleOrDefault<WeatherTBL>();
            if (existingDay != null)
            {
                existingDay.Temp = day.Temp;
                existingDay.WindSpeed = day.WindSpeed;
                existingDay.Pressure = day.Pressure;
                existingDay.Humidity = day.Humidity;
                existingDay.Cloudiness = day.Cloudiness;
                existingDay.TypeRecip = day.TypeRecip;
                db.SubmitChanges();
            }
            else
            {
                WeatherTBL newDay = new WeatherTBL();
                newDay.DateTime = day.Date;
                newDay.Temp = day.Temp;
                newDay.WindSpeed = day.WindSpeed;
                newDay.Pressure = day.Pressure;
                newDay.Humidity = day.Humidity;
                newDay.Cloudiness = day.Cloudiness;
                newDay.TypeRecip = day.TypeRecip;
                db.WeatherTBL.InsertOnSubmit(newDay);
                db.SubmitChanges();
            }
        }
    }

当我试图从UnitTest项目中打电话给他时:

  [TestMethod]
    public void TestDataAccess()
    {
        DayWeather day = new DayWeather(DateTime.Now);
        DataAccessClass.SaveDayWeather(day);
    }

它写道,该测试已成功通过。但如果看看表,它就没有了。 没有显示错误消息。有谁知道这个问题是什么?

P.S。抱歉我的英语不好。

UDP 问题在于: “... db可能会在每次构建时复制到debug或release文件夹,覆盖你修改过的文件夹”。谢谢@Silvermind

3 个答案:

答案 0 :(得分:5)

我编写了简单的方法将员工详细信息保存到数据库中。

Checkout this link to know Insert, Update, Delete operations using LINQ C#

     private void AddNewEmployee()
     {
        using (DataContext objDataContext = new DataContext())
        {                
            Employee objEmp = new Employee();
            // fields to be insert
            objEmp.EmployeeName = "John";
            objEmp.EmployeeAge = 21;
            objEmp.EmployeeDesc = "Designer";
            objEmp.EmployeeAddress = "Northampton";                
            objDataContext.Employees.InsertOnSubmit(objEmp);
            // executes the commands to implement the changes to the database
            objDataContext.SubmitChanges();
        }
      }

Getting started with LINQ C#

答案 1 :(得分:0)

请尝试使用lambda表达式。在您的代码中, var existingDay 的类型为 IQueryable 要插入或更新,您需要 WeatherTBL 类型的变量 var existingDay 。 因此请尝试使用以下..

 var existingDay =
 db.WeatherTBL.SingleOrDefault(d => d.DateTime.Equals(day.Date.ToString()));

 if(existingDay != null)
  {
   //so on...
  }

希望它能够发挥作用..

答案 2 :(得分:0)

Linq to SQL

        Detail tc = new Detail();


        tc.Name = txtName.Text;
        tc.Contact = "92"+txtMobile.Text;
        tc.Segment = txtSegment.Text;
        var datetime = DateTime.Now;
        tc.Datetime = datetime;
        tc.RaisedBy = Global.Username;
        dc.Details.InsertOnSubmit(tc);

        try
        {


            dc.SubmitChanges();
            MessageBox.Show("Record inserted successfully!");
            txtName.Text = "";
            txtSegment.Text = "";
            txtMobile.Text = "";

        }


        catch (Exception ex)
        {
            MessageBox.Show("Record inserted Failed!");

        }