【问题标题】:SQL Express Edition, SQL Compact Editin and SQLCMD for learning purpose用于学习目的的 SQL Express Edition、SQL Compact Editin 和 SQLCMD
【发布时间】:2010-02-27 19:19:22
【问题描述】:

我想从我在这里听说过的一些 SQL 教程网站学习 SQL 编程,但我需要一些环境来执行查询。我想我的计算机上同时安装了 SQL CE 和 SQL EE,但我对这些 DBMS 有一些疑问,我不确切知道如何使用 SQLCMD 实用程序,所以我希望这里有人有时间并会向我解释以下内容:

    1234563速成版名称?
  1. 我可以通过我的 C# (VC# Express) 应用程序交付和使用数据库,该应用程序是使用 SQL EE(嵌入式?)创建的?

  2. 1234563想要使用 SQL 我很想避免 DBMS 的图形工具)?
  3. 如果您有其他建议,请告诉我,关于在学习如何使用 SQL 编程时我应该更好地使用什么,或者我现在应该坚持上述建议。

提前致谢。

【问题讨论】:

    标签: sql sql-server-ce sql-server-express


    【解决方案1】:

    您将需要使用 Microsoft SQL Server Management Studio Express。这要容易得多,您可以保存查询。您可以直接从 microsoft 下载,只需 google 即可。

    要将它与自定义程序一起使用,用户还必须安装 Express,除非您将数据库转换为其他内容。许多使用 SQL Server 数据库制作程序的公司在他们的产品中包含 SQL Express 的一部分。

    w3schools 是开始学习 SQL 的好地方。

    【讨论】:

      【解决方案2】:

      SQL 入门的好地方是sql zoo

      【讨论】:

        【解决方案3】:

        我做了一个小程序:SqlCdm 类似但用于 SDF 文件。 这是代码,下面是一个简单的测试。

        我的程序的使用说明如下:

        SqlCeCmd: [--create-database] [--shrink-database] --connection-string <connection_string> -f <sql_file>
          if create-database flag is present, it will be created for you.
          if shrink-database is present, the database will be shrinked.
        WARNING: only one sqlfile can be provided
        

        可能是SqlCmd 支持吧?但是这里的任何方式都是代码

        SqlCeCmd.cs:

        using System;
        using System.Collections.Generic;
        using System.Text;
        using System.Data.SqlServerCe;
        using System.Data.Common;
        using System.Data;
        using System.IO;
        
        namespace SqlCeCmd
        {
          /// <summary>
          /// A basic SqlCeCmd to be an equivalent to SqlCmd but for sdf files.
          /// </summary>
          public static class SqlCeCmd
          {
        
            private static void showUsage()
            {
              Console.Out.WriteLine("SqlCeCmd: [--create-database] [--shrink-database] --connection-string <connection_string> -f <sql_file>");
              Console.Out.WriteLine("  if create-database flag is present, it will be created for you.");
              Console.Out.WriteLine("  if shrink-database is present, the database will be shrinked.");
              Console.Out.WriteLine("WARNING: only one sqlfile can be provided");
            }
        
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main(string[] args)
            {
              if (args.Length == 0)
              {
                showUsage();
                return;
              }
        
              // parsing arguments
              string connectionString = "";
              string sqlFile = "";
              bool createDatabase = false;
              bool shrinkDatabase = false;
        
        
              bool nextIsConnectionString = false;
              bool nextIsSqlFile = false;
              foreach (string arg in args)
              {
        
                // ConnectionString
                if (nextIsConnectionString)
                {
                  connectionString = arg;
                  nextIsConnectionString = false;
                }
                if (arg.Equals("--connection-string"))
                  nextIsConnectionString = true;
        
                // SqlFile
                if (nextIsSqlFile)
                {
                  sqlFile = arg;
                  nextIsSqlFile = false;
                }
                if (arg.Equals("-f"))
                  nextIsSqlFile = true;
        
                if (arg.Equals("--create-database"))
                  createDatabase = true;
                if (arg.Equals("--shrink-database"))
                  shrinkDatabase = true;
        
              }
        
              if (connectionString == "")
              {
                Console.Out.WriteLine("error: can't find --connection-string <connection_string>");
                showUsage();
                return;
              }
        
              if (!createDatabase && sqlFile == "")
              {
                Console.Out.WriteLine("error: can't find -f <sql_file>");
                showUsage();
                return;
              }
        
              // creating database?
              if (createDatabase)
                createSdfDatabase(connectionString);
              if (shrinkDatabase)
                shrinkSdfDatabase(connectionString);
        
              // executing queies
              if (sqlFile != "")
              {
                Console.Out.WriteLine("connectionString: " + connectionString);
                Console.Out.WriteLine("sqlFile: " + sqlFile);
                executeSqlFile(connectionString, sqlFile);
              }
            }
        
            private static void createSdfDatabase(string connectionString)
            {
              Console.Out.WriteLine("Creating database: " + connectionString);
              new SqlCeEngine(connectionString).CreateDatabase();
            }
            private static void shrinkSdfDatabase(string connectionString)
            {
              Console.Out.WriteLine("Shrinking database: " + connectionString);
              new SqlCeEngine(connectionString).Shrink();
            }
        
            public static void executeSqlFile(String connectionString, String sqlFile)
            {
        
              IDbConnection cn = new SqlCeConnection(connectionString);
              cn.Open();
              string lastQuery = ""; // for debug only
              try
              {
                foreach (string query in splitCaseInsensitive(readWholeFile(sqlFile), "go"))
                {
                  if (!query.Trim().Equals(""))
                  {
                    lastQuery = query;
                    executeSqlQuery(cn, query);
                  }
                }
              }
              catch (Exception e)
              {
                Console.Out.WriteLine("error: executing " + lastQuery);
                Console.Out.WriteLine("-----------------------------");
                Console.Out.WriteLine(e.StackTrace);
                Console.Out.WriteLine("-----------------------------");
              }
              finally
              {
                cn.Close();
              }
            }
        
            private static void executeSqlQuery(IDbConnection cn, string query)
            {
              IDbCommand cmd = new SqlCeCommand(query);
              cmd.Connection = cn;
              cmd.CommandType = CommandType.Text;
              cmd.ExecuteNonQuery();
            }
        
        
            // ************************
            // Util
            // ************************
            public static String[] split(String text, String delimiter)
            {
              return split(text, delimiter, false);
            }
        
            public static String[] splitCaseInsensitive(String text, String delimiter)
            {
              return split(text, delimiter, true);
            }
        
            private static String[] split(String text, String delimiter, bool caseSensitive)
            {
              List<String> splitted = new List<String>();
              String remaining = text;
        
              while (remaining.IndexOf(delimiter) > -1)
              {
                splitted.Add(leftMost(remaining, delimiter));
                remaining = right(remaining, delimiter);
              }
              splitted.Add(remaining);
        
              return splitted.ToArray();
            }
        
            /// <summary>
            /// 
            /// </summary>
            /// <param name="expression">The string to split</param>
            /// <param name="delimiter">The splitting delimiter</param>
            /// <returns>The left most part of the string</returns>
            public static string leftMost(string expression, string delimiter)
            {
              int index = expression.IndexOf(delimiter);
              if (index > 0)
              {
                return expression.Substring(0, index);
              }
              return "";
            }
        
            /// <summary>
            /// 
            /// </summary>
            /// <param name="expression">The string to split</param>
            /// <param name="delimiter">The splitting delimiter</param>
            /// <returns>Return the right part of an expression</returns>
            public static string right(string expression, string delimiter)
            {
              int index = expression.IndexOf(delimiter);
              if (index > -1 && index < (expression.Length - 1))
              {
                return expression.Substring(index + delimiter.Length, expression.Length - index - delimiter.Length);
              }
              return "";
            }
        
            /// <summary>
            /// Read the whole file and return its content
            /// </summary>
            /// <param name="path">path for existing file used to read from</param>
            /// <returns>The whole content of the file</returns>
            public static string readWholeFile(string path)
            {
              StreamReader reader = File.OpenText(path);
              string content = reader.ReadToEnd();
              reader.Close();
              return content;
            }
          }
        }
        

        TestSqlCeCmd.bat:

        @echo off
        
        :: Creating the Test.sql file
        echo create table test (id int, test nvarchar(100)) > Test.sql
        echo go >> Test.sql
        echo create table test2 (id int, test nvarchar(100)) >> Test.sql
        
        rm -f Test.sdf
        SqlCeCmd.exe --create-database --connection-string "data source='Test.sdf'; mode=Exclusive; LCID=3084" -f "Test.sql"
        
        :: an error should be raised here
        :: SqlCeCmd.exe --connection-string "data source='Test.sdf'; mode=Exclusive; LCID=3084" -f Test.sql
        
        pause
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多