日期:2014-05-20  浏览次数:20739 次

数据库更新问题.
sing   System;
using   System.Data;
using   System.Data.OleDb;
using   System.Xml;  
using   System.Windows.Forms;    
namespace   代码收集
{

public   class   DataBase
{
private   OleDbConnection   conn;
public     OleDbCommand         cmd;
                private   OleDbDataAdapter   adp;

public   DataBase()
{
conn   =   new   OleDbConnection();
cmd   =   new   OleDbCommand();
cmd.Connection   =   conn;  
adp   =   new     OleDbDataAdapter();  
conn.ConnectionString   = "Provider=Microsoft.jet.OLEDB.4.0;Data   Source= "+Application.StartupPath+@ "\db1.mdb;Jet   OLEDB:Database   Password=zhijun123.mpc.cn ";
adp.SelectCommand   =   cmd;
adp.InsertCommand   =   cmd;
adp.DeleteCommand   =   cmd;
adp.UpdateCommand   =   cmd;
conn.Open();  
}
public   DataSet   DataInit()
{
DataSet   ds   =   new   DataSet();  
cmd.CommandText   =   "select   *   from   函数 ";
adp.Fill(ds, "函数 ");
cmd.CommandText   =   "select   *   from   文章 ";
adp.Fill(ds, "文章 ");
cmd.CommandText   =   "select   *   from   代码 ";
adp.Fill(ds, "代码 ");
return   ds;
}
public   void   update(DataSet   ds   ,string   TableName)
{
cmd.CommandText   = "Select   *   from   "+TableName;
                        OleDbDataAdapter   madp   =new   OleDbDataAdapter( "Select   *   from   "+TableName,conn);    
        OleDbCommandBuilder   cb   =   new   OleDbCommandBuilder(madp);
MessageBox.Show(cb.GetInsertCommand().ToString());
madp.UpdateCommand   =   cb.GetUpdateCommand();  
madp.Update(ds,TableName);
ds.AcceptChanges();  
}
}
}
添加过程
private   void   menuItem12_Click(object   sender,   System.EventArgs   e)
{
Form2   ft   =   new   Form2();
if   (ft.ShowDialog(this)==DialogResult.OK)
{
if   (MessageBox.Show( "真的要添加吗? ")==DialogResult.OK)
{

DataTable   dt   =ds.Tables[TableName];
DataRow   dr   =   dt.NewRow();
dr[1]   =   ft.textBox1.Text;
dr[2]   =   this.textBox1.Text;
dt.Rows.Add(dr);
dt.AcceptChanges();    
da.update(ds,TableName);        
ds   =   da.DataInit();  
ListInit();
}
}
}
为什么更新不成功啊。

------解决方案--------------------
写法有问题,给你一个我写的增、删、改的代码:
private SqlConnection sqlCn=null;
private SqlDataAdapter sqlDA=null;
private string m_strCn = "Data Source=(local);Initial Catalog=LineMonitor; "
+ "Integrated Security=SSPI; ";
private string strSql = "Select * from UserManager ";

private SqlCommandBuilder sqlCB = null;