While you can make a coded part that work, i would recommend you consider what you want to do and then do it in another way than through code?
Anyway you can do like this
using System;
using System.Data;
using System.Data.SqlClient;
namespace Ramblings
{
public class Inspector
{
public string ConnectionString;
public void AddFieldToTable(string tableName, string columnName, string sqlType, bool nullable = true)
{
using (var cn = new SqlConnection(ConnectionString))
{
cn.Open();
string SQL = "select top 1 * from " + tableName;
using (var cmd = new SqlCommand(SQL, cn))
{
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.KeyInfo);
DataTable defTable = reader.GetSchemaTable();
if (defTable == null) throw new ArgumentException("No table returned for " + tableName, tableName);
reader.Close();
foreach (DataRow columnDescRow in defTable.Rows)
{
if (columnDescRow["ColumnName"].ToString() == columnName)
{
return;
}
}
SQL = "ALTER TABLE " + tableName + " ADD " + columnName + " " + sqlType + " " + (nullable ? "NULL" : "NOT NULL");
cmd.CommandText = SQL;
cmd.ExecuteNonQuery();
}
}
}
}
}