【问题标题】:Linq Syntax: SubmitChanges() method not effecting on database but just the DataContextLinq 语法:SubmitChanges() 方法不影响数据库,而只影响 DataContext
【发布时间】:2021-09-04 11:49:03
【问题描述】:

尝试将我的更改保存在我的数据库表 EmpTable 中时,数据库中没有发生任何提交。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Linq;

namespace ConsoleApplication_
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Working Fine");

            DataBase1ClassDataContext dc = new DataBase1ClassDataContext(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True");
            Table<EmpTable> t = dc.EmpTables;
    
            foreach (var i in t)
            {
                Console.WriteLine(i.Id+" "+i.name+" "+i.Address+" "+i.contact+" "+i.GetType());
            }
            List<EmpTable> l = dc.EmpTables.ToList();
            for (int j = 0; j < l.Count(); j++)
            {
                Console.WriteLine(l[j].Id + " " + l[j].name + " " + l[j].Address + " " + l[j].contact + " " + l[j].GetType());
            }
            label1:
            Console.WriteLine("1 for inserting data, 2 for exit");
            int a = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine();
            EmpTable obj = new EmpTable();
            switch (a)
            {
                case 1:
                    {
                        Console.Write("Enter a new unique ID: ");
                        obj.Id = Convert.ToInt32(Console.ReadLine());
                        Console.WriteLine("Your new id is : "+obj.Id);
                        Console.Write("Enter name: ");
                        obj.name = Console.ReadLine();
                        Console.Write("Enter address: ");
                        obj.Address = Console.ReadLine();
                        Console.Write("Enter contact no.: ");
                        obj.contact = Console.ReadLine();
                        dc.EmpTables.InsertOnSubmit(obj);
                        dc.SubmitChanges();
                        Console.WriteLine("INSERTED. . . :)");
                        dc = new DataBase1ClassDataContext();
                        t = dc.EmpTables;
                        foreach (var i in t)
                        {
                            Console.WriteLine(i.Id + " " + i.name + " " + i.Address + " " + i.contact + " " + i.GetType());
                        }
                        Console.WriteLine("To operate again,");
                        goto label1;
                    }
                case 2:
                    Console.WriteLine("Press any key to exit,\nExiting . . .");
                    break;
                default:
                    Console.WriteLine("Wrong Entry, Try Again");
                    goto label1;
            }
            Console.ReadKey();
        }
    }
}

每当我运行我的代码时,它只会显示我手动插入的表中当前的数据。

请帮忙。

重要提示:我的表中已经存在一个主键。

【问题讨论】:

  • 我已经 30 年没有见过任何人使用 goto 的代码了。我不确定它是否仍然存在。
  • 常见的 localdb 混淆。您的数据库在每次运行时都会被覆盖。不要使用AttachDbFilename,而是使用database=Database1
  • 在 VS 解决方案资源管理器中,您是否检查过 Database1.mdf 的“复制到输出目录”属性是“如果较新则复制”而不是默认的“始终复制”?
  • 这能回答你的问题吗? Why saving changes to a database fails?
  • 如何检查 Database1.mdf 中的设置“复制到输出目录”属性“如果较新则复制”而不是默认的“始终复制”

标签: c# sql-server linq linq-to-sql


【解决方案1】:

我得到了答案,通过更改我的 database1.mdf “复制到输出目录” 因为不复制,这个选项在数据库的属性中,然后探索了一个新错误,我通过给出完整路径来纠正它db 在配置中,因为它始终指向服务器资源管理器,而真正的 db 在解决方案资源管理器中。 十分感谢大家。 快乐编程?

【讨论】:

    猜你喜欢
    • 2019-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-11
    • 1970-01-01
    • 2011-12-30
    • 2013-08-01
    • 1970-01-01
    相关资源
    最近更新 更多