C# İle Veritabanlarına Bağlantı Yapmak

C# İle Veritabanlarına Bağlantı Yapmak

Ağustos 23, 2023

Okuma süresi: 12 dakika

Sizler için C# ile bir çok veri tabanına bağlantı yapabilmeniz için bağlantı cümlelerini bir araya getirdim. Makale içinde bulunan veritabanları şunlardır:

  •  MS SQL Server,
  • MS SQL Express,
  • MS Access, Oracle,
  • IBM DB2,
  • MySql,
  • Sybase,
  • Interbase,
  • Informix,
  • Excel,
  • Text,
  • dBase Dbf,
  • Visual FoxPro .

 

Umarım faydasını görürsünüz.

 

  1. Microsoft SQL Server​
  • ODBC DSN
?
1
2
3
4
5
6
7
using System.Data.Odbc;
var conn = new OdbcConnection();
conn.ConnectionString =
              "Dsn=DsnName;" +
              "Uid=UserName;" +
              "Pwd=Secret;";
conn.Open();
  • ODBC -- Standard Connection
?
1
2
3
4
5
6
7
8
9
using System.Data.Odbc;
var conn = new OdbcConnection();
conn.ConnectionString =
              "Driver={SQL Server};" +
              "Server=DataBaseNamex;" +
              "DataBase=DataBaseName;" +
              "Uid=UserName;" +
              "Pwd=Secret;";
conn.Open();
  • ODBC -- Trusted Connection
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System.Data.Odbc;
var conn = new OdbcConnection();
conn.ConnectionString =
              "Driver={SQL Server};" +
              "Server=ServerName;" +
              "DataBase=DataBaseName;" +
              "Uid=;" +
              "Pwd=;";
conn.Open();
// or
var conn = new OdbcConnection();
conn.ConnectionString =
              "Driver={SQL Server};" +
              "Server=ServerName;" +
              "DataBase=DataBaseName;" +
              "Trusted_Connection=Yes;";
conn.Open();
  • OleDb -- Standard Connection
?
1
2
3
4
5
6
7
8
9
using System.Data.OleDb;
var conn = new OleDbConnection();
conn.ConnectionString =
              "Driver=SQLOLEDB;" +
              "Data Source=ServerName;" +
              "Initial Catalog=DataBaseName;" +
              "User id=UserName;" +
              "Password=Secret;";
conn.Open();
  • OleDb -- Trusted Connection
?
1
2
3
4
5
6
7
8
using System.Data.OleDb;
var conn = new OleDbConnection();
conn.ConnectionString =
              "Driver=SQLOLEDB;" +
              "Data Source=ServerName;" +
              "Initial Catalog=DataBaseName;" +
              "Integrated Security=SSPI;";
conn.Open();
  • OleDb -- via IP Address
?
1
2
3
4
5
6
7
8
9
10
using System.Data.OleDb;
var conn = new OleDbConnection();
conn.ConnectionString =
              "Driver=SQLOLEDB;" +
              "Network Library=DBMSSOCN;" +
              "Data Source=xxx.xxx.xxx.xxx,1433;" +
              "Initial Catalog=DataBaseName;" +
              "User id=UserName;" +
              "Password=Secret;";
conn.Open();
  • .NET DataProvider -- Standard Connection
?
1
2
3
4
5
6
7
8
using System.Data.SqlClient;
var conn = new SqlDbConnection();
conn.ConnectionString =
              "Data Source=ServerName;" +
              "Initial Catalog=DataBaseName;" +
              "User id=UserName;" +
              "Password=Secret;";
conn.Open();
  • .NET DataProvider -- Trusted Connection
?
1
2
3
4
5
6
7
using System.Data.SqlClient;
var conn = new SqlConnection();
conn.ConnectionString =
              "Data Source=ServerName;" +
              "Initial Catalog=DataBaseName;" +
              "Integrated Security=SSPI;";
conn.Open();
  • .NET DataProvider -- via IP Address
?
1
2
3
4
5
6
7
8
9
using System.Data.SqlClient;
var conn = new SqlConnection();
conn.ConnectionString =
              "Network Library=DBMSSOCN;" +
              "Data Source=xxx.xxx.xxx.xxx,1433;" +
              "Initial Catalog=DataBaseName;" +
              "User Id=UserName;" +
              "Password=Secret;";
conn.Open();
  1. Microsoft Sql Express

  • .NET Data Provider -- Default Relative Path -- Standard Connection
?
1
2
3
4
5
6
7
8
9
using System.Data.SqlClient;
var conn = new SqlConnection();
conn.ConnectionString =
     "Data Source=.\SQLExpress;" +
     "User Instance=true;" +
     "User Id=UserName;" +
     "Password=Secret;" +
     "AttachDbFilename=|DataDirectory|DataBaseName.mdf;"
conn.Open();
  • .NET Data Provider -- Default Relative Path -- Trusted Connection
?
1
2
3
4
5
6
7
8
using System.Data.SqlClient;
var conn = new SqlConnection();
conn.ConnectionString =
     "Data Source=.\SQLExpress;" +
     "User Instance=true;" +
     "Integrated Security=true;" +
     "AttachDbFilename=|DataDirectory|DataBaseName.mdf;"
conn.Open();
  • .NET Data Provider -- Custom Relative Path -- Standard Connection
?
1
2
3
4
5
6
7
8
9
10
11
using System.Data.SqlClient;
AppDomain.CurrentDomain.SetData(
     "DataDirectory", "C:\MyPath\");
var conn = new SqlConnection();
conn.ConnectionString =
     "Data Source=.\SQLExpress;" +
     "User Instance=true;" +
     "User Id=UserName;" +
     "Password=Secret;" +
     "AttachDbFilename=|DataDirectory|DataBaseName.mdf;"
conn.Open();
  • .NET Data Provider -- Custom Relative Path -- Trusted Connection
?
1
2
3
4
5
6
7
8
9
10
using System.Data.SqlClient;
AppDomain.CurrentDomain.SetData(
     "DataDirectory", "C:\MyPath\");
var conn = new SqlConnection();
conn.ConnectionString =
     "Data Source=.\SQLExpress;" +
     "User Instance=true;" +
     "Integrated Security=true;" +
     "AttachDbFilename=|DataDirectory|DataBaseName.mdf;"
conn.Open();
  • .NET Data Provider -- Absolute Path -- Standard Connection
?
1
2
3
4
5
6
7
8
9
using System.Data.SqlClient;
var conn = new SqlConnection();
conn.ConnectionString =
     "Data Source=.\SQLExpress;" +
     "User Instance=true;" +
     "User Id=UserName;" +
     "Password=Secret;" +
     "AttachDbFilename=C:\MyPath\DataBaseName.mdf;"
conn.Open();
  • .NET Data Provider -- Absolute Path -- Trusted Connection
?
1
2
3
4
5
6
7
8
using System.Data.SqlClient;
var conn = new SqlConnection();
conn.ConnectionString =
     "Data Source=.\SQLExpress;" +
     "User Instance=true;" +
     "Integrated Security=true;" +
     "AttachDbFilename=C:\MyPath\DataBaseName.mdf;"
conn.Open();

 

  1. Microsoft Access

  • ODBC DSN
?
1
2
3
4
using System.Data.Odbc;
var conn = new OdbcConnection();
conn.ConnectionString = "Dsn=DsnName";
conn.Open();
  • ODBC -- Standard Security
?
1
2
3
4
5
6
7
using System.Data.Odbc;
var conn = new OdbcConnection();
conn.ConnectionString =
    "Driver={Microsoft Access Driver (*.mdb)};" +
    "Dbq=c:\myPath\myDb.mdb;" +
    "Uid=Admin;Pwd=;";
conn.Open();
  • ODBC -- Workgroup (System Database)
?
1
2
3
4
5
6
7
using System.Data.Odbc;
var conn = new OdbcConnection();
conn.ConnectionString =
    "Driver={Microsoft Access Driver (*.mdb)};" +
    "Dbq=c:\myPath\myDb.mdb;" +
    "SystemDb=c:\myPath\myDb.mdw;";
conn.Open();
  • ODBC -- Exclusive Use
?
1
2
3
4
5
6
7
8
using System.Data.Odbc;
var conn = new OdbcConnection();
conn.ConnectionString =
     "Driver={Microsoft Access Driver (*.mdb)};" +
     "Dbq=c:\myPath\myDb.mdb;" +
     "Exclusive=1;";
     "Uid=Admin;Pwd=;";
conn.Open();
  • OleDb with MS Jet -- Standard Security
?
1
2
3
4
5
6
7
8
using System.Data.OleDb;
var conn = new OleDbConnection();
conn.ConnectionString =
           "Provider=Microsoft.Jet.OLEDB.4.0;" +
           "Data Source=c:\mypath\myDb.mdb;" +
           "User id=admin;" +
           "Password=";
conn.Open();
  • OleDb with MS Jet -- Workgroup (System Database)
?
1
2
3
4
5
6
7
using System.Data.OleDb;
var conn = new OleDbConnection();
conn.ConnectionString =
           "Provider=Microsoft.Jet.OLEDB.4.0;" +
           "Data Source=c:\mypath\myDb.mdb;" +
           "System Database=c:\mypath\myDb.mdw;";
conn.Open();
  • OleDb with MS Jet -- With Password
?
1
2
3
4
5
6
7
using System.Data.OleDb;
var conn = new OleDbConnection();
conn.ConnectionString =
           "Provider=Microsoft.Jet.OLEDB.4.0;" +
           "Data Source=c:\mypath\myDb.mdb;" +
           "Database Password=Secret;"
conn.Open();

 

  1. Oracle

 

  • ODBC DSN
?
1
2
3
4
5
6
7
using System.Data.Odbc;
var conn = new OdbcConnection();
conn.ConnectionString =
              "Dsn=DsnName;" +
              "Uid=UserName;" +
              "Pwd=Secret;";
conn.Open();
  • ODBC -- New Microsoft Driver
?
1
2
3
4
5
6
7
8
using System.Data.Odbc;
var conn = new OdbcConnection();
conn.ConnectionString =
           "Driver={Microsoft ODBC for Oracle};" +
           "Server=OracleServer.world;" +
           "Uid=UserName;" +
           "Pwd=Secret;";
conn.Open();
  • ODBC -- Old Microsoft Driver
?
1
2
3
4
5
6
7
8
using System.Data.Odbc;
var conn =