1

I have a list of fields with descriptions and types that needs to be added to a table.

Eg. SomeField/SomeDescription/Decimal

How would I go about adding this columns with descrption and type to a sql server 2012 db using c#? I know how to add the column via sql, but I need to be able to do it in c#

2 Answers 2

1

Heres the solution I came up with.
Basically I added the columns using sql (i.e. Alter table etc...)
Then I called an existing sql stored procedure: sp_addextendedproperty

string[] allLines = File.ReadAllLines(@"C:\Table.csv");

            var query = from line in allLines
                        let data = line.Split(',')
                        select new
                        {
                            FieldName = data[0],
                            Description = data[1]
                        };


            try
            {
                string connection = "my connection string";
                using (SqlConnection conn = new SqlConnection(connection))///add your connection string
                {                    
                    SqlCommand command = new SqlCommand();
                    conn.Open();

                    using (SqlTransaction trans = conn.BeginTransaction())
                    {
                        command.Connection = conn;
                        command.Transaction = trans;

                        foreach (var item in query)
                        {
                            String sql = string.Format("ALTER TABLE MyTable ADD {0} Decimal(18,6)", item.FieldName.ToString());
                            command.CommandText = sql;
                            command.ExecuteNonQuery();
                        }

                        trans.Commit();
                    }


                    conn.Close();
                    conn.Open();
                    SqlTransaction transaction = conn.BeginTransaction();

                    using (SqlCommand cmd = new SqlCommand("sp_addextendedproperty", conn, transaction))
                    {


                            cmd.CommandType = CommandType.StoredProcedure;


                            foreach (var item in query)
                            {
                                cmd.Parameters.Clear();

                                cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = "MS_Description";
                                cmd.Parameters.Add("@value", SqlDbType.VarChar).Value = item.Description.ToString();
                                cmd.Parameters.Add("@level0type", SqlDbType.VarChar).Value = "SCHEMA";
                                cmd.Parameters.Add("@level0name", SqlDbType.VarChar).Value = "dbo";
                                cmd.Parameters.Add("@level1type", SqlDbType.VarChar).Value = "TABLE";
                                cmd.Parameters.Add("@level1name", SqlDbType.VarChar).Value = MyTable;
                                cmd.Parameters.Add("@level2type", SqlDbType.VarChar).Value = "COLUMN";
                                cmd.Parameters.Add("@level2name", SqlDbType.VarChar).Value = item.FieldName.ToString();


                                cmd.ExecuteNonQuery();
                            }

                            transaction.Commit();

                    }                    
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }   
Sign up to request clarification or add additional context in comments.

Comments

0

Something like that:

string connection = "your connection string";
try
{
  using(SqlConnection conn = new SqlConnection(connection))///add your connection string
  {
    String sql="ALter Table MyTable add SomeField Decimal(18,3)";
    conn.Open();
    using( SqlCommand command = new SqlCommand(sql,conn))
    {
      command.ExecuteNonQuery();
    }
  }
}
catch (Ecxeption ex)
{
 MessageBox.Show(ex.Message);
}

3 Comments

Yeah, but I would need to add the description to the column as well.
Sorry, What do you mean by description of the column?
In sql server one can add column descriptions. If you check out the properties of a column in visual studio, you will see a Description attribute in which one can describe the column

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.