Home > Backend Development > C#.Net Tutorial > Detailed introduction to the C# solution to the problem that the row already belongs to another table

Detailed introduction to the C# solution to the problem that the row already belongs to another table

黄舟
Release: 2017-03-11 13:15:13
Original
2103 people have browsed it

Detailed introduction to the C# solution that the row already belongs to another table

The error code:

DataTable dtContract_src = Oper.GetDataTable("select * from T_Contract where ProjectID=" + ProjectID_src + " and Flag=0", con_src);
                            foreach (DataRow dr in dtContract_src.Rows)
                            {
                                String ContractID_src = dr["ContractID"].ToString();
                                DataTable dtContract_dst = Oper.GetDataTable("select * from T_Contract where ProjectID=" + ProjectID_src + "
                                 and ContractID=" + ContractID_src + " and Flag=0", con_dst);
                                if (dtContract_dst.Rows.Count != 0)
                                {
                                    impContract_exist++;
                                    continue;
                                }

                                dtContract_dst.Rows.Add(dr);

                                String columns = "";
                                String paramss = "";
                                OleDbCommand updateCmd = con_dst.CreateCommand();
                                foreach (DataColumn dc in dtContract_dst.Columns)
                                {
                                    if (columns == "")
                                    {
                                        columns = dc.ColumnName;
                                        paramss = "@" + dc.ColumnName;
                                    }
                                    else
                                    {
                                        columns += ", " + dc.ColumnName;
                                        paramss += ", @" + dc.ColumnName;
                                    }
                                    updateCmd.Parameters.Add(new OleDbParameter(dc.ColumnName, dc.DataType);
                                }
                                updateCmd.CommandText = "insert into T_Contract(" + columns + ") Values(" + paramss + ")";

                                updateCmd.ExecuteNonQuery();

                                //OleDbDataAdapter dataAdap = new OleDbDataAdapter();
                                //dataAdap.InsertCommand = updateCmd;
                                //dataAdap.Update(dtContract_dst);

                            }
Copy after login

Error line:

dtContract_dst.Rows.Add(dr);
Copy after login

Solution:

dtContract_dst.Rows.Add(dr.ItemArray);
Copy after login

The above is the detailed content of Detailed introduction to the C# solution to the problem that the row already belongs to another table. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
c#
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template