SQL AZure 学习之三
这一部分使用ADO.Net的方式访问刚刚创建的TestDB数据库的T_Users表,例子是从MSDN上截取下来的。
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace HelloSQLAZure
{
class Program
{
// Provide the following information
private static string userName = "testUser";
private static string password = "myPassword";
private static string dataSource = "abc.database.windows.net ";
private static string sampleDatabaseName = "TestDB";
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
// Create a connection string for the sample database
SqlConnectionStringBuilder connString;
connString = new SqlConnectionStringBuilder();
connString.DataSource = dataSource;
connString.InitialCatalog = sampleDatabaseName;
connString.Encrypt = true;
connString.TrustServerCertificate = false;
connString.UserID = userName;
connString.Password = password;
// Connect to the sample database and perform various operations
using (SqlConnection conn = new SqlConnection(connString.ToString()))
{
using (SqlCommand command = conn.CreateCommand())
{
conn.Open();
// Query the table and print the results
command.CommandText = "SELECT * FROM T_Users";
using (SqlDataReader reader = command.ExecuteReader())
{
// Loop over the results
while (reader.Read())
{
Console.WriteLine("Name: {0}, Age: {1}",
reader["Name"].ToString().Trim(),
reader["Age"].ToString().Trim());
}
}
conn.Close();
}
}
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}
}
很晚了,睡觉去了。
努力不一定成功,但放弃一定失败!