sql = "select woid,woname from [L_Workshop] where woname='" + comboBox1.Text + "'";//查询所选择的部门的ID
                rs = new SqlDataAdapter(sql, conn);
                rs.Fill(myDataSet, "L_Workshop");
                myBind = this.BindingContext[myDataSet, "L_Workshop"];
                int aa=0;
                if (myBind.Count > 0)
                {
                    aa = Convert.ToInt32(myDataSet.Tables["L_Workshop"].Rows[0]["woid"].ToString());
                }                if (publicclass.pass == "")         //如果是添加
                {
                    sql = "insert into [L_Employ] (emcode,emname,woid)"
                        + " values ('" + textBox1.Text + "','" + textBox2.Text + "'," + aa + ")";
                }
                else                      //如果是修改
                {
                    sql = "update [L_Employ] set emcode='" + textBox1.Text + "',emname='" + textBox2.Text+"',"
                        + "woid=" + aa + " where emid=" + publicclass.pass;
                }
                rs = new SqlDataAdapter(sql, conn);
                rs.Fill(myDataSet, "L_Employ");
                myBind = this.BindingContext[myDataSet, "L_Employ"];++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++sql = "select teid,tename from [L_Technology] where tename='" + comboBox1.Text + "'";//查询所选择的工艺的ID
                myDataSet = new DataSet();
                rs = new SqlDataAdapter(sql, conn);
                rs.Fill(myDataSet, "L_Technology");
                myBind = this.BindingContext[myDataSet, "L_Technology"];
                int aa=0;
                if (myBind.Count > 0)
                {
                    aa = Convert.ToInt32(myDataSet.Tables["L_Technology"].Rows[0]["teid"].ToString());
                }                if (publicclass.pass == "")         //如果是添加
                {
                    sql = "insert into [L_Process] (prcode,prname,zid,teid)"
                        + " values ('" + textBox1.Text + "','" + textBox2.Text + "'," + textBox3.Text + "," + aa + ")";
                }
                else                      //如果是修改
                {
                    sql = "update [L_Process] set prcode='" + textBox1.Text + "',prname='" + textBox2.Text+"',"
                        + "zid=" + textBox3.Text + ",teid=" + aa + " where prid=" + publicclass.pass;
                }
                rs = new SqlDataAdapter(sql, conn);’过滤此处
                rs.Fill(myDataSet, "L_Process");
                myBind = this.BindingContext[myDataSet, "L_Process"];
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
以上两段代码,基本相同,但是第一段代码执行无误,第二段就提示无法创建字段***的子表,后来发现当过滤掉上面的                rs = new SqlDataAdapter(sql, conn);时候就正常了~~~
为啥??

解决方案 »

  1.   

    rs = new SqlDataAdapter(sql, conn);’过滤此处
    啥意思?注释?
    没这句,adapter的sql没变化啊
      

  2.   

    你是不是根本不会用SqlDataAdapter类啊,这个类的构造函数中传递的sql语句必须是select语句,而不能是insert或update(虽然不会报错,但是执行的结果是预期之外的,错误的)。如果要完成更新操作,必须用SqlCommand来定义更新语句,你可以对SqlDataAdapter的属性UpdateCommand或者InsertCommand或者DeleteCommand来赋值,自定义更新语句来更改数据库,也可以单独编写SqlCommand,调用其ExecuteNonQuery()方法直接执行。
      

  3.   

    SqlDataAdapter是 DataSet和 SQL Server之间的桥接器,用于检索和保存数据。SqlDataAdapter通过对数据源使用适当的Transact-SQL语句映射 Fill(它可更改DataSet中的数据以匹配数据源中的数据)和 Update(它可更改数据源中的数据以匹配 DataSet中的数据)来提供这一桥接。当SqlDataAdapter填充 DataSet时,它为返回的数据创建必需的表和列(如果这些表和列尚不存在)。来自http://baike.baidu.com/view/2978109.htm不知道二楼的为什么说不能insert和update,而只能select??
      

  4.   

    建议楼主看msdn:
    SqlDataAdapter Constructor (String, SqlConnection)
    http://msdn.microsoft.com/en-us/library/kx703tc9.aspx下面的话来自上面的链接:
    SqlDataAdapter Constructor (String, SqlConnection)
    Initializes a new instance of the SqlDataAdapter class with a SelectCommand and a SqlConnection object.selectCommandText
    Type: System.String
    A String that is a Transact-SQL SELECT statement or stored procedure to be used by the SelectCommand property of the SqlDataAdapter. .....
      

  5.   

    这是msdn上的例子
    InsertCommand和UpdateCommand还有DeleteCommand可以通过属性复制,但是不能传给构造函数。
    public static SqlDataAdapter CreateSqlDataAdapter(string commandText,
        SqlConnection connection)
    {
        SqlDataAdapter adapter = new SqlDataAdapter(commandText, connection);    adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;    // Create the other commands.
        adapter.InsertCommand = new SqlCommand(
            "INSERT INTO Customers (CustomerID, CompanyName) " +
            "VALUES (@CustomerID, @CompanyName)");    adapter.UpdateCommand = new SqlCommand(
            "UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " +
            "WHERE CustomerID = @oldCustomerID");    adapter.DeleteCommand = new SqlCommand(
            "DELETE FROM Customers WHERE CustomerID = @CustomerID");    // Create the parameters.
        adapter.InsertCommand.Parameters.Add("@CustomerID", 
            SqlDbType.Char, 5, "CustomerID");
        adapter.InsertCommand.Parameters.Add("@CompanyName", 
            SqlDbType.VarChar, 40, "CompanyName");    adapter.UpdateCommand.Parameters.Add("@CustomerID", 
            SqlDbType.Char, 5, "CustomerID");
        adapter.UpdateCommand.Parameters.Add("@CompanyName", 
            SqlDbType.VarChar, 40, "CompanyName");
        adapter.UpdateCommand.Parameters.Add("@oldCustomerID", 
            SqlDbType.Char, 5, "CustomerID").SourceVersion = DataRowVersion.Original;    adapter.DeleteCommand.Parameters.Add("@CustomerID", 
            SqlDbType.Char, 5, "CustomerID").SourceVersion = DataRowVersion.Original;    return adapter;
    }
      

  6.   

    DataAdapter可以设置Insert,update,Delete语句的,而且某些情况下是必须要自行设置的如何更新这种语句的。一般情况下(单表有主键)只需有Select语句,可以使用CommandBuild自动生成语句的。