Incidental primary key information when you add data to a dataTable

After recently encountered a problem in doing the project, we need to remove the original data loaded into a dataTable, but found the information they need to master key data in the operation dataTable in favor of dataTable PrimaryKey property simply can not get the primary key value . Finally, access to information that, when put data out of the data that came out to set the primary key attribute (do not know so tell time principle is not a deviation), recorded as follows specific steps to prepare for subsequent reference:

 using (SqlConnection conn = new SqlConnection(CAppCfg.cnStrs))
                            {
                                conn.Open();
                                using (SqlCommand command = conn.CreateCommand())
                                {

                                        try
                                        {
                                            command.CommandText = sqlstrs;
                                            using (SqlDataAdapter adp = new SqlDataAdapter(command))
                                            {
                                                adp.MissingSchemaAction = MissingSchemaAction.AddWithKey;
                                                adp.Fill(dt_upload);
                                            }
                                        }
                                        catch (Exception er)
                                        {
                                            throw new Exception(er.Message);
                                        }
                                 
                                }
                            }

Such re-use can get dataTable PrimaryKey property of the primary key information of the data information.

Check out the remaining functions: MissingSchemaAction 

Add: When filled directly over the accumulated data sets, with no primary key relationship (default)

AddWithKey: from the data source we can get a key which is the primary key, and then filled in accordance with the primary key, the master key will not be duplication of, usually selected in this way, of course, in this manner an inspection of the primary key. Particularly important for non-typed dataset

Error: If you choose this option, when filling UnTyped DataSet, because of UnTypedDataSet no data structure at this time will complain

          抛出一个InvalidOperationException:  Missing the 'Table' DataTable for the 'Table' SourceTable.

Ignore: ignore, that is not present in the case of architecture, filled with data will be ignored. Since Untyped DataSet schema does not, then it will ignore the padding,

Guess you like

Origin www.cnblogs.com/linweimu/p/11123218.html