当前位置:编程学习 > C#/ASP.NET >>

关于access的insert into问题

在对access数据表进行insert into插入后再吧关闭程序的情况下对该表进行查询可以看到insert进去的那条记录,但是将程序关闭重启后insert进行的那条记录又没有了,感觉是在程序insert的时候在内存中执行成功了,但是并没有insert到硬盘中的文件中去。这是什么原因啊。 --------------------编程问答-------------------- 没插进到access数据表里,不会吧? --------------------编程问答-------------------- 不会的,可能是你的access有缓存,所以没有察觉到插入的新纪录, --------------------编程问答-------------------- 应该不会吧,楼主再检查一下哈~~ --------------------编程问答-------------------- 检查过了确定,
代码如下:  


class DBUtil
    {
        private static string strConn = ConfigurationManager.ConnectionStrings["EPMS.Properties.Settings.EPMSConnectionString"].ConnectionString.ToString();
        

        public static OleDbConnection GetConn()
        {
            OleDbConnection oledbConn = new OleDbConnection(strConn);
            try
            {
                if (oledbConn.State.Equals(ConnectionState.Closed))
                {
                    oledbConn.Open();
                }
            }
            catch (Exception ex)
            {
                oledbConn.Close();
                oledbConn.Dispose(); 
                oledbConn = null;
                MessageBox.Show(ex.Message);
            } 
            return oledbConn;
        }

        public static OleDbCommand GetCmd(string sql, OleDbConnection oledbConn)
        {
            if (oledbConn == null) return null;
            OleDbCommand oledbCmd = oledbConn.CreateCommand();
            oledbCmd.CommandText = sql;
            return oledbCmd;

        }

        public static OleDbDataReader GetReader(string sql, OleDbConnection oledbConn)
        {
            if (oledbConn == null) return null;
            OleDbCommand oledbCmd = GetCmd(sql, oledbConn);
            OleDbTransaction oledbTran = null;
            OleDbDataReader oledbReader = null;
            try
            {
                oledbTran = oledbConn.BeginTransaction(); 
                oledbCmd.Transaction = oledbTran;
                oledbReader = oledbCmd.ExecuteReader();
                oledbCmd.Transaction.Commit();
               // oledbTran.Commit();
            }
            catch (Exception ex)
            {

                try
                {
                    // Attempt to roll back the transaction.
                    oledbTran.Rollback();
                }
                catch(Exception ex2)
                {
                    // Do nothing here; transaction is not active.
                }
                oledbReader.Close();
                oledbConn.Close(); //关闭与数据库的连接
                MessageBox.Show(ex.Message);
            }
            finally
            {
                oledbCmd.Dispose();
            }
            return oledbReader;
        }
        public static bool Update(string sql, OleDbConnection oledbConn)
        {
            
            if (oledbConn == null) return false;
            OleDbCommand oledbCmd = GetCmd(sql, oledbConn);
            OleDbTransaction oledbTran = null;
            bool result = true;
            try
            {
                oledbTran = oledbConn.BeginTransaction();
                oledbCmd.Transaction = oledbTran;
                oledbCmd.ExecuteNonQuery();//执行SQL语句
                oledbTran.Commit();
               
            }
            catch (Exception ex)
            {
                try
                {
                    // Attempt to roll back the transaction.
                    oledbTran.Rollback();
                }
                catch (Exception ex2)
                {
                    // Do nothing here; transaction is not active.
                }
                //oledbConn.Close();
                result = false;
                MessageBox.Show(ex.Message);
                
            }
            finally
            {
                oledbCmd.Dispose();   //释放所有空间
                oledbConn.Close(); //调用con_close()方法,关闭与数据库的连接
            }
            return result;
        }
        public static bool Updates(string[] sqls, OleDbConnection oledbConn)
        {
            if (oledbConn == null) return false;
            OleDbCommand oledbCmd = oledbConn.CreateCommand();
            OleDbTransaction oledbTran = null;
            oledbCmd.Transaction = oledbTran;
            bool result = true;
            try
            {
                oledbTran = oledbConn.BeginTransaction();
                oledbCmd.Transaction = oledbTran;
                for (int i = 0; i < sqls.Length; i++)
                {
                    oledbCmd.CommandText = sqls[i];
                    oledbCmd.ExecuteNonQuery();
                }
                oledbTran.Commit();

            }
            catch (Exception ex)
            {
               
                // Attempt to roll back the transaction.
                try
                {
                    oledbTran.Rollback();
                }
                catch (Exception ex2)
                {
                    Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType());
                    Console.WriteLine("  Message: {0}", ex2.Message);
                }
                result = false;
            }
            finally
            {

                oledbCmd.Dispose();   //释放所有空间
                oledbConn.Close(); //关闭与数据库的连接
            }
            return result;
        }
        public static DataSet GetDataSet(string sql, string tableName, OleDbConnection oledbConn)
        {
            if (oledbConn == null) return null;
            OleDbDataAdapter oledbDa = new OleDbDataAdapter(sql, oledbConn);  //创建一个SqlDataAdapter对象,并获取指定数据表的信息
            DataSet ds = new DataSet(); //创建DataSet对象
            oledbDa.Fill(ds, tableName);  //通过SqlDataAdapter对象的Fill()方法,将数据表信息添加到DataSet对象中
            oledbConn.Close();    //关闭数据库的连接
            return ds;  //返回DataSet对象的信息
        }}


事件

 private void btnSave_Click(object sender, EventArgs e)
        {
            string sql = "insert into tb_Material(MaterialName,Specification,Unit,Amount,Remark) values('" + txtMaterialName.Text.Trim() 
                + "','" + txtSpecification.Text.Trim() + "','" + txtUnit.Text.Trim() + "',0,'" + txtRemark.Text.Trim() + "')";
         //   MessageBox.Show(sql);
            if(EPMS.DataClass.DBUtil.Update(sql, EPMS.DataClass.DBUtil.GetConn()))
                MessageBox.Show("保存成功。");

            sql = "Update tb_Material(MaterialName='" + txtMaterialName.Text.Trim()
                 + "',Specification='" + txtSpecification.Text.Trim() +
                 "',Unit='" + txtUnit.Text.Trim() +
                 "',Amount=0,Remark='" + txtRemark.Text.Trim() + "')"; 
            
        } --------------------编程问答-------------------- 在对access数据表进行insert into插入后,不关闭程序的情况下对该表进行查询可以看到insert进去的那条记录,但是将程序关闭重启后insert进行的那条记录又没有了,感觉是在程序insert的时候在内存中执行成功了,但是并没有insert到硬盘中的文件中去。这是什么原因啊。 --------------------编程问答--------------------
引用 5 楼 wh0826 的回复:
在对access数据表进行insert into插入后,不关闭程序的情况下对该表进行查询可以看到insert进去的那条记录,但是将程序关闭重启后insert进行的那条记录又没有了,感觉是在程序insert的时候在内存中执行成功了,但是并没有insert到硬盘中的文件中去。这是什么原因啊。

检查一下你是不是那个Access文件有2份,其实你已经插入数据,但是你查看的是另一个access数据库而已。 --------------------编程问答-------------------- 看看你的链接字符串配置,可能是你调试的时候连到app_data里的数据库了(ASP.net自动搞得)了,但你关了程序后看得是硬盘实际位置

把硬盘里的access文件也放到app_data就行,或者在Webconfig中该配置,好像是appSettings这个 --------------------编程问答-------------------- 我在打开的时候会生成已个“EPMS.ldb”文件,是这个原因啊?
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,