title: Generating sql statements in c# - insert tabe value into new table
body: <p>I'm writing a small program that converts csv files into a seried of insert statements into the database. The program has no knowledge of the database, so the statements written are a once-off thing, they can't be dynamic.</p>
  
  <p>Now, each dataset has it's own guid, and I want to have the statements query the database to determine if the dataset exists, based on it's name. So I have:</p>
  
  <pre><code>sw.WriteLine("-- Determine if dataset exists from school name");
  sw.WriteLine(string.Format(
  "INSERT INTO DataSets (Guid, DataSets ) VALUES ('{0}', '{1}') WHERE NOT EXISTS (SELECT * FROM DataSets WHERE DataSetName = '{1}')",
  Guid,
  DataSetName 
  ));
  </code></pre>
  
  <p>(Please correct me if I'm wrong, sql really isn't my forte)
  Now, the next issue is that the Guid for the dataset is used in some of the other tables, such as:</p>
  
  <pre><code>string sql = string.Format(
                      "INSERT INTO Table1 VALUES ('{0}', '{1}', '{2}', {3}, '{4}');",
                      object.value1
                      object.value1
                      object.value1
                      object.value1
                      Guid
  </code></pre>
  
  <p>'Guid' cannot be used as a variable in c# though, it could be either a new Guid (if 'datasets' doesn't exist in the database) or an existing guid (if 'datasets' exists, get the existing guid)</p>
  
  <p>So I will need something like:</p>
  
  <pre><code>string sql = string.Format(
                      "INSERT INTO Table1 (Data1, Data2, Data3, Data4) VALUES ('{0}', '{1}', '{2}', {3});",
                      object.value1
                      object.value1
                      object.value1
                      object.value1
  string sql2 = string.Format("INSERT INTO Table1 (Data5) VALUES (SELECT Guid FROM Datasets WHERE DataSetName = '{0}');
                      Guid
                      );
  </code></pre>
  
  <p>Is this a good (even valid?) way of going about this?</p>