此 SQL 命令的 C# SqlCommand 语法

C# SqlCommand syntax for this SQL command

我正在尝试将此 SQL 示例命令转换为 C# SqlCommand:

if not exists (select column_name
               from INFORMATION_SCHEMA.columns
               where table_name = 'TotalHeals'
                 and column_name = '"+Healee+"')
    alter table TotalHeals add +Healee+ int

到目前为止我有什么

// TO DO check to see if column exists instead of throwing error
cmd2 = new SqlCommand("ALTER TABLE TotalHeals ADD " + Healee + " INT",openCon);
cmd2.ExecuteNonQuery();

我完全不知道如何编写 if not exists 语句。有帮助吗?

您需要执行第一个 select 查询来检查该列是否像这样存在

cmd = new SqlCommand("Select count(*) from
                     INFORMATION_SCHEMA.columns
                     where
                     table_name = 'TotalHeals'
                     and column_name = '"+Healee+"',openCon);
int output=Convert.ToInt32(cmd.ExecuteScalar());

if (output>0)
 // column already present....handle that case..
else
//alter your table to add the column