关于access的insert into问题

wh0826 2012-03-17 06:00:44
在对access数据表进行insert into插入后再吧关闭程序的情况下对该表进行查询可以看到insert进去的那条记录,但是将程序关闭重启后insert进行的那条记录又没有了,感觉是在程序insert的时候在内存中执行成功了,但是并没有insert到硬盘中的文件中去。这是什么原因啊。
...全文
603 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wh0826 2012-03-20
  • 打赏
  • 举报
回复
我在打开的时候会生成已个“EPMS.ldb”文件,是这个原因啊?
色拉油 2012-03-17
  • 打赏
  • 举报
回复
看看你的链接字符串配置,可能是你调试的时候连到app_data里的数据库了(ASP.net自动搞得)了,但你关了程序后看得是硬盘实际位置

把硬盘里的access文件也放到app_data就行,或者在Webconfig中该配置,好像是appSettings这个
暖枫无敌 2012-03-17
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 wh0826 的回复:]

在对access数据表进行insert into插入后,不关闭程序的情况下对该表进行查询可以看到insert进去的那条记录,但是将程序关闭重启后insert进行的那条记录又没有了,感觉是在程序insert的时候在内存中执行成功了,但是并没有insert到硬盘中的文件中去。这是什么原因啊。
[/Quote]
检查一下你是不是那个Access文件有2份,其实你已经插入数据,但是你查看的是另一个access数据库而已。
wh0826 2012-03-17
  • 打赏
  • 举报
回复
在对access数据表进行insert into插入后,不关闭程序的情况下对该表进行查询可以看到insert进去的那条记录,但是将程序关闭重启后insert进行的那条记录又没有了,感觉是在程序insert的时候在内存中执行成功了,但是并没有insert到硬盘中的文件中去。这是什么原因啊。
wh0826 2012-03-17
  • 打赏
  • 举报
回复
检查过了确定,
代码如下:


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() + "')";

}
dalmeeme 2012-03-17
  • 打赏
  • 举报
回复
应该不会吧,楼主再检查一下哈~~
stonespace 2012-03-17
  • 打赏
  • 举报
回复
不会的,可能是你的access有缓存,所以没有察觉到插入的新纪录,
EnForGrass 2012-03-17
  • 打赏
  • 举报
回复
没插进到access数据表里,不会吧?

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧