当我在程序中添加数据时,数据不保存 [英] when i added data in my program,datas dont saved

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

问题描述



我正在VS2008中运行我的项目,并提供要插入到表中的数据.

在当前程序中,添加了数据,并且当程序从该表中选择时,我看到了更改.
在更改了一些不相关的代码后重新运行程序或关闭VS2008并再次打开新程序并运行程序时,该数据似乎未添加(但是当我在VS2008的服务器资源管理器中添加数据时,如果不运行程序,则已添加).
在#1类中

Hi,

I am running my project in VS2008 and give datas to insert to table.

In current program, datas are added and when the program selects from that table, I see changes.
When rerunning the program after changing a little unrelated codes or close VS2008 and Open a new and run program again, that data does not appear to be added (but when I add data in server explorer of VS2008, without running the program, the data is added).
in class #1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Mallmanagement
{
    public class mydataaccess
    {
        public string ServerName;
        public string DBName = "goldDB.mdf";
        public string username;
        public string password;
        SqlConnection con;
        SqlCommand cmd;
        SqlDataAdapter da;
        public mydataaccess()
        {
            con = new SqlConnection();
            cmd = new SqlCommand();
            da = new SqlDataAdapter();
            cmd.Connection = con;
            da.SelectCommand = cmd;
        }
        public void connect()
        {
            string cs = "Data source=.\\SQLEXPRESS;Attachdbfilename=|DataDirectory|\\{0};Integrated security=true;user Instance=true";
            cs = string.Format(cs, this.DBName);
            con.ConnectionString = cs;
            con.Open();
        }
        public void Disconnect()
        {
            con.Close();
        }
        public DataTable select(string sql)
        {
            DataTable dt = new DataTable();
            cmd.CommandText = sql;
            da.Fill(dt);
            return dt;
        }
        public void docommand(string sql)
        {
            cmd.CommandText = sql;
            cmd.ExecuteNonQuery();
        }
    }
}


和#2类


and class #2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Mallmanagement
{
    class sanadlogic
    {
        mydataaccess md = new mydataaccess();
        public int codehesab;
        public int codesanad;
        public int day;
        public int month;
        public int year;
        public DataTable select()
        {
            md.connect();
            string ss = "Select * from sanadha";
            DataTable dt = new DataTable();
            dt = md.select(ss);
            md.Disconnect();
            return dt;
        }
        public void Insert()
        {
            md.connect();
            string AA = "Insert into sanadha(codesanad,codehesab,day,month,year)values({0},{1},{2},{3},{4}";
            AA = string.Format(AA, this.codesanad, this.codehesab, this.day, this.month, this.year);
            md.docommand(AA);
            md.Disconnect();
        }
    }
}



有什么问题吗?
我可以在运行的程序中更改,添加,删除数据吗?
并且我可以更改,添加,删除表中的数据吗?



What is the problem?
Can I to change, add, delete data in running program?
And can I change, add, delete data in tables?

推荐答案

如果我正确理解了您的问题,则可能是由于您为数据库附加了文件例如,您的bin目录中的示例,并在构建过程中将其从项目目录复制到该目录中.每次构建解决方案时,这都会导致db文件被覆盖.

旁注:为什么要对年/月/日使用单独的字段.考虑使用date 数据类型,我相信这会在以后避免麻烦.
If I understood you problem correctly, one cause could be that you''re attaching the database file for example from your bin directory and it''s copied into that directory from your project directory during build. This would cause the overwrite of the db file every time you build the solution.

Side-note: Why use separate fields for year/month/date. Consider using date datatype, which I believe would save you from lot''s of trouble later.


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

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