请教高人:
C#操作数据库改变了数据但打开表后显示数据没有改变?
为什么?

解决方案 »

  1.   


    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;namespace 登录程序
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void IncErrortimes()
            {
                using (SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;
            AttachDBFilename=|DataDirectory|\myDB.mdf;
            Integrated Security=True;User Instance=True"))            using (SqlCommand updateCmd = con.CreateCommand())
                {
                    con.Open();
                    updateCmd.CommandText = "update Mytable set Errortimes=Errortimes+1 where UserName=@UN";
                    updateCmd.Parameters.Add(new SqlParameter("UN", Usernametextbox.Text));
                   
                    updateCmd.ExecuteNonQuery();
                }
                MessageBox.Show("登录失败!!!");        }
                    
            private void button1_Click(object sender, EventArgs e)
            {
             using (SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;
    AttachDBFilename=|DataDirectory|\myDB.mdf;
    Integrated Security=True;User Instance=True"))
             {
                 con.Open();
                 using (SqlCommand cmd = new SqlCommand())
                 {
                    cmd.Connection = con;
                    cmd.CommandText = "select * from Mytable where Username=@UN";
                    cmd.Parameters.Add(new SqlParameter("UN", Usernametextbox.Text));
                    Username=Usernametextbox.Text";
                    using (SqlDataReader reader = cmd.ExecuteReader())
                     {
                         if (reader.Read())
                         {
                             int arrortime = reader.GetInt32(reader.GetOrdinal("Errortimes"));
                             if (arrortime>2)                         {
                                 MessageBox.Show("登录次数过多,禁止登录!!!");
                                 ResetErrortimes();
                                 return;
                             }
                             string dbpassword = reader.GetString(reader.GetOrdinal("Password"));
                             if (dbpassword==PasswordtextBox.Text)
                             {
                                 MessageBox.Show("登录成功!");
                             }
                             else
                             {
                                 IncErrortimes();
                             }
                         }
                         else
                             {
                              MessageBox.Show("用户名不存在");
                             }
                         }
                 }
             }        }        private void button2_Click(object sender, EventArgs e)
            {
                creatTable();
                MessageBox.Show("创建新表成功!!!");
            }
        }
    }
    当某一用户名连续登录三次失败会报错"登录次数过多,禁止登录!!!";(可见Mytable表字段Errortimes数据变成3了)
    但是打开表后该对应用户Errortimes字段值还是0
      

  2.   

    没有使用dateset,是对数据库直接操作,不是对本地缓存(dateset)操作。IncErrortimes()函数里的
    "update Mytable set Errortimes=Errortimes+1 where UserName=@UN";
    这里的语句有问题。应该使用字符串相加的方式
    "update Mytable set Errortimes=“+Errortimes+1”+" where UserName=@UN";
    不然的话,Errortimes就不作为变量,而是SQL语句的一部分执行,当然得不到更新
    还要把当前错误次数作为参数传给IncErrortimes()函数这么写太麻烦了,每次都要打开关闭连接。。还要写连接字符串。。最好把字符串写到配置文件里。。
      

  3.   

    也可以使用参数的方式
    "update Mytable set Errortimes=@Errortimes where UserName=@UN";
    cmd.Parameter.Add(new SqlParameter("Errortimes",Errortimes+1));
      

  4.   

    Errortimes是表里的字段名采用上面的方法,会报错说cmd.Parameter.Add(new SqlParameter("Errortimes",Errortimes+1));中的红色Errortimes在当前上下文中不存在
      

  5.   

    我们现在正在学这个,应该是要用
    SqlCommandBuilder builder=new SqlCommandBuilder(DataAdapter);
    DataAdapter.Update(dataset,表名)更新
    好像是这样,你试试!
      

  6.   


    你没给IncErrortimes()函数传参数,当然不存在了。。
      

  7.   

    当我看到这段,就知道问题了:AttachDBFilename=|DataDirectory|\myDB.mdf;显然你用的是临时数据库,地址都是临时地址,而非绝对地址,每次你调试程序,这个数据库都会新建,用你项目中的那个数据库替换bin\dubug目录下面的数据库,因此你所做的修改都是徒劳。
      

  8.   

    另外updateCmd.Parameters.Add(new SqlParameter("UN", Usernametextbox.Text));
    改为:
    updateCmd.Parameters.Add(new SqlParameter("@UN", Usernametextbox.Text));
    注意参数前面的@符号必须有,不然这语句本身执行都要报错,不知道你怎么可能执行通过的。
      

  9.   

    请教怎样才能用到bin\dubug目录下面的数据库呢?
      

  10.   

    lz在开发的时候是否是使用内嵌的mdf数据库。如果是要使用内嵌的mdf数据库时在main函数中加入以下的代码就可以:
    string dataDir = AppDomain.CurrentDomain.BaseDirectory;
                if (dataDir.EndsWith(@"\bin\Debug\") || dataDir.EndsWith(@"\bin\Release\"))
                {
                    dataDir = System.IO.Directory.GetParent(dataDir).Parent.Parent.FullName;
                    AppDomain.CurrentDomain.SetData("DataDirectory", dataDir);
                }
      

  11.   

    楼上正解,以前做大作业时也遇到过如此问题,查了很多地方才知道,每次运行时,都会把项目根目录下的数据库复制到bin\debug下,所以程序运行的时候操作的是bin\debug下的那个数据库,等到下次运行的时候,又把根目录下的那个数据库复制到bin\debug下去,覆盖了原来bin\debug下的那个数据库,之前的操作结果自然就丢失了,但一直不知道如何解决这个问题,看到此贴,学习了