读书人

急送10分求一段检查库中时候存在数据

发布时间: 2011-12-30 23:30:45 作者: rapoo

急送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();                //失            } 

读书人网 >C#

热点推荐