Note that there are some explanatory texts on larger screens.

plurals
  1. POLINQ to SQL INSERT WHERE not in collection
    primarykey
    data
    text
    <p>I need to mirror all of the DNS entries on our DNS server in a SQL database. I am able to query DNS using WMI and C#. And I can query SQL for existing records using L2S but there is a lot of room for improvement.</p> <p>What does the LINQ To SQL statement look like that will insert records that are not in the ManagementObjectCollection returned by WMI into the SQL table?</p> <p>What I have so far works but seems inefficient.</p> <pre><code>internal static void SyncRecordInfo() { int affected = 0; DnsEntityDataContext db = new DnsEntityDataContext(); //Select records in DNS string[] Types = { "A", "CName", "MX" }; for (int i = 0; i &lt; Types.Length; i++) { string Query = "SELECT * FROM MicrosoftDNS_" + Types[i] + "Type"; ObjectQuery qry = new ObjectQuery(Query); DnsProvider dns = new DnsProvider(); ManagementObjectSearcher s = new ManagementObjectSearcher(dns.Session, qry); ManagementObjectCollection col = s.Get(); dns.Dispose(); foreach (var item in col) { //(from m in ManagementObjectCollection select m.SomeID).Contains(&lt;main source field&gt;) int count = (from r in db.Records where ((r.Data.Contains(item["RecordData"].ToString())) &amp;&amp; (r.Name.Contains(item["OwnerName"].ToString()))) select r).Count(); if (count &lt; 1) { //Insert records in SQL Record record = new Record(); record.Data = item["RecordData"].ToString(); record.Name = item["OwnerName"].ToString(); //record.Notes = item["ContainerName"].ToString(); record.Type = Types[i]; db.Records.InsertOnSubmit(record); db.SubmitChanges(); affected++; } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload