Используем PostgreSQL вместо MS SQL в проектах на .NET и ASP.NET
Эта низкоуровневая обвязка используется вот этой BLL.
1: 'Server=127.0.0.1;Port=5432;Database=XXXXXXX;User Id=postgres;Password=YYYYYYY;ConnectionLifetime=0;
2:
3: Public Class Postgres
4:
5: Dim CN1 As Npgsql.NpgsqlConnection
6: Dim CMD1 As Npgsql.NpgsqlCommand
7: Dim RDR1 As Npgsql.NpgsqlDataReader
8:
9: Function Open(ByVal ConnectionString As String) As Npgsql.NpgsqlConnection
10: Try
11: If CN1 Is Nothing Then
12: CN1 = New Npgsql.NpgsqlConnection(ConnectionString)
13: CN1.Open()
14: Else
15: If CN1.FullState = ConnectionState.Open Then
16: 'отлично, работаем дальше
17: ElseIf CN1.FullState = ConnectionState.Closed Then
18: CN1.Open()
19: ElseIf CN1.FullState = ConnectionState.Broken Then
20: Throw New Exception("ConnectionState=Broken")
21: ElseIf CN1.FullState = ConnectionState.Connecting Then
22: Throw New Exception("ConnectionState=Connecting")
23: ElseIf CN1.FullState = ConnectionState.Executing Then
24: Throw New Exception("ConnectionState=Executing")
25: ElseIf CN1.FullState = ConnectionState.Fetching Then
26: Throw New Exception("ConnectionState=Fetching")
27: End If
28: End If
29: Return CN1
30: Catch ex As Exception
31: MsgBox(ex.Message)
32: End Try
33: End Function
34:
35: Function ExecScalar(ByVal CMD As String) As Integer
36: If CMD1 Is Nothing Then
37: CMD1 = New Npgsql.NpgsqlCommand(CMD, CN1)
38: Else
39: 'команда уже есть - есть ли в ней открытый ридер
40: If RDR1 IsNot Nothing Then
41: RDR1.Close()
42: End If
43: 'теперь новая команда
44: CMD1.CommandText = CMD
45: End If
46: RDR1 = CMD1.ExecuteReader
47: If RDR1.Read Then
48: Return RDR1(0)
49: End If
50: End Function
51:
52: Function ExecRDR(ByVal CMD As String) As Npgsql.NpgsqlDataReader
53: If CMD1 Is Nothing Then
54: CMD1 = New Npgsql.NpgsqlCommand(CMD, CN1)
55: Else
56: 'команда уже есть - есть ли в ней открытый ридер
57: If RDR1 IsNot Nothing Then
58: RDR1.Close()
59: End If
60: 'теперь новая команда
61: CMD1.CommandText = CMD
62: End If
63: RDR1 = CMD1.ExecuteReader
64: Return RDR1
65: End Function
66:
67: Sub Close()
68: If CN1 IsNot Nothing Then
69: CN1.Close()
70: End If
71: End Sub
72: End Class
Comments (
)
Link to this page:
//www.vb-net.com/PostgreSQL/LowLevel.htm
|