1

在我为学校制作的一个小型应用程序中,我试图确定是否存在必要的数据库。

如果没有,我想创建它(连同数据库中的表),然后照常使用它。

我正在使用 Visual C# Express Edition 以及 SQL Server Express。我在下面写了一个小测试程序......

namespace DatabaseConnectionTest
{
    class Program
    {
        public static SqlConnection con;

        public static void EstablishConnection()
        {
            string userName = "username";
            string password = "password";
            string server = @".\SQLEXPRESS";
            string database = "Blibbity";
            string trustedConnection = "yes";
            string timeout = "30";

            try
            {
                con = new SqlConnection(
                        "user id=" + userName +
                        ";password=" + password +
                        ";server=" + server +
                        ";Trusted_Connection=" + trustedConnection +
                        ";database=" + database +
                        ";connection timeout=" + timeout);
                con.Open();
                Console.WriteLine("Connection successful!");

                var command = new SqlCommand("insert into sometable values ('somedata')", con);
                command.ExecuteNonQuery();
                Console.WriteLine("Insert successful!");

                command = new SqlCommand("select somecolumn from sometable", con);
                SqlDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    Console.WriteLine("Fectched data ====> " + reader["somecolumn"].ToString());
                }

                reader.Close();
                Console.WriteLine("Query successful!");

                command = new SqlCommand("delete from sometable where somecolumn = 'somedata'", con);
                command.ExecuteNonQuery();
                Console.WriteLine("Delete successful!");
            }
            catch
            {
                database = "master";

                con = new SqlConnection(
                    "user id=" + userName +
                    ";password=" + password +
                    ";server=" + server +
                    ";Trusted_Connection=" + trustedConnection +
                    ";database=" + database +
                    ";connection timeout=" + timeout);

                con.Open();

                var command = new SqlCommand(@"
                    USE [master]
                    GO

                    /****** Object:  Database [Blibbity]    Script Date: 04/12/2012 07:08:45 ******/
                    CREATE DATABASE [Blibbity] ON  PRIMARY 
                    ( NAME = N'Blibbity', FILENAME = N'c:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\Blibbity.mdf' , SIZE = 2048KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
                     LOG ON 
                    ( NAME = N'Blibbity_log', FILENAME = N'c:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\Blibbity_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
                    GO

                    ALTER DATABASE [Blibbity] SET COMPATIBILITY_LEVEL = 100
                    GO

                    IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
                    begin
                    EXEC [Blibbity].[dbo].[sp_fulltext_database] @action = 'enable'
                    end
                    GO

                    ALTER DATABASE [Blibbity] SET ANSI_NULL_DEFAULT OFF 
                    GO

                    ALTER DATABASE [Blibbity] SET ANSI_NULLS OFF 
                    GO

                    ALTER DATABASE [Blibbity] SET ANSI_PADDING OFF 
                    GO

                    ALTER DATABASE [Blibbity] SET ANSI_WARNINGS OFF 
                    GO

                    ALTER DATABASE [Blibbity] SET ARITHABORT OFF 
                    GO

                    ALTER DATABASE [Blibbity] SET AUTO_CLOSE OFF 
                    GO

                    ALTER DATABASE [Blibbity] SET AUTO_CREATE_STATISTICS ON 
                    GO

                    ALTER DATABASE [Blibbity] SET AUTO_SHRINK OFF 
                    GO

                    ALTER DATABASE [Blibbity] SET AUTO_UPDATE_STATISTICS ON 
                    GO

                    ALTER DATABASE [Blibbity] SET CURSOR_CLOSE_ON_COMMIT OFF 
                    GO

                    ALTER DATABASE [Blibbity] SET CURSOR_DEFAULT  GLOBAL 
                    GO

                    ALTER DATABASE [Blibbity] SET CONCAT_NULL_YIELDS_NULL OFF 
                    GO

                    ALTER DATABASE [Blibbity] SET NUMERIC_ROUNDABORT OFF 
                    GO

                    ALTER DATABASE [Blibbity] SET QUOTED_IDENTIFIER OFF 
                    GO

                    ALTER DATABASE [Blibbity] SET RECURSIVE_TRIGGERS OFF 
                    GO

                    ALTER DATABASE [Blibbity] SET  DISABLE_BROKER 
                    GO

                    ALTER DATABASE [Blibbity] SET AUTO_UPDATE_STATISTICS_ASYNC OFF 
                    GO

                    ALTER DATABASE [Blibbity] SET DATE_CORRELATION_OPTIMIZATION OFF 
                    GO

                    ALTER DATABASE [Blibbity] SET TRUSTWORTHY OFF 
                    GO

                    ALTER DATABASE [Blibbity] SET ALLOW_SNAPSHOT_ISOLATION OFF 
                    GO

                    ALTER DATABASE [Blibbity] SET PARAMETERIZATION SIMPLE 
                    GO

                    ALTER DATABASE [Blibbity] SET READ_COMMITTED_SNAPSHOT OFF 
                    GO

                    ALTER DATABASE [Blibbity] SET HONOR_BROKER_PRIORITY OFF 
                    GO

                    ALTER DATABASE [Blibbity] SET  READ_WRITE 
                    GO

                    ALTER DATABASE [Blibbity] SET RECOVERY SIMPLE 
                    GO

                    ALTER DATABASE [Blibbity] SET  MULTI_USER 
                    GO

                    ALTER DATABASE [Blibbity] SET PAGE_VERIFY CHECKSUM  
                    GO

                    ALTER DATABASE [Blibbity] SET DB_CHAINING OFF 
                    GO", con);

                command.ExecuteNonQuery();
                con.Close();

                database = "Blibbity";

                con = new SqlConnection(
                    "user id=" + userName +
                    ";password=" + password +
                    ";server=" + server +
                    ";Trusted_Connection=" + trustedConnection +
                    ";database=" + database +
                    ";connection timeout=" + timeout);

                con.Open();

                command = new SqlCommand(@"
                    USE [Blibbity]
                    GO

                    /****** Object:  Table [dbo].[sometable]    Script Date: 04/12/2012 07:09:07 ******/
                    SET ANSI_NULLS ON
                    GO

                    SET QUOTED_IDENTIFIER ON
                    GO

                    SET ANSI_PADDING ON
                    GO

                    CREATE TABLE [dbo].[sometable](
                        [somecolumn] [varchar](50) NULL
                    ) ON [PRIMARY]

                    GO

                    SET ANSI_PADDING OFF
                    GO", con);

                command.ExecuteNonQuery();
                con.Close();

                EstablishConnection();
            }
            finally
            {
                con.Close();
                Console.WriteLine("Connection now closed...");
                Console.ReadLine();
            }
        }

        static void Main(string[] args)
        {
            EstablishConnection();
        }
    }
}

显然Blibbity只是一个垃圾数据库。当我点击ExecuteNonQuery()异常捕获中的第一行时,它告诉我附近的语法"GO"不正确,但我只是使用 SQL Server Management Studio 的“Script As CREATE TO”功能复制了用于创建数据库/表的文本。

有人知道我为什么遇到这个问题吗?

非常感谢。

4

2 回答 2

4

GO 不是 sql server 命令,它由 Management Studio 解释。如果要实现相同的行为,则必须通过 go 指令拆分 sql 命令并顺序执行每个部分。

于 2012-04-12T13:13:58.363 回答
1

我建议您使用实体框架,而不是手动创建数据库,它在代码优先的方法中具有内置支持,如果它不存在,则可以创建新数据库、添加表甚至用初始数据填充它们。

因此,与其寻找解决问题的方法,不如解决问题本身。

于 2012-04-12T13:13:47.950 回答