How to determine the type of value of a column in a DataRow?
<p>I have the following method: <code>public static void Post(this DataRow dr, string tableName, SqlConnection conn)</code></p>

<p>I'm trying to build an <code>INSERT</code> statement for my <code>DataRow</code>, but I'm running into difficulty trying to determine the type of data each column is in order to format the SQL statement correctly.</p>

<p>I have this:</p>

<pre><code>public static void Post(this DataRow dr, string tableName, SqlConnection conn)
{
    string sql = "INSERT INTO " + tableName + " VALUES(";

    for (int i = 0; i &lt; dr.ItemArray.Length; i++)
    {
        sql += "'" + dr[i].ToString() + "'";

        if (i != dr.ItemArray.Length - 1)
            sql += ",";
    }
}
</code></pre>

<p>But there are numerous Date and Numeric columns in the table, so obviously not all of my values can be enclosed in <code>'</code>.</p>

<p>I know that starting from a <code>DataTable</code>, you can do something like this: <code>myDt.Columns[0].Datatype</code> which I could then use to branch the logic out and format the values accordingly but I cannot seem to find how to access this information when given just a <code>DataRow</code>.</p>

<p>Worst case scenario, I <em>can</em> refactor the application to simply take a <code>DataTable</code> instead of an individual <code>DataRow</code>, but this would require significant work so I'd much prefer to find a way to do it as is.</p>

<p>If it matters, this is a winforms application / .NET 4.5</p>