将问题插入.sdf数据库C# [英] Insert problems to .sdf database C#

查看:84
本文介绍了将问题插入.sdf数据库C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行程序时,它似乎没有任何错误,但没有向数据库中插入任何数据.是否缺少一些基本代码?

When i run the program it doesn't seem to have any errors but it doesn't insert any data to the database. Is there some essential code missing?

这是我的代码:

            Using string connection = @"Data Source=|DataDirectory|\InvoiceDatabase.sdf";
            SqlCeConnection cn = new SqlCeConnection(connection);

            try
            {
                cn.Open();
            }
            catch (SqlCeException ex)
            {
                MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.ExitThread();
            }

            SqlCeCommand cmd = new SqlCeCommand("INSERT INTO Client(Name, Address, Postcode, Telephone_Number)VALUES(@name, @address, @postcode, @tel)", cn);
            cmd.Parameters.AddWithValue("@name", txt_ClientName.Text);
            cmd.Parameters.AddWithValue("@address", txt_ClientAddress.Text);
            cmd.Parameters.AddWithValue("@postcode", txt_postcode.Text);
            cmd.Parameters.AddWithValue("@tel", txt_TelNo.Text);

            try
            {
                int affectedRows = cmd.ExecuteNonQuery();

                if (affectedRows > 0)
                {
                    txt_ClientAddress.Text = "";
                    txt_ClientName.Text = "";
                    txt_postcode.Text = "";
                    txt_TelNo.Text = "";
                    MessageBox.Show("Client: " + txt_ClientName.Text + " added to database. WOoo", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Client: " + txt_ClientName.Text + " Failed to add to database. WOoo", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

在此先感谢您的反馈.

推荐答案

请确保您观看添加行的数据库.

Please make sure that you watch the database where you add the rows.

在大多数情况下,数据库.sdf文件会复制到release文件夹中,您可以在服务器资源管理器打开其他一些数据库文件的同时进行处理.

In most cases, the database .sdf file gets copied to the release folder and you work on that, while the server explorer has opened some other database file.

尝试打开发布"下的.sdf文件,检查是否在其中添加了行.

Try and open the .sdf file under Release, check if rows where added there.

这篇关于将问题插入.sdf数据库C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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