关于access的insert into问题 在对access数据表进行insert into插入后再吧关闭程序的情况下对该表进行查询可以看到insert进去的那条记录,但是将程序关闭重启后insert进行的那条记录又没有了,感觉是在程序insert的时候在内存中执行成功了,但是并没有insert到硬盘中的文件中去。这是什么原因啊。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 没插进到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,Re) values('" + txtMaterialName.Text.Trim() + "','" + txtSpecification.Text.Trim() + "','" + txtUnit.Text.Trim() + "',0,'" + txtRe.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,Re='" + txtRe.Text.Trim() + "')"; } 在对access数据表进行insert into插入后,不关闭程序的情况下对该表进行查询可以看到insert进去的那条记录,但是将程序关闭重启后insert进行的那条记录又没有了,感觉是在程序insert的时候在内存中执行成功了,但是并没有insert到硬盘中的文件中去。这是什么原因啊。 检查一下你是不是那个Access文件有2份,其实你已经插入数据,但是你查看的是另一个access数据库而已。 看看你的链接字符串配置,可能是你调试的时候连到app_data里的数据库了(ASP.net自动搞得)了,但你关了程序后看得是硬盘实际位置把硬盘里的access文件也放到app_data就行,或者在Webconfig中该配置,好像是appSettings这个 我在打开的时候会生成已个“EPMS.ldb”文件,是这个原因啊? 怎么设置pageSetupDialog显示的横向纵向打印的默认值。 想做操作系统,做过的人给点建议 C#非类库项目怎么编译出DLL? 远程通信问题 VS2005 c# 数据源 新建连接失败! 求教:简单判断某个对象是否实现了某个接口? 如何在winform中关闭登录窗体后打开主窗体? Web中DataGrid的分页问题 datagridview 中的单元格如何实现类似EXCEL 单元格的计算。 语音提示(帮忙!!!剧给分) 关于窗口图形删除的问题------跪求解决 最好能详细点 sql2005+vs2008(C#)数据库应用软件开发
代码如下:
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,Re) values('" + txtMaterialName.Text.Trim()
+ "','" + txtSpecification.Text.Trim() + "','" + txtUnit.Text.Trim() + "',0,'" + txtRe.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,Re='" + txtRe.Text.Trim() + "')";
}