使用LINQ将数据插入数据库

8xiog9wr  于 2023-01-18  发布在  其他
关注(0)|答案(3)|浏览(178)

我写了一个非常简单的方法。它把类DayWeather中的数据保存到数据库中。方法检查表中是否存在当天的行,并更新它或创建一个新行。
我通过为LINQ添加一个新的类,并将表从服务器检查器移动到构造函数中来实现。它会生成一个新的类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);
    }

它写着,测试已经成功通过。但是如果查看表格,它没有改变。没有错误信息显示。有人知道问题是什么吗?
另外,抱歉我的英语很差。

UDP问题在于:“... db可能会在每次构建时复制到调试或发布文件夹,覆盖您修改的文件夹”。谢谢@Silvermind

euoag5mw

euoag5mw1#

我写了一个简单的方法来保存员工的详细信息到数据库.

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

pgpifvop2#

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

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

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

希望它能起作用。

nwsw7zdq

nwsw7zdq3#

链接到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!");

        }

相关问题