Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我的LINQ到SQL插入代码失败_C#_Linq To Sql_Visual Studio 2012 - Fatal编程技术网

C# 我的LINQ到SQL插入代码失败

C# 我的LINQ到SQL插入代码失败,c#,linq-to-sql,visual-studio-2012,C#,Linq To Sql,Visual Studio 2012,我正在将我的代码从VisualStudio2010迁移到2012,并确保所有代码都按预期运行。在此转换过程中,我的LINQ到SQL示例程序在尝试将数据插入我的一个表时失败。以下是用户单击“保存”按钮时的代码: public class DebugTextWriter : TextWriter { public override void Write(char[] buffer, int index, int count) { Debug.Write(new Str

我正在将我的代码从VisualStudio2010迁移到2012,并确保所有代码都按预期运行。在此转换过程中,我的LINQ到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)
        {
        }
    }
public类DebugTextWriter:TextWriter
{
公共重写无效写入(字符[]缓冲区、整数索引、整数计数)
{
Write(新字符串(缓冲区、索引、计数));
}
公共重写无效写入(字符串值)
{
Debug.Write(值);
}
公共覆盖编码
{
得到
{
返回编码。默认值;
}
}
}
私有无效保存按钮\u单击(对象发送方,路由目标)
{
尝试
{
//首先,我们找到用户填写的“orderViewSource”对象
CollectionViewSource订单ViewSource=
FindResource(“orderViewSource”)作为CollectionViewSource;
//接下来,我们将输入的值保存到通用列表中(仅1个条目)
这里)
List ordList=orderViewSource.Source作为列表;
//同样,因为只有一个条目,所以我们接受它并在
//我们的“订单”表
Order ord=ordList.FirstOrDefault();
//为了使插入生效,您需要创建
//将该表的LINQ转换为SQL类,然后在
//“ord”容器,然后最终提交。
var ctx=new MyShopDataContext();
ctx.Orders.InsertOnSubmit(ord);
ctx.SubmitChanges();
MessageBox.Show(“订单已保存!”);
}
捕获(例外)
{
}
}

我的代码构建没有错误,也没有引发异常。有没有想过为什么这段代码不起作用?

我在这个问题上的错误…我应该知道得更清楚:在正确研究了这个网站后,我发现错误解决方法如下:


感谢所有发帖的人

你怎么知道没有抛出异常?你吞下了任何可能从该块中产生的东西。orderViewSource中的order不是已经存在了吗?@Jay-问得好,我在数据处理方面的新手状态显示在这里,但当我在debug中运行时,它会直接流到“order Saved!”而且永远不会转到捕获行。@OndrejSvejdar-我试图用这段代码添加的顺序是唯一的,而不是重复的如果你不知道如何使用探查器,你可以像这样记录linq到sql查询-创建类public class DebugTextWriter:TextWriter{public override void Write(char[]buffer,int index,int count){Debug.Write(新字符串(缓冲区、索引、计数));}public override void Write(字符串值){Debug.Write(值);}public override Encoding Encoding{get{return Encoding.Default;}}}}}并将ctx.Log设置为此类的实例。在调试中,它将显示正在执行的sql命令。