无法在数据库中保存数据 [英] Unable to Save data in database

查看:145
本文介绍了无法在数据库中保存数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我添加数据功能的代码

Here is the code of my add data function

protected void addData(string name,string email,string field,string address,string notes,int phone,string site)
        {
            DateTime dt = DateTime.Today;
            SqlConnection con = new SqlConnection(@"Data Source=GAURAV-PC\SQLEXPRESS;AttachDbFilename=|DataDirectory|\JAMS.mdf;Integrated Security=True;User Instance=True");
            con.Open();
            SqlCommand Cmd = con.CreateCommand();
            SqlTransaction trans = con.BeginTransaction();
            Cmd.Connection = con;
            Cmd.Transaction = trans;
            try
            {
                Cmd.CommandText = "Insert into JAMS_Companies(company,email,website,phone,address,field,notes) values(@name,@email,@site,@phone,@address,@field,@notes)";
                Cmd.Parameters.AddWithValue("@name", name);
                Cmd.Parameters.AddWithValue("@email", email);
                Cmd.Parameters.AddWithValue("@site", site);
                Cmd.Parameters.AddWithValue("@phone", phone);
                Cmd.Parameters.AddWithValue("@address", address);
                Cmd.Parameters.AddWithValue("@field", field);
                Cmd.Parameters.AddWithValue("@notes", notes);
                //comm.Parameters.AddWithValue("@dt", dt);
                Cmd.ExecuteNonQuery();
                trans.Commit();
                this.Close();
            }
            catch (Exception e)
            {
                Console.Write("Haha" + e.ToString());
                trans.Rollback();
            }
            finally
            {
                con.Close();
            }
        }



这会临时添加数据,即当我重新运行程序时,它会在网格中显示数据,但是当我检查数据库时,则没有数据.请帮助



This adds the data temporarily i.e when I rerun the program It shows data in grid but when I check database then there is no data.Kindly Help

推荐答案

您是否有两个数据库?您可以插入一个,但查看另一个吗?
Do you have two databases? Could you be inserting to one but looking at the other?


如果插入失败,则会发生错误或异常.您似乎正在正确处理和显示异常.

因此,要么插入成功(在某处),要么代码从未执行,或者其他一些代码正在删除插入的记录.
If an insert fails an error or exception should occur. You seem to be handling and displaying exceptions correctly.

So either the insert is succeeding (somewhere) or the code is never executing or some other code is deleting the inserted record.


您正在VS中运行应用程序.
项目中的数据库用作运行时的一种模板.
您应该在数据库的bin目录中查找,而不是在项目中的数据目录中查找.

.mdf的复制到输出目录"属性的值是什么?
如果它是始终复制",则每次运行应用程序时,bin中的数据库将被项目中的数据库覆盖.
如果将属性设置为如果更新则复制",则只有在更改设计或添加值时,该属性才会复制到垃圾箱.
还有不复制"选项.您在调试时更新的值将始终保留.

干杯
You are running your app from within VS.
The database in your project serves as a kind of template for the runtime.
You should look in the bin-directory for your DB, not the one in your project for the data.

What the value of "Copy to output directory" property of the .mdf?
If it''s "Copy always", every time you run your app the database in the bin will be overwritten by the one in your project.
If you set the property to "Copy if newer" it will be copied to the bin only when you change the design or add values.
There''s also the "Do not Copy" option. The values you updated while debugging will be preserved always.

Cheers


这篇关于无法在数据库中保存数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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