我正在尝试将此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不存在语句的代码。有什么帮助吗?
您需要执行第一个选择查询以检查该列是否存在,如下所示
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句