Connecting to Microsoft SQL azure database using ADO. Net has become very easy now.ArticleProvides an example ConsoleProgramTo describe how to connect to the azure database, there are also some notes I think about connecting to the Microsoft SQL azure database. SQL azure entry level issues. Ha, azure talents can bypass.
Console program example:
1. Use vs to create a console Program
2.CodeMedium<Provideusername> replace it with the SQL azure database login name. The format is as follows:Login @ server. For more information about accounts, seeManaging databases and logins in SQL azureArticle.
3. Replace<Providepassword> password for your account.
4. Replace<Provideservername> name of your SQL azure server, as shown in figure
Servername.database.windows.net, which is the same as the post part of the '@' symbol in the Login Format.
5.<Providedatabasename> is the name of the database you want to create with your code (which does not exist ).
The Code is as follows:
View code
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;
Using System. Data. sqlclient;
Using System. Data;
Namespace Microsoft. SDS. Samples
{
Class Program
{
// Provide the following information
Private Static String Username = " <Provideusername> " ;
Private Static String Password = " <Providepassword> " ;
Private Static String Datasource = " <Provideservername> " ;
Private Static String Sampledatabasename = " <Providedatabasename> " ;
Static Void Main ( String [] ARGs)
{
// Create a connection string for the master database
Sqlconnectionstringbuilder connstring1builder;
Connstring1builder = New Sqlconnectionstringbuilder ();
Connstring1builder. datasource = Datasource;
Connstring1builder. initialcatalog = " Master " ;
Connstring1builder. Encrypt = True ;
Connstring1builder. trustservercertificate = False ;
Connstring1builder. userid = Username;
Connstring1builder. Password = Password;
// Create a connection string for the sample database
Sqlconnectionstringbuilder connstring2builder;
Connstring2builder = New Sqlconnectionstringbuilder ();
Connstring2builder. datasource = Datasource;
Connstring2builder. initialcatalog = Sampledatabasename;
Connstring2builder. Encrypt = True ;
Connstring2builder. trustservercertificate = False ;
Connstring2builder. userid = Username;
Connstring2builder. Password = Password;
//Connect to the master database and create the sample database
Using(Sqlconnection Conn= NewSqlconnection (connstring1builder. tostring ()))
{
Using(Sqlcommand command=Conn. createcommand ())
{
Conn. open ();
// Create the sample database
String Plain text = String. Format ( " Create Database {0} " ,
Sampledatabasename );
Command. commandtext = Plain text;
Command. executenonquery ();
Conn. Close ();
}
}
// connect to the sample database and perform various operations
using (sqlconnection conn = New sqlconnection (connstring2builder. tostring ()
{< br> using (sqlcommand command = Conn. createcommand ()
{< br> Conn. open ();
// Create a table
command. commandtext = " Create Table T1 (col1 int primary key, col2 varchar (20) " ;< br> command. executenonquery ();
// insert sample records
command. commandtext = " insert into T1 (col1, col2) values (1, 'string 1'), (2, 'string 2'), (3, 'string 3 ') " ;
int rowsadded = command. executenonquery ();
//Query the table and print the results
Command. commandtext= "Select * from T1";
Using (Sqldatareader Reader = Command. executereader ())
{
// Loop over the results
While (Reader. Read ())
{
Console. writeline ( " Col1: {0}, col2: {1} " ,
Reader [ " Col1 " ]. Tostring (). Trim (),
Reader [ " Col2 " ]. Tostring (). Trim ());
}
}
//Update a record
Command. commandtext= "Update T1 set col2 = 'string 1111 'Where col1 = 1";
Command. executenonquery ();
// delete a record
command. commandtext = " Delete from T1 where col1 = 2 " ;< br> command. executenonquery ();
//Query the table and print the results
Console. writeline ("\ Nafter update/Delete the table has these records...");
Command. commandtext= "Select * from T1";
Using (Sqldatareader Reader = Command. executereader ())
{
// Loop over the results
While (Reader. Read ())
{
Console. writeline ( " Col1: {0}, col2: {1} " ,
Reader [ " Col1 " ]. Tostring (). Trim (),
Reader [ " Col2 " ]. Tostring (). Trim ());
}
}
Conn. Close ();
}
}
Console. writeline ("Press enter to continue...");
Console. Readline ();
}
}
}
Now let's take a look at what this code has done.
1. First, use the codeSqlconnectionstringbuilder object to connect to the master database of SQL azure database, and thenCreate a database in the name of the sampledatabasename string
2. Use anotherSqlconnectionstringbuilder object to connect to the database created in step 1
3. Once we connect to the database just created in SQL azure database, we use the secondSqlconnectionstringbuilder is used to create database tables and some sample data operations.
4. Finally, the Code returns data to the console before and after data modification.
For more information about connecting to SQL Azure, seeConnecting to a data source (ADO. Net)
Note:
Through the above examples, we have some basic knowledge about connecting to SQL azure. Now let's discuss some of the small issues that need attention between them.
To avoid injection attacks, we use the sqlconnectionstringbuilder class, which is also a class provided by the. NET Framework framework.
We need to well protect our connection string information. If we let others know, they can use our data and tamper with it, and other devastating losses.
Since we do not know how many route nodes are going through during the communication with SQL Azure, we 'd better set up ADO to protect our connection strings from being stolen by others. net encrypt and trustservercertificate connection parameters, as shown in the code, encrypt = true, trustservercertificate = false can ensure that our connection string is encrypted, it is useless for anyone who intercepts communication to obtain your connection string.