title: How to use SQL Server database as datasource for MS Word Mail Merge in C#
body: <p>I have a word document used as my mail merge template. I am able to connect an Excel spreadsheet or Access database using wordDocument.MailMerge.OpenDataSource, but am pretty lost when it comes to connecting it to a SQL database. </p>
  
  <pre><code>SqlConnection myConnection = new SqlConnection("server=(local);" +
                                         "Trusted_Connection=yes;" +
                                         "database=MailMerge; " +
                                         "connection timeout=30");
  object connection = myConnection;
  object query = "SELECT * FROM 'FHB'";
  string dbPath = @"C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\MailMerge.mdf";
  
  wordDocument.MailMerge.OpenDataSource(dbPath, ref missing, ref missing, ref missing, ref missing,
      ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, connection,
      query, ref missing, ref missing, ref missing);
  </code></pre>
  
  <p>I would receive errors indicating a "COMException was unhandled" and that there was a "Type mismatch". As you can see in the code, I'm trying to use the file path to my database as the parameter for the the OpenDataSource method, as well as the SQL connection and query.</p>