如何在 Linq to SQL 中确定记录已成功添加?

发布于 2024-11-01 08:20:14 字数 142 浏览 1 评论 0原文

我刚刚学习Linq to SQL,了解不多。请指导和帮助我。

我正在使用 SubmitChanges() 添加一条新记录;如何确认记录已添加?例如,在使用存储过程时,我们将一个标志发送回应用程序,但在 LINQ to SQL 中是如何完成的呢?请指导我。

I am just learning Linq to SQL and don't know much. Kindly guide and help me.

I am adding a new record by using a SubmitChanges(); How I can be confirmed that record has been added? For example, in using stored procedures we use to send a flag back to application but how is it done in LINQ to SQL? Please guide me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

╭ゆ眷念 2024-11-08 08:20:14

让您的代码流出您的方法。只有当抛出异常时,你的语句才算完成。

如果您想要一个“标志”,您可以返回一个布尔值。

public bool AddCustomer()
{
  try{
     ....
     db.SubmitChanges();
     return true;
  }
  catch(Exception e)
  {
    ...
    return false;
  }
}

Allow your code to flow out of your method. Only when an exception is throw has your statement not completed.

If you wanted a 'flag', you could return a bool.

public bool AddCustomer()
{
  try{
     ....
     db.SubmitChanges();
     return true;
  }
  catch(Exception e)
  {
    ...
    return false;
  }
}
放飞的风筝 2024-11-08 08:20:14

你可以做这样的事情;

public void Save()
{
    Northwnd db = new Northwnd(@"c:\northwnd.mdf");
    // Make changes here. 
    try
    {
        db.SubmitChanges();
    }
    catch (Exception err)
    {
        //Log error
    }
}

如果没有抛出异常,您可以假设数据保存正确。虽然另一种选择是这样的。

public bool Save()
{
    Northwnd db = new Northwnd(@"c:\northwnd.mdf");
    // Make changes here. 
    try
    {
        db.SubmitChanges();
        return true;
    }
    catch (Exception e)
    {
        //Log the error
        return false;
    }
}

保存成功则返回true,否则返回false

You could do something like this;

public void Save()
{
    Northwnd db = new Northwnd(@"c:\northwnd.mdf");
    // Make changes here. 
    try
    {
        db.SubmitChanges();
    }
    catch (Exception err)
    {
        //Log error
    }
}

If no exception is thrown you can assume the data is saved correct. Though another option is something like this.

public bool Save()
{
    Northwnd db = new Northwnd(@"c:\northwnd.mdf");
    // Make changes here. 
    try
    {
        db.SubmitChanges();
        return true;
    }
    catch (Exception e)
    {
        //Log the error
        return false;
    }
}

True will be returned if save succeeds, otherwise false will be returned

泪意 2024-11-08 08:20:14

请参阅:MSDN,如何:将更改提交到数据库< /a>

请注意,存在 的重载SubmitChanges() 可以让您指定如何处理冲突。

See: MSDN, How to: Submit Changes to the Database

Note that there is an overload of SubmitChanges() that will let you specidy how to handle conflicts.

临风闻羌笛 2024-11-08 08:20:14

检查此 http://msdn.microsoft.com/en-us/library/bb399378 .aspx

此时,数据库检测到的任何错误都会导致提交过程停止,并引发异常。对数据库的所有更改都会回滚,就像从未发生过提交一样。 DataContext 仍然完整记录了所有更改。因此,您可以尝试纠正问题并再次调用 SubmitChanges

Northwnd db = new Northwnd(@"c:\northwnd.mdf");
// Make changes here. 
try
{
    db.SubmitChanges();
}
catch (ChangeConflictException e)
{
    Console.WriteLine(e.Message);
    // Make some adjustments.
    // ...
    // Try again.
    db.SubmitChanges();
}

Check this http://msdn.microsoft.com/en-us/library/bb399378.aspx

At this point, any errors detected by the database cause the submission process to stop, and an exception is raised. All changes to the database are rolled back as if no submissions ever occurred. The DataContext still has a full recording of all changes. You can therefore try to correct the problem and call SubmitChanges again

Northwnd db = new Northwnd(@"c:\northwnd.mdf");
// Make changes here. 
try
{
    db.SubmitChanges();
}
catch (ChangeConflictException e)
{
    Console.WriteLine(e.Message);
    // Make some adjustments.
    // ...
    // Try again.
    db.SubmitChanges();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文