记录已成功插入,但数据库未更新C#和SQL Server Compact数据库 [英] Record inserted successfully but database not updated C# and SQL Server Compact Database

查看:124
本文介绍了记录已成功插入,但数据库未更新C#和SQL Server Compact数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#Windows窗体应用程序
数据库类型为Microsoft®SQLServer®Compact,版本为4.0.8876.1
当我插入数据消息框时弹出已添加新记录",但我检查数据库中没有数据插入表为空
这是为什么?我的代码有问题吗?
预先感谢!


这是我的代码数据库和应用程序exe在同一目录中


I''m using a C# windows form application
Database type is Microsoft® SQL Server® Compact and version is 4.0.8876.1
When I insert data message box pop up "New Record Added" but I check the database no data inserted table is empty
Why is that? Any wrong with my code?
Thank in advance!


This is my code database and application exe in the same directory


int index = cb1.SelectedIndex;
            string apPath = Path.GetDirectoryName(Application.ExecutablePath);


            SqlCeConnection con = new SqlCeConnection("Data Source=" + app_path + "\\application.sdf;Persist Security Info=False;");
            con.Open();
            SqlCeCommand cm = new SqlCeCommand("INSERT INTO tbl_user(u_id,u_name,u_scl,u_que) VALUES (@u_id,@u_name, @u_scl, @u_que)", con);
            cm.Parameters.AddWithValue("@u_id", 2);
            cm.Parameters.AddWithValue("@u_name", txtname.ToString());
            cm.Parameters.AddWithValue("@u_scl", txtscl.ToString());
            cm.Parameters.AddWithValue("@u_que", index);

            try
            {
                int eff = cm.ExecuteNonQuery();

                if (eff == 1)
                {
                    con.Close();
                    MessageBox.Show("New Record Added " , "Done");
                    
                }
                else
                {
                    con.Close();
                    MessageBox.Show("not inserted ", "Error");
                }
            }
            catch (SqlCeException ex)
            {
                MessageBox.Show(ex.ToString());
            }

推荐答案

您应该查看参数,并确保它们指向文本框的text属性.
You should look at the parameters and make sure that they are pointing to the text property of the textboxes.


4票被接受


整个用户实例和AttachDbFileName =方法都有缺陷-充其量!在Visual Studio中运行应用程序时,它将在.mdf文件周围复制(从App_Data目录复制到输出目录-通常是.\ bin \ debug-应用程序运行所在的位置),并且很可能INSERT可以正常工作-但您只是在看错误的.mdf文件而已!

如果您要坚持这种方法,请尝试在myConnection.Close()调用上放置一个断点-然后使用SQL Server Mgmt Studio Express检查.mdf文件-我几乎可以确定您的数据在那里. />
我认为真正的解决方案是

安装SQL Server Express(并且您已经完成了此操作)

安装SQL Server Management Studio Express

在SSMS Express中创建数据库,并为其指定逻辑名称(例如MyDB)

使用其逻辑数据库名称(在服务器上创建时提供)连接到它-并不会弄乱物理数据库文件和用户实例.在这种情况下,您的连接字符串将类似于:

数据源=.\\ SQLEXPRESS;数据库= MyDB;集成安全性= True

其他一切都和以前完全一样...
4 down vote accepted


The whole User Instance and AttachDbFileName= approach is flawed - at best! When running your app in Visual Studio, it will be copying around the .mdf file (from your App_Data directory to the output directory - typically .\bin\debug - where you app runs) and most likely, your INSERT works just fine - but you''re just looking at the wrong .mdf file in the end!

If you want to stick with this approach, then try putting a breakpoint on the myConnection.Close() call - and then inspect the .mdf file with SQL Server Mgmt Studio Express - I''m almost certain your data is there.

The real solution in my opinion would be to

install SQL Server Express (and you''ve already done that anyway)

install SQL Server Management Studio Express

create your database in SSMS Express, give it a logical name (e.g. MyDB)

connect to it using its logical database name (given when you create it on the server) - and don''t mess around with physical database files and user instances. In that case, your connection string would be something like:

Data Source=.\\SQLEXPRESS;Database=MyDB;Integrated Security=True

and everything else is exactly the same as before...


这篇关于记录已成功插入,但数据库未更新C#和SQL Server Compact数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆