C# INSERT语句不添加任何数据而不引发错误

C# INSERT语句不添加任何数据而不引发错误,c#,sql-server,database,C#,Sql Server,Database,我试图将train对象添加到数据库中,以保存它们的详细信息,以便持久化 我有它的工作,所以我可以添加到一个列表的火车。但是,当我试图设置一个INSERT语句来将train对象的详细信息添加到数据库中时。之后我检查数据库时,没有向数据库中添加任何内容。我也不会在任何地方抛出任何错误 有人能看出我的INSERT语句有什么问题吗 //If the type combobox has Express selected if (cbxType.Text == "Expres

我试图将train对象添加到数据库中,以保存它们的详细信息,以便持久化

我有它的工作,所以我可以添加到一个列表的火车。但是,当我试图设置一个INSERT语句来将train对象的详细信息添加到数据库中时。之后我检查数据库时,没有向数据库中添加任何内容。我也不会在任何地方抛出任何错误

有人能看出我的INSERT语句有什么问题吗

        //If the type combobox has Express selected
        if (cbxType.Text == "Express")
        {
            //Create a new train with its specific details
            Train train = trainFactory.TFactory("Express");
            //Checks for error when making train
            if (train == null)
                MessageBox.Show("Can't Create Train");
            else //Executes adding a new Express Train
            {
                //Stores the details of the textboxes/Combo boxes into the train details for each Train object
                train.Type = cbxType.Text;
                train.Departure = cbxDepartStation.Text;
                train.Destination = cbxDepartStation.Text;
                //Converts the time into DateTime format before passing to variable
                train.DepartureTime = TimeSpan.Parse(txtDepartureTime.Text);
                //Converts the date into DateTime format before passing to variable
                train.DepartureDay = DateTime.Parse(txtDepartureDay.Text);
                //If intermediate stops are selected. Throw exception
                if (chbPeterborough.IsChecked == true || chbDarlington.IsChecked == true ||
                            chbYork.IsChecked == true || chbNewcastle.IsChecked == true)
                {
                    throw new Exception();
                }
                //If first class radio button is checked, sets first class to true, else false
                if (chbFirstClass.IsChecked == true)
                {
                    train.FirstClass = true;
                }
                else
                {
                    train.FirstClass = false;
                }

                //Adds a train object to the train list with its specific details
                trains.add(train);

                //String to hold all the Intermediate stops together in one for displaying to user
                string intStops = string.Join(", ", train.IntermediateStop.Where(s => !string.IsNullOrEmpty(s)));

                //Sql sequence to connect to database and insert details of each train
                SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Trains.mdf;Integrated Security=True");
                SqlCommand cmd = new SqlCommand();
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = "INSERT train (id, departure, destination, type, intermediate, departure_time, departure_date, sleeperBerth, firstClass) " +
                                  "VALUES ( @trainID , @departure, @destination, @type, @intermediate, @dep_time, @dep_date, @sleep, @first)";
                cmd.Parameters.AddWithValue("@trainID", train.TrainID);
                cmd.Parameters.AddWithValue("@departure", train.Departure);
                cmd.Parameters.AddWithValue("@destination", train.Destination);
                cmd.Parameters.AddWithValue("@type", train.Type);
                cmd.Parameters.AddWithValue("@intermediate", intStops);
                cmd.Parameters.AddWithValue("@dep_time", train.DepartureTime);
                cmd.Parameters.AddWithValue("@dep_date", train.DepartureDay);
                cmd.Parameters.AddWithValue("@sleep", train.SleeperBerth);
                cmd.Parameters.AddWithValue("@first", train.FirstClass);

                cmd.Connection = con;

                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
整个AttachDbFileName=方法是有缺陷的-最多!在Visual Studio中运行应用程序时,它将围绕.mdf文件从应用程序的数据目录复制到输出目录-通常是。\bin\debug-应用程序运行的地方,很可能插入工作正常-但最终您看到的是错误的.mdf文件

如果您想坚持使用这种方法,那么请尝试在myConnection.Close调用上放置一个断点-然后使用SQL Server Management Studio检查.mdf文件-我几乎可以肯定您的数据在那里

在我看来,真正的解决办法是

安装SQL Server Express,您已经完成了安装 安装SQL Server Management Studio 在SSMS中创建数据库,给它一个逻辑名称,例如Trains 使用在服务器上创建时提供的逻辑数据库名称连接到它,并且不要混淆物理数据库文件和用户实例。在这种情况下,您的连接字符串类似于:

Data Source=.\\SQLEXPRESS;Database=Trains;Integrated Security=True
其他一切都和以前一样

更多背景信息,请参见Aaron Bertrand的优秀博客文章。

整个AttachDbFileName=方法都有缺陷——最多!在Visual Studio中运行应用程序时,它将围绕.mdf文件从应用程序的数据目录复制到输出目录-通常是。\bin\debug-应用程序运行的地方,很可能插入工作正常-但最终您看到的是错误的.mdf文件

如果您想坚持使用这种方法,那么请尝试在myConnection.Close调用上放置一个断点-然后使用SQL Server Management Studio检查.mdf文件-我几乎可以肯定您的数据在那里

在我看来,真正的解决办法是

安装SQL Server Express,您已经完成了安装 安装SQL Server Management Studio 在SSMS中创建数据库,给它一个逻辑名称,例如Trains 使用在服务器上创建时提供的逻辑数据库名称连接到它,并且不要混淆物理数据库文件和用户实例。在这种情况下,您的连接字符串类似于:

Data Source=.\\SQLEXPRESS;Database=Trains;Integrated Security=True
其他一切都和以前一样


更多背景信息,请参见Aaron Bertrand的优秀博客文章。

尝试更改此代码

 cmd.CommandText = "INSERT train (id, departure, destination, type, intermediate, departure_time, departure_date, sleeperBerth, firstClass) " +
                              "VALUES ( @trainID , @departure, @destination, @type, @intermediate, @dep_time, @dep_date, @sleep, @first)";
进入


我只在您的插入文本查询中添加了

尝试更改此代码

 cmd.CommandText = "INSERT train (id, departure, destination, type, intermediate, departure_time, departure_date, sleeperBerth, firstClass) " +
                              "VALUES ( @trainID , @departure, @destination, @type, @intermediate, @dep_time, @dep_date, @sleep, @first)";
进入


我只在你的插入文本查询中添加了一个

似乎你处于这种情况似乎你处于这种情况他使用LocalDB可能是因为他不想使用SqlServer Express的完整安装,他想将其应用程序分发给他的客户,而不必担心分发Sql Server和强制执行让他们了解安装Sql Server的整个过程Express@Steve:如果有人认真考虑使用SQL Server进行开发,那么安装SQL Server Express是一个非常小的步骤,而且非常值得,因为它可以一直避免此类问题。而且:如果他想发布这个,他无论如何都必须处理安装SQL Server的问题——以这样或那样的方式……以我的经验,这并不总是正确的。假设你有一个可以从互联网下载的完整应用程序的演示。您的匿名客户希望在购买应用程序之前对其进行测试。您不想花时间为它们安装和配置Sql Server。但是,当然,如果他们喜欢你的应用程序并决定购买它,那么你的客户会非常乐意为他们安装Sql Server Express。我同意你关于有缺陷的附件文件名的看法approach@ESuth:如果您已使用所有默认设置安装了SQL Server Express,则会创建一个SQL Server的命名实例-使用。\SQLEXPRESS或local\SQLEXPRESS作为服务器/实例名称连接到它-如我的回答中所示和提到的…。无需担心。现在一切正常,我可以在检查数据库时看到结果。非常感谢。他使用LocalDB可能是因为他不想使用SqlServer Express的完整安装,他想将其应用程序分发给他的客户,而不必担心分发Sql Server并迫使他们完成安装Sql Server的整个过程Express@Steve当前位置如果有人对你的行为很认真使用SQL Server进行开发时,安装SQL Server Express是一个非常小的步骤,而且非常值得,可以一直避免此类问题。而且:如果他想发布这个,他必须处理安装SQL Server的问题——不管怎样
依我的经验,这并不总是正确的。假设你有一个可以从互联网下载的完整应用程序的演示。您的匿名客户希望在购买应用程序之前对其进行测试。您不想花时间为它们安装和配置Sql Server。但是,当然,如果他们喜欢你的应用程序并决定购买它,那么你的客户会非常乐意为他们安装Sql Server Express。我同意你关于有缺陷的附件文件名的看法approach@ESuth:如果您已使用所有默认设置安装了SQL Server Express,则会创建一个SQL Server的命名实例-使用。\SQLEXPRESS或local\SQLEXPRESS作为服务器/实例名称连接到它-如我的回答中所示和提到的…。无需担心。现在一切正常,我可以在检查数据库时看到结果。非常感谢你。