日期:2014-05-18  浏览次数:21026 次

Update 无法找到 TableMapping['a'] 或 DataTable“a”
[code=C#][/code]


private string search;
  string sql; //数据库语句 根据不同选择 赋值不同


  public DataSet ds = new DataSet();
  SqlDataAdapter sda1 = null;
  public SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings[1].ConnectionString.ToString());
   


private void BT_edit_Click(object sender, EventArgs e)
  {
   
  sda1.Update(ds, "a");
  conn.Close();
  Databind();
  }

public void Databind()
  {


  if (RBT_name.Checked == true) //如果选中 按名称查找
  {
  search = TB_name.Text.Trim();
  sql = "select [hw_name] '名称',[hw_bianma] '产品编码' ,[hw_shuliang] '数量',[hw_weizhi] '位置'from [kucun] where [hw_name]='" + search + "'";
  }

  if (RBT_bianma.Checked == true)
  {
  search = TB_bianma.Text.Trim();
  sql = "select [hw_name] '名称',[hw_bianma] '产品编码' ,[hw_shuliang] '数量',[hw_weizhi] '位置'from [kucun] where [hw_bianma]='" + search + "'";
  }


  if (RBT_weizhi.Checked == true)
  {
  search = CBOX_huojia.Text.Trim() + CBOX_ceng.Text.Trim();
  sql = "select [hw_name] '名称',[hw_bianma] '产品编码' ,[hw_shuliang] '数量', [hw_weizhi] '位置'from [kucun] where [hw_weizhi]='" + search + "'";
  }


   
  SqlCommand scmd1 = new SqlCommand(sql, conn);
  sda1 = new SqlDataAdapter(scmd1);
  sda1.SelectCommand = scmd1;
  SqlCommandBuilder scb = new SqlCommandBuilder(sda1);

   
  sda1.UpdateCommand = scb.GetUpdateCommand();
   
  try
  {
  conn.Open();
  ds.Clear();
  sda1.Fill(ds, "a");
  this.DATA_chakan.DataSource = ds.Tables[0];
  ds.Tables.Clear(); // 将ds中的数据清空 要不下一个项目的查询 数据会重叠
   

  }
  catch (Exception ex)
  {
  MessageBox.Show("数据库异常");
  }
  finally
  {
  conn.Close();
  scmd1.Dispose();
  sda1.Dispose();

  }
  }

------解决方案--------------------
ds 定义为局部变量就行了
C# code
conn.Open();
DataSet ds = new DataSet();
sda1.Fill(ds, "a");
this.DATA_chakan.DataSource = ds.Tables[0];