namespace Database
{
    public class Dbase
    {
        private string ss = "Integrated Security=SSPI;User Id=sa;database=jindu;server=localhost";
        public SqlConnection sqlcon;
        public static System.DateTime[] time = new System.DateTime[50];
        public Dbase()
{
this.sqlcon=new SqlConnection(this.ss);
                            sqlcon.Open();
           
}
        public DataSet  getdataset(string Sqltext)
        {
           
            DataSet  ds=new DataSet ();
            SqlDataAdapter da = new SqlDataAdapter(Sqltext, this.ss);
            SqlCommandBuilder thisbuilder = new SqlCommandBuilder(da); 
            da.Fill(ds);
            return ds;
        }        public void update(string Sqltext,string table)
        {
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(Sqltext, this.ss);
            SqlCommandBuilder thisbuilder = new SqlCommandBuilder(da);
            da.Fill(ds);
            da.Update(ds,table);
        }
}
namespace manage
{
    public partial class sonproject : Form
    {
........................
string cm = "select  * from sonproject ";
            Dbase db = new Dbase();
            DataSet ds = new DataSet();
            ds = db.getdataset(cm);
            DataRow thisrow = ds.Tables[0].NewRow();
            
            thisrow ["父项目编号"]=pronumtext .Text ;
            thisrow ["子项目编号"]=sonpronumtext .Text ;
            thisrow ["子项目名称"]=sonpronametext .Text ;
            thisrow ["开始时间"]=sonprobegindatetime .Value ;
            thisrow["结束时间"] = sonproenddatetime.Value;
            thisrow ["前置子项目"]=sonprobeforetext.Text ;
            thisrow ["负责人"]=sonprobeforetext .Text ;
            thisrow ["负责公司"]=sonprocompanytext .Text ;
            
            string tb = "sonproject";
            ds.Tables[0].Rows.Add(thisrow);
            db.update(cm,tb);       //此处为什么会出现如题所示的错误呢
.................................
}

解决方案 »

  1.   

    使用SqlCommanderBuilder时DataSet ds = new DataSet();
    SqlDataAdapter da = new SqlDataAdapter(Sqltext, this.ss);
    SqlCommandBuilder thisbuilder = new SqlCommandBuilder(da);//确保此处sqltext返回了主键信息===================================================================
    da.SelectCommand = thisbuilder .GetInsertCommand();
    da.UpdateCommand = thisbuilder .GetUpdateCommand();
    da.DeleteCommand = thisbuilder .GetDeleteCommand();
    ===================================================================
    da.Fill(ds);
    da.Update(ds,table);
      

  2.   

    针对找不到table是因为你在da.Fill(ds)时 应当指定table的名称da.Fill(ds,sonproject);
      

  3.   

    public void update(string Sqltext,string table)
            {
                DataSet ds = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(Sqltext, this.ss);
                SqlCommandBuilder thisbuilder = new SqlCommandBuilder(da);
                DataColumn[] keys = new DataColumn[1];
                keys[0] = ds.Tables[0].Columns[1];    //无法找到表 0。
                ds.Tables[table].PrimaryKey = keys;
                da.UpdateCommand = thisbuilder.GetUpdateCommand();
                da.Fill(ds);
                da.Update(ds,table);
            }
    我把上面update()方法修改如上,却提示找不到表0,那个表不就是sqltext打开的那个表吗,为什么还提示找不到呢
      

  4.   

    只是修改da.Fill(ds,table)后,程序可以正常运行,可是为什么数据库中添加记录不成功呢
      

  5.   

    da.Fill(ds);
    ================〉〉 da.Fill(ds,table);
      

  6.   

    使用SqlCommaderBuilder操作数据库时,首先要确保数据库中该表有主键不是让你在ds中设置主键
      

  7.   


    if (!ds.HasChanges())
                {
                    da.Update(ds,table);
                    ds.AcceptChanges();
                }
      

  8.   

    好像还是不行耶,数据库中没插进记录
    请问能加你qq吗
    158695389我qq