What is SQL server and connection with SQL server?

► Play

The connection with the server is the way to connect with the server with your authentication (username and password) and access the content from that server. We can connect with SQL server different ways and access the content like for web services (PHP, Python, and java etc.) and Desktop Applications. Now SQL Server is available for both operating systems (Windows and Linux).

How to Connect to SQL Server in C#?

► Play

C# means C Sharp programming language. We can connect with SQL Server using C# code. The is given below,

First, we built a connection with the Server with its name

connetionString =

‘Data Source = ServerName ;

Initial Catalog = DatabaseName;

User ID = UserName ;

Password = Password ‘

If your SQL server is running on your own PC then just type this command and built a connection with SQL Server:

                               ‘ Server = localhost\sqlexpress ‘

After that, you need to close the connection with “ cnn.Close ( ); ” function.

A sample Code for connecting with SQL Server in C#.

using System;

using System.Windows.Forms;

using System.Data.SqlClient;

namespace WindowsApplication1

{

    public partial class Form1: Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)

        {

            string connetionString = null;

            SqlConnection cnn ;

connetionString = ‘Data Source=ServerName;Initial Catalog=DatabaseName;

User ID=UserName;Password=Password’

            cnn = new SqlConnection(connetionString);

            try

            {

                cnn.Open();

                MessageBox.Show (‘Connection Open ! ‘);

                cnn.Close();

            }

            catch (Exception ex)

            {

                MessageBox.Show(‘Can not open connection ! ‘);

            }

        }

    }

}

How to Connect to SQL Server using Python?

► Play

Before built connection with SQL Server using python we need to install the library name “ pyodbc ” then import this library with your code:

import pyodbc

conn = pyodbc.connect ( ‘ Driver = { SQL Server } ; ‘ ‘ Server = server_name ; ‘ ‘ Database = db_name ; ‘

                                                 ‘ Trusted_Connection = yes ; ‘ ) ;

cursor = conn.cursor ( ) ;

cursor.execute ( ‘ SELECT * FROM db_name.Table ‘ ) ;

How to Connect to SQL Server using Java?

► Play

Java is the most useful programming language nowadays. Billions of devices are on JDK ( Java Development Kit ). Java provides some SQL libraries to connect with the SQL Server and retrieve data.

Java code for connecting with SQL Server:

import java.sql.Connection ;

import java.sql.DriverManager ;

import java.sql.SQLException ;

public class SQLDatabaseConnection {

    // Connect to your database.

    // Replace server name, username, and password with your credentials

    public static void main ( String [ ] args ) {

        String connectionUrl =

                ‘ jdbc:sqlserver://yourserver.database.windows.net:1433; ‘

                        + ‘ database=AdventureWorks; ‘

                        + ‘ user=yourusername@yourserver; ‘

                        + ‘ password=yourpassword; ‘

                        + ‘ encrypt=true; ‘

                        + ‘ trustServerCertificate=false; ‘

                        + ‘ loginTimeout=30; ‘ ;

        try ( Connection connection = DriverManager.getConnection ( connectionUrl ) ; ) {

            // Code here.

        }

        // Handle any errors that may have occurred.

        catch ( SQLException e ) {

            e.printStackTrace ( ) ;

        }

    }

}

How to Connect to SQL Server using JavaScript?

► Play

A simple example of connecting JavaScript with SQL Server:

var connection = new ActiveXObject (‘ ADODB.Connection ‘ )  ;

var connectionstring = ‘ Data Source=; Initial Catalog = ; User ID = ; Password = ; Provider = SQLOLEDB’ ;

connection.Open(connectionstring);

var rs = new ActiveXObject(‘ADODB.Recordset’);

rs.Open(‘SELECT * FROM table’, connection);

rs.MoveFirst

while(!rs.eof)

{

   document.write(rs.fields(1));

   rs.movenext;

}

rs.close;

connection.close;

Send us a feedback0/500

Do you like this article?
Yes
No