vendredi 21 octobre 2016

If statement, "Else", block executes code when it shouldn't

I'm making use of SqlDataReader.GetSchemaTable for the first time and think I might be using it incorrectly. In my if statement below the else block executes even though the column exists.

I should mention that the item.Property is a string with number characters. From past experience I've become accustomed to concatenating the required parenthesis. Removing the parenthesis makes no change to the flow of execution.

DataTable Schema;
using (SqlConnection myCon = new SqlConnection(constring)
{
    using (SqlCommand cmd = new SqlCommand("SELECT * FROM Table_Name", myCon)
    {
        SqlDataReader Reader;
        myCon.Open();
        Reader = cmd.ExecuteReader();
        Schema = Reader.GetSchemaTable();
    }
}

foreach(var item in array)
{
    if(Schema.Columns.Contains(item.Property)
    {

    }
    else
    {
        using (SqlConnection myCon = new SqlConnection(connstring))
        {
            using (SqlCommand cmd = new SqlCommand("ALTER TABLE TableName ADD " + item.Property + " bit;", myCon))
            {
                SqlDataReader Reader;
                myCon.Open();
                Reader = cmd.ExecuteReader();
                Reader.Read();
            }
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire