C# sqlserver 判断表是否存在

转自:http://hi.baidu.com/gaocher/item/e5aca502dade2898a2df4341

string link = File.ReadAllText("database.txt");
conn = new SqlConnection(link);

            try
            {
                conn.Open();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = "if object_id( 'baseinfo ') is not null select 1 else select 0";

                if ((int)cmd.ExecuteScalar() == 0)
                {
                    cmd.CommandText = @"CREATE TABLE baseinfo (id bigint   identity(1,1) 
primary key,departure varchar(10),arrival varchar(10),depdate varchar(20),arrdate varchar(20),
flightCode varchar(10),departTime varchar(20),arrivalTime varchar(20),departAirportName
 varchar(20),departAirportCode varchar(10),arrivalAirportName varchar(20),arrivalAirportCode 
varchar(10),isDirect bit)";
                    cmd.ExecuteNonQuery();
                }

                cmd.CommandText = "if object_id( 'venderinfo ') is not null select 1 else select 0";

                if ((int)cmd.ExecuteScalar() == 0)
                {
                    cmd.CommandText = @"CREATE TABLE venderinfo (id bigint   identity(1,1) 
primary key,flight_id bigint,vendername nvarchar(30),venderprice int,venderfee int)";
                    cmd.ExecuteNonQuery();
                }
            }

            catch (SqlException e)
            {
                Console.WriteLine("数据库连接错误,请确认database.txt中的数据库正确连接");
                Environment.Exit(-1);
            }


你可能感兴趣的:(C# sqlserver 判断表是否存在)