创建数据库。为什么不起作用?

时间:2014-06-19 21:14:44

标签: c# sql sql-server

我有问题。我想用C#创建一个Books数据库,其中包含serialNumber,Author,Name和Year of published列。我做了代码,我没有任何错误,但当我启动它时,控制台是黑色的,它什么也没做。有人能告诉我为什么它不起作用吗?

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

namespace T4DB1
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnection conn = new SqlConnection("Data Source=localhost; Initial Catalog=master; Integrated Security = True");
            {
                string create = "if not exists (select * from sys.databases where name='Buecher') create database [Buecher] else begin drop database [Buecher] create database [Buecher] end ";
                SqlCommand createDB = new SqlCommand(create, conn);
                try
                {
                    conn.Open();
                    createDB.ExecuteNonQuery();
                    Console.WriteLine("Database created");
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error");
                }
                finally
                {
                    if (conn.State == System.Data.ConnectionState.Open)
                    {
                        conn.Close();
                    }
                }
                conn = new SqlConnection("Data Source = localhost; Initial Catalog=Buecher; Integrated Security = True");
                create = " create table Buecher1 ( ISBNNummer varchar(10) not null Primary Key, Autor varchar(40), Titel varchar(50), Erscheinungsjahr smallint)";
                createDB = new SqlCommand(create, conn);
                try
                {
                    conn.Open();
                    createDB.ExecuteNonQuery();
                    Console.WriteLine("Table created");
                    string insertrow = "insert into Buecher1 (ISBNNummer, Autor, Titel, Erscheinungsjahr) values('2658A42', 'Douglas Adams', 'Galaxy', 2007)";
                    createDB = new SqlCommand(insertrow, conn);
                    createDB.ExecuteNonQuery();
                    string insertrow2 = "insert into Buecher1 (ISBNNummer, Autor, Titel, Erscheinungsjahr) values('58624FG85', 'Charles Dickens', 'White Fang',1992)";
                    createDB = new SqlCommand(insertrow2, conn);
                    createDB.ExecuteNonQuery();
                    string insertrow3 = "insert into Buecher1 (ISBNNummer, Autor, Titel, Erscheinungsjahr) values('65224AS4', 'Erik Corr', 'Somewhere', 2014)";
                    createDB = new SqlCommand(insertrow3, conn);
                    createDB.ExecuteNonQuery();
                    string select = "Select * From Buecher1";
                    SqlDataAdapter da = new SqlDataAdapter(select, conn);
                    DataTable dt = new DataTable(); da.Fill(dt);
                    foreach (DataRow row in dt.Rows)
                    {
                        Console.WriteLine("ISBNNummer: " + row["ISBNNummer"] + " " + "Autor: " + row["Autor"] + " " + "Titel: " + row["Titel"] + " " + "Erscheinungsjahr:" + row["Erscheinungsjahr"]);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
                finally
                {
                    if (conn.State == System.Data.ConnectionState.Open)
                    {
                        conn.Close();
                    }
                }
            }
            Console.ReadKey();
        }
    }
}

1 个答案:

答案 0 :(得分:3)

我假设您使用的是SQL Server。请考虑以下陈述:

if not exists (select * from sys.databases where name='Buecher')
    create database [Buecher] 

它的行为与您的预期不符。该命令首先编译。在此阶段,如果存在Buecher,您将在create database [Buecher]'上收到错误,因为您无法创建已存在的数据库。你可能会抗议,#34;但这就是为什么我有if!"太糟糕了。编译器没有监听。在执行阶段之后 - 编译阶段之后,您会看到if没有被执行。

drop也是如此。

处理此问题的典型方法是使用动态SQL。这推迟了编译直到执行。所以,这个片段应该可以工作:

if not exists (select * from sys.databases where name='Buecher')
    exec('create database [Buecher] ');