SQL Express Edition,SQL Compact Edition和SQLCMD用于学习目的

时间:2010-02-27 19:19:22

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

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

  1. 由于在命令提示符命令下运行sqlcmd -S.\sqlexpress会给出“1>”提示我假设我已经安装了SQL Express但是无论如何我怎么能确定我在我的机器上安装了什么,因为我在安装的程序中找不到SQL Express Edition名称?

  2. 我可以在使用SQL EE(嵌入式?)创建的C#(VC#Express)应用程序中运送和使用数据库吗?

  3. 如何使用sqlcmd来学习SQL,即通过发出create, use, select...之类的命令,再次强调是学习SQL我不想运行脚本但是使用交互式命令提示符MySQL(因为我想使用SQL,我非常希望避免使用DBMS的图形工具)?

  4. 请告诉我你是否有一些其他建议,关于我应该更好地学习如何在SQL中编程,或者我现在应该坚持使用上述内容。

  5. 提前致谢。

3 个答案:

答案 0 :(得分:0)

您将需要使用Microsoft SQL Server Management Studio Express。它更容易,您可以保存您的查询。你可以直接从微软下载它,只需谷歌吧。

要将其与自定义程序一起使用,用户必须安装Express,除非您将数据库转换为其他内容。许多制作使用SQL Server数据库的程序的公司都将其产品与SQL Express一起发布。

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

答案 1 :(得分:0)

开始使用SQL的好地方是sql zoo

答案 2 :(得分:0)

我制作了一个小程序: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