0

我正在将我的代码从 Visual Studio 2010 迁移到 2012 并确保所有代码都按预期运行。在此转换期间,我的 LINQ-to-SQL 示例程序在尝试将数据插入我的一个表时失败。以下是用户单击保存按钮时的代码:

public class DebugTextWriter : TextWriter
{
    public override void Write(char[] buffer, int index, int count)
    {
        Debug.Write(new String(buffer, index, count));
    }
    public override void Write(string value)
    {
        Debug.Write(value);
    }
    public override Encoding Encoding
    {
        get
        {
            return Encoding.Default;
        }
    }
}

private void SaveButton_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            //First we locate the 'orderViewSource' object that the user filled in
              CollectionViewSource orderViewSource =
              FindResource("orderViewSource") as CollectionViewSource;

            //Next we save the values entered in into a generic list (only 1 entry 
              here)
            List<Order> ordList = orderViewSource.Source as List<Order>;

            //Again, since there is only one entry, we take it and fill in a new row in
            //our 'Order' table
            Order ord = ordList.FirstOrDefault();

            //In order for the insert to work, you need to create an instance of the
            //LINQ to SQL class for that table and then call the insert method on the
            //'ord' container and then finally submit it.
            var ctx = new MyShopDataContext();
            ctx.Orders.InsertOnSubmit(ord);
            ctx.SubmitChanges();
            MessageBox.Show("Order Saved!");
        }
        catch (Exception)
        {
        }
    }

我的代码构建没有错误,也没有抛出异常。关于为什么这段代码不起作用的任何想法?

4

1 回答 1

0

我在这个问题上的错误......我应该更清楚:在正确研究这个网站后,我发现错误解决方案如下:

“尝试附加自动命名的数据库”错误

感谢所有发帖的人

于 2013-06-22T13:32:10.633 回答