如何使用c#检查sql server 2008中是否存在表。 [英] how to check if a table exist in sql server 2008 using c#.

查看:241
本文介绍了如何使用c#检查sql server 2008中是否存在表。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用c#检查sql server 2008中的数据库中是否存在表。

how to check if a table exist in a database in sql server 2008 using c#.

推荐答案

是这样的:



like that:

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test_DbTableExistance
{
    class Program
    {
        static void Main(string[] args)
        {
            string connStr = @"data source=.\sqlexpress; initial catalog=KKD; integrated security=true";
            string tableQuery = @"select 1 from INFORMATION_SCHEMA.TABLES where TABLE_NAME='{0}'";

            try
            {
                string cmdText = string.Format(tableQuery, "DISPLAY_STAT");
                using (SqlConnection conn = new SqlConnection(connStr))
                {
                    conn.Open();
                    using (SqlCommand cmd = new SqlCommand(cmdText, conn))
                    {
                        object o = cmd.ExecuteScalar();
                        Console.WriteLine(o == null ? "none" : "exists");
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
    }
}


看看这个:我如何检查在sql server 2008 R2上存在我的表C#? [ ^ ]


这篇关于如何使用c#检查sql server 2008中是否存在表。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆