Saturday, 6 July 2013

Database connectivity with Oracle,SQL Server and MS Access using Visual Studio




When your project connected with database in visual studio, the properties of Data Connection holds working connection string in Server Explorer in Visual Studio. So to find connection string you only need to connect database through visual studio. Here I will explain database connectivity with three main databases as SQL Server, MS Access database and Oracle Database.

Connect SQL Server with Visual Studio/SQL server Database Connectivity

   1.   Open Visual Studio.
   2.  Go to View menu=> Server Explorer.
   3.   Right click on Data Connections and select Add Connection.
   4.  You will get add connection window.
   5.  Provide Server name. Select Use SQL Server Authentication/Use Window Authentication radio    button. Then submit the username and password.
  6. After validating provided credentials, the database lists is shown in dropdown list .Select relevant database. OR You can attach a .mdf database file also.
   7.  You can test connection and if succeeded Ok.



  8.       A new data connection created in your visual studio Server Explorer.
  9.       Right Click on data connection and go to properties. You can see Connection string here.
















10.    Copy connection string and paste it in your web config.










11.    You can use this connection string in your project to open and use connection
public class DbData
{

static SqlConnection cn; //defining in class
    static string cnn; //defining in class
public DbData()
{
}
public static SqlConnection GetConnection()
    {
        cnn = System.Web.Configuration.WebConfigurationManager.AppSettings["con"].ToString();
        cn = new SqlConnection(cnn);
        if (cn.State == ConnectionState.Closed)
        {
            cn.Open();
        }
        else
        {
            cn.Close();
        }
        return cn;

    }
}
Use this method as
SqlCommand cmd = new SqlCommand(query, DbData.GetConnection());

  12.    That is whole thing about getting and using connection string from Visual Studio.




Connection String for MS Access database/MS Access database connectivity

1.       Open Visual Studio.
2.       Go to View menu=> Server Explorer.
3.       Right click on Data Connections and select Add Connection.
4.       You will get add connection window.
5.       By default Microsoft SQL Server (sqlclient) selected , click change and select Microsoft Access database.
mssql connection string
















































6.       Browse Access database file and test connection if connection succeeded then OK.





















                                                                                          


7.       A new data connection created in your visual studio Server Explorer.
8.       Right Click on data connection and go to properties. You can see Connection string here. Copy and use as above steps for SQL Server.



 Connection String for Oracle database/Oracle Database Connectivity

1.       First install oracle client on machine where you want to configure linked server.
2.       Go to [Oracle Client Installation Directory]\product\11.2.0\]dbhome_1\NETWORK\ADMIN
3.       Open tnsnames.ora file.(It is Network configuration file for oracle)
4.       Add TNS setting in this file as Example here
TEST=
  (DESCRIPTION=
    (ADDRESS=
      (PROTOCOL=TCP)
      (HOST=192.168.0.35)
      (PORT=1556)
    )
    (CONNECT_DATA=
      (SERVICE_NAME=TEST)
    )
  )

5.       See more details of tnsnames.ora file.
To Test oracle connection:
6.       Open cmd
7.       Enter SQLPLUS
8.       Enter USERNAME:  username@Test
9.       Enter PASSWORD: password
10.   Type any select query to confirm connection.
Now you can create connection string
11.    Open Visual Studio.
12.    Go to View menu=> Server Explorer.
13.    Right click on Data Connections and select Add Connection.
14.    You will get add connection window.

15.    By default Microsoft SQL Server (sqlclient) selected , click change and select Oracle Database (OracleClient).In TNS Server name is TNS entry name.and Provied user name and password

mssql connection string


16.    A new data connection created in your visual studio Server Explorer.
17.    Right Click on data connection and go to properties. You can see Connection string here. Copy and use as above steps for SQL Server.

 Hope you enjoy this post

Tuesday, 28 May 2013

Find table name from Column Name

One of my friend ask to provide data as invoice_no,supplier_code etc. of Supplier.Now my database have more than 300 tables and it is difficult to find which table have these column names manually.So I  find it  by querying on joining of  sysobjects and sys.columns

Example:



select so.name

from sysobjects so inner join syscolumns sc
ON so.id = sc.id where sc.name like '%supplier%'

Output like:

Find table name from Column Name

















above is list of Table that contain a column like Supplier.






Wednesday, 8 May 2013

How to track failed login attempts in SQL Server


SQL Server Audit allows you to track and monitor Database/Server level events occurs on database engine.For LOGIN events there is a SQL Server audit action group as “FAILED_LOGIN_GROUP”.You can see list of specifications here.

For auditing login attempts you have first create Server Audit.


USE MASTER

GO

CREATE SERVER AUDIT audit_test                                           

TO FILE (FILEPATH = 'C:\Audit')

GO

Now create audit specification against FAILED_LOGIN_GROUP.

CREATE SERVER AUDIT SPECIFICATION audit_test_spec       

FOR SERVER AUDIT [audit_test]

ADD (FAILED_LOGIN_GROUP)

WITH (STATE=ON)

GO

You can see these audits in Object Explorer under Security=>Audits.


 failed login attempts in SQL Server



Now suppose you log on to same instance on that above audit is applied with wrong credentials.

 failed login attempts in SQL Server


Now connect with right credentials. And go to
Object Explorer=> Security=>Audits=> audit_test
Right click on audit_test  and click View Audit Logs



 failed login attempts in SQL Server






In logs you can see various details of Client who have tried to login Database engine.Also a log file has been created to defined destination.