2010-02-27 58 views
0

我想從一些SQL教程網站上學習SQL編程,但我需要一些執行查詢的環境。我想我的計算機上同時安裝了SQL CE和SQL EE,但是我對這些DBMS有一些疑問,並且我不知道如何使用SQLCMD實用程序,所以我希望這裏有人會有時間和意願向我解釋以下內容:用於學習目的的SQL Express Edition,SQL Compact Editin和SQLCMD

  1. 因爲在命令運行sqlcmd -S.\sqlexpress提示命令給出「1>」提示符我想我是SQL Express安裝,但無論如何,我怎麼能知道我已經安裝了我的機器,因爲我不能在已安裝程序的SQL Express找到版本名稱?

  2. 我可以在我的C#(VC#Express)應用程序中使用SQL EE(嵌入式?)創建數據庫嗎?

  3. 如何使用sqlcmd學習SQL,這是通過發出像create, use, select...命令,再次強調的是學習SQL,我不希望運行腳本,但使用的交互式命令提示符喜歡使用MySQL(因爲我想用SQL我非常想避免使用DBMS的圖形工具)?

  4. 請告訴我,如果您有其他建議,應該如何更好地使用SQL來學習如何編程,或者我現在應該堅持使用上述方法。

在此先感謝。

回答

0

您將需要使用Microsoft SQL Server Management Studio Express。這很容易,你可以保存你的查詢。你可以直接從微軟下載它,只需谷歌它。

要將其與自定義程序一起使用,用戶必須安裝Express,除非將數據庫轉換爲其他內容。許多使用SQL Server數據庫程序的公司將其產品與SQL Express部分一起運送。

開始學習SQL的好地方是w3schools。

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 
相關問題