???紵 发表于 2018-10-19 10:57:47

Use .NET (C#) with VS to connect Azure SQL DB-Ricky

using System;  
using System.Data.SqlClient;
  
using System.Text;
  
namespace sqltest
  
{
  
    class Program
  
    {
  
      static void Main(string[] args)
  
      {
  
            try
  
            {
  
                SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
  
                builder.DataSource = "your_server.database.windows.net";
  
                builder.UserID = "your_user";
  
                builder.Password = "your_password";
  
                builder.InitialCatalog = "your_database";
  
                using (SqlConnection connection = new SqlConnection(builder.ConnectionString))
  
                {
  
                  Console.WriteLine("\nQuery data example:");
  
                  Console.WriteLine("=========================================\n");
  
                  connection.Open();
  
                  StringBuilder sb = new StringBuilder();
  
                  sb.Append("SELECT TOP 20 pc.Name as CategoryName, p.name as ProductName ");
  
                  sb.Append("FROM . pc ");
  
                  sb.Append("JOIN . p ");
  
                  sb.Append("ON pc.productcategoryid = p.productcategoryid;");
  
                  String sql = sb.ToString();
  
                  using (SqlCommand command = new SqlCommand(sql, connection))
  
                  {
  
                        using (SqlDataReader reader = command.ExecuteReader())
  
                        {
  
                            while (reader.Read())
  
                            {
  
                              Console.WriteLine("{0} {1}", reader.GetString(0), reader.GetString(1));
  
                            }
  
                        }
  
                  }
  
                }
  
            }
  
            catch (SqlException e)
  
            {
  
                Console.WriteLine(e.ToString());
  
            }
  
            Console.ReadLine();
  
      }
  
    }
  
}


页: [1]
查看完整版本: Use .NET (C#) with VS to connect Azure SQL DB-Ricky