日期:2014-05-18 浏览次数:20835 次
private bool ExistsTable(string TableName) { string sql = "select count(*) from sysobjects where name='" + TableName+"' and type='U'"; SqlConnection cn = new SqlConnection("連接字符串"); SqlCommand cmd = new SqlCommand(sql, cn); try { cn.Open(); int cnt = Convert.ToInt32(cmd.ExecuteScalar()); return (cnt == 0) ? false : true; } finally { cn.Close(); cn.Dispose(); cmd.Dispose(); } }
------解决方案--------------------
string sql = "create table tbname(col1 int primary key not null,col2 varchar(20) not null)"; SqlConnection cn = new SqlConnection("連接字符串"); SqlCommand cmd = new SqlCommand(sql, cn); try { cn.Open(); cmd.ExecuteNonQuery(); //不出異常,就已經成功 } finally { cn.Close(); cn.Dispose(); cmd.Dispose(); //失敗 }