Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wrote following sql query to add a column to a table.
but i want to find first whether any existing column has the same name which i dont know how to do.
I have put "comm.ExecuteNonQuery()" in Try-Catch statement- as it give exception when column name exist.
Am i doing it correct???

One more question : When writing following sql it creates column with data type:yes/No, this is what i want but it takes value 0/-1 but i have to check for true/false.

C#
ConnectionToDB();
string sql = "ALTER TABLE table_name ADD Proj bit";
comm.CommandType = CommandType.Text;
comm.CommandText = sql;
comm.Connection = conn;
conn.Open();
comm.ExecuteNonQuery();
conn.Close();


Thanks in advance
Posted
Comments
Thanks7872 16-Sep-14 3:11am    
Altering table through code? Why?

 
Share this answer
 
Hello Try this
If Not Exists(Select b.name from sysObjects a , syscolumns b 
where b.id = a.Id and a.Type ='U' and b.name = 'columnname' 
and a.name = 'tblname')
	Begin
		ALTER TABLE tblname ADD columnname INT 
	End
GO


thanks
 
Share this answer
 
Hi,

Check this...Does a Database/Table/Column Exist in SQL Server[^]

But still....why do you want to perform it through code?


Hope this will help you

Cheers
 
Share this answer
 
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();
                }
            }
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900