.:: Jasa Membuat Aplikasi Website,Desktop,Android Order Now..!! | | Order Now..!! Jasa Membuat Project Arduino,Robotic,Print 3D ::.

Connect to SQL Azure Using ADO.NET

0 komentar


بِسْــــــــــــــــمِ اﷲِالرَّحْمَنِ اارَّحِيم
bismillaahirrahmaanirrahiim

السَّلاَمُ عَلَيْكُمْ وَرَحْمَةُ اللهِ وَبَرَكَاتُهُ
Assalamu'alaikum warahmatullahi wabarakatuh


Sample code using ADO.NET to connect to a database in Microsoft SQL Azure Database is very similar to connecting to an instance of SQL Server on your premises

Using this Example
To use this example in Visual Studio with your SQL Azure server, do the following steps:
  1. Open Visual Studio and create a new console application.
  2. Replace the code in the program file with the code from this example.
  3. Replace <ProvideUserName> with the name of an SQL Azure Database login that has been assigned the dbmanager role. Note: if you use the login@server username format, the server portion of the name must match the first part of the server's fully qualified name. For example, if your server is servername.database.windows.net, your login name will look like loginname@servername. For more information about SQL Azure Database roles.
  4. Replace <ProvidePassword> with the password associated with the login. Note: We recommend using a strong password when creating a login.
  5. Replace <ProvideServerName> with the fully qualified domain name of your SQL Azure server. For example: servername.database.windows.net
  6. Replace <ProvideDatabaseName> with the name of the database you want the code to create.
  7. Run the code.
' Sample Code
Option Explicit On
Option Strict On

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Data.SqlClient
Imports System.Data

Module Program
    ' Provide the following information
    Private ReadOnly userName As String = "<ProvideUserName>"
    Private ReadOnly password As String = "<ProvidePassword>"
    Private ReadOnly dataSource As String = "<ProvideServerName>"
    Private ReadOnly sampleDatabaseName As String = "<ProvideDatabaseName>"


    Sub Main()
        ' Create a connection string for the master database
        Dim connString1Builder As New SqlConnectionStringBuilder()
        With connString1Builder
            .DataSource = dataSource
            .InitialCatalog = "master"
            .Encrypt = True
            .TrustServerCertificate = False
            .UserID = userName
            .Password = password
        End With

        ' Create a connection string for the sample database
        Dim connString2Builder As New SqlConnectionStringBuilder()
        With connString2Builder
            .DataSource = dataSource
            .InitialCatalog = sampleDatabaseName
            .Encrypt = True
            .TrustServerCertificate = False
            .UserID = userName
            .Password = password
        End With


        ' Connect to the master database and create the sample database
        Using conn As New SqlConnection(connString1Builder.ToString())
            Using command As SqlCommand = conn.CreateCommand()
                conn.Open()

                ' Create the sample database
                Dim cmdText As String
                cmdText = String.Format("CREATE DATABASE {0}", _
                                        sampleDatabaseName)
                command.CommandText = cmdText
                command.ExecuteNonQuery()
                conn.Close()
            End Using
        End Using


        ' Connect to the sample database and perform various operations
        Using conn As New SqlConnection(connString2Builder.ToString())
            Using command As SqlCommand = conn.CreateCommand()
                conn.Open()

                ' Create a table
                command.CommandText = "CREATE TABLE T1(Col1 int primary key, Col2 varchar(20))"
                command.ExecuteNonQuery()

                ' Insert sample records
                command.CommandText = "INSERT INTO T1 (col1, col2) VALUES (1, 'string 1'), (2, 'string 2'), (3, 'string 3')"
                Dim rowsAdded As Integer = command.ExecuteNonQuery()

                ' Query the table and print the results
                command.CommandText = "SELECT * FROM T1"
                Using reader As SqlDataReader = command.ExecuteReader()
                    ' Loop over the results
                    While reader.Read()
                        Console.WriteLine("Col1: {0}, Col2: {1}", _
                                          reader("Col1").ToString().Trim(), _
                                          reader("Col2").ToString().Trim())
                    End While
                End Using

                ' 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"
                command.ExecuteNonQuery()

                ' Query the table and print the results
                Console.WriteLine("After update/delete, the table has these records...")
                command.CommandText = "SELECT * FROM T1"
                Using reader As SqlDataReader = command.ExecuteReader()
                    ' Loop over the results
                    While reader.Read()
                        Console.WriteLine("Col1: {0}, Col2: {1}", _
                                          reader("Col1").ToString().Trim(), _
                                          reader("Col2").ToString().Trim())
                    End While
                End Using

                conn.Close()
            End Using
        End Using

        Console.WriteLine("Press Enter to continue...")
        Console.ReadLine()
    End Sub
End Module

Update Contact :
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email : Fajarudinsidik@gmail.com
NB :: Bila Sobat tertarik Ingin membuat software, membeli software, membeli source code, membeli hardware elektronika untuk kepentingan Perusahaan maupun Tugas Akhir (TA/SKRIPSI), Insyaallah Saya siap membantu, untuk Respon Cepat dapat menghubungi kami, melalui :

No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email: Fajarudinsidik@gmail.com


atau Kirimkan Private messanger melalui email dengan klik tombol order dibawah ini :

ٱلْحَمْدُ لِلَّهِ رَبِّ ٱلْعَٰلَمِين
Alhamdulilah hirobil alamin

وَ السَّلاَمُ عَلَيْكُمْ وَرَحْمَةُ اللهِ وَبَرَكَاتُهُ
wassalamualaikum warahmatullahi wabarakatuh


Artikel Connect to SQL Azure Using ADO.NET, Diterbitkan oleh scodeaplikasi pada Sabtu, 26 November 2011. Semoga artikel ini dapat menambah wawasan Anda. Website ini dipost dari beberapa sumber, bisa cek disini sumber, Sobat diperbolehkan mengcopy paste / menyebar luaskan artikel ini, karena segala yang dipost di public adalah milik public. Bila Sobat tertarik Ingin membuat software, membeli software, membeli source code ,Dengan Cara menghubungi saya Ke Email: Fajarudinsidik@gmail.com, atau No Hp/WA : (fajar) : 085369237896, (puat) : 085267792168.

Tawk.to