首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

急送10分,求一段检查库中时候存在数据表的的.cs代码?解决思路

2011-12-30 
急送10分,求一段检查库中时候存在数据表的的.cs代码?要求:如果存在要查找的数据表返回一个值即可,如果没有

急送10分,求一段检查库中时候存在数据表的的.cs代码?
要求:如果存在要查找的数据表返回一个值即可,如果没有此该库则建立此库。

[解决办法]

C# code
        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();            }        }
[解决办法]
C# code
            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();                //失敗            } 

热点排行