新手帮助 | 版主申请 | BST制度 | MST制度
返回列表 回复 发帖

SQL Server 、Access 数据库连接[分享]

SQL Server
/ W) R# ?2 z9 V7 \% _6 f" \/ ^  AODBC
' G7 p7 B2 ]: ~. l: O" n- w# _( @) U: f5 M& A
Standard Security:4 e& _$ m7 d- d% x: S% Q7 J$ P
"Driver={SQL Server};Server=Aron1;Database=pubs;Uid=sawd=asdasd;"
% }4 _* k3 }3 `! u1 l
. o2 r: R4 N/ `! r' k+ y0 B
, m9 o: G: ^0 f, ]0 z( M! |Trusted connection:
. f9 k" W0 ~5 F; M! U"Driver={SQL Server};Server=Aron1;Database=pubs;Trusted_Connection=yes;" # y- h$ n( K; h2 N: M3 ], W

1 }7 |* S0 y0 U9 t( H6 W& i3 ?; T' q8 W5 j8 W+ h# R1 `; u/ f
Prompt for username and password:
. H; ^$ k8 Z9 IoConn.Properties("rompt") = adPromptAlways
7 i9 m6 S2 K; j3 boConn.Open "Driver={SQL Server};Server=Aron1;DataBase=pubs;" # A* f4 N: g0 x* i8 [7 z
OLE DB, OleDbConnection (.NET)
6 n, R3 ], M0 S% V; ~$ o9 a3 e0 p+ n1 F, z
Standard Security:) H. ]+ n) I1 `5 W( H: L
"rovider=sqloledb;Data Source=Aron1;Initial Catalog=pubs;User Id=sa;Password=asdasd;" ; y  ?: K$ |& T' L* r1 Z

) [; y: d$ y( F- H+ `3 g- q3 y1 L& R
Trusted Connection:
. y8 T+ }, U1 r! ~"Provider=sqloledb;Data Source=Aron1;Initial Catalog=pubs;Integrated Security=SSPI;"
1 H, z  y+ ], b/ P6 B- s3 q2 o9 Y0 Z(use serverName\instanceName as Data Source to use an specifik SQLServer instance, only SQLServer2000)
, f6 T" Z; h# O* x1 u3 ePrompt for username and password:
0 z; R) f, X/ g, voConn.Provider = "sqloledb"
2 F4 R$ D) \& h# l9 v) moConn.Properties("Prompt") = adPromptAlways2 N/ c) e; Y0 o+ m, q
oConn.Open "Data Source=Aron1;Initial Catalog=pubs;"
/ Z9 u- L6 o- Q: s
% h2 ~3 u3 |5 q
! \! o4 x* ~6 W4 R1 z4 sConnect via an IP address:" ?( w; ~, a$ T) U* d: I
"Provider=sqloledb;Data Source=190.190.200.100,1433;Network Library=DBMSSOCN;Initial Catalog=pubs;User ID=sa;Password=asdasd;" , R3 z: S  l+ x1 u- A! _. G3 y+ J
(DBMSSOCN=TCP/IP instead of Named Pipes, at the end of the Data Source is the port to use (1433 is the default))
; G, q) R6 z" F, v, b3 g! ^<!-- HERE! -->
9 N, U5 f9 f+ G: XSqlConnection (.NET)
' S' S. q: n% K7 }7 k: e0 F) i; ~* `2 F2 Q4 l
Standard Security:
; d: f7 q) P% n4 l"Data Source=Aron1;Initial Catalog=pubs;User Id=sa;Password=asdasd;"
' D0 k2 [: Y6 \. w. d8 r* t- o! g* g- or -
2 f. B. U7 q* e6 O+ k$ t/ n4 D"Server=Aron1;Database=pubs;User ID=sa;Password=asdasd;Trusted_Connection=False"
2 N7 X! w6 Q: C  x5 B$ H$ A(both connection strings produces the same result)
- m  \- z6 E% a4 j7 E0 p6 k* ]% u5 X* ^

1 E) h) y4 y7 A. R% z. n5 uTrusted Connection:* P) p6 V  ^6 L8 O
"Data Source=Aron1;Initial Catalog=pubs;Integrated Security=SSPI;"
3 h( r5 O- t+ ]' }- or -# S, E- M1 n- `6 k9 T
"Server=Aron1;Database=pubs;Trusted_Connection=True;"
/ Y/ G9 e  V8 T; q- g9 C! |(both connection strings produces the same result)& |' v& \5 v& l+ v8 O; G  Z
(use serverName\instanceName as Data Source to use an specifik SQLServer instance, only SQLServer2000)0 o) {- b  |( [
Connect via an IP address:
  u2 a5 W% s$ y3 k"Data Source=190.190.200.100,1433;Network Library=DBMSSOCN;Initial Catalog=pubs;User ID=sa;Password=asdasd;" . k$ ], g. K$ G9 {5 v# q
(DBMSSOCN=TCP/IP instead of Named Pipes, at the end of the Data Source is the port to use (1433 is the default))* g; G1 o9 \" a& s# f) K
Declare the SqlConnection:5 z7 Q, w! i6 K7 U+ N9 j! C5 s
C&#35;:
; o3 P) U* R2 m: x% N" Q# Musing System.Data.SqlClient;
1 C/ e4 Z. j' f8 USqlConnection oSQLConn = new SqlConnection();
; r. x! ~/ j# ]$ AoSQLConn.ConnectionString="my connectionstring";8 E) a' b8 J$ e% ~1 h
oSQLConn.Open();
; V9 T1 Q5 A. f/ F% [2 N$ a5 C! ?VB.NET:; K" D( a* T0 H2 c) U
Imports System.Data.SqlClient
; H7 i- O# Y! J! S4 mDim oSQLConn As SqlConnection = New SqlConnection()1 @, T4 S- n& j, i' m. r" @3 s
oSQLConn.ConnectionString="my connectionstring"/ w  x* M4 i8 n7 S" v
oSQLConn.Open() ( F/ A' h. z2 m7 _
Data Shape 9 ~0 v" o5 y2 J" \$ b3 U

3 X+ S6 X2 K' f. U# C  t" c2 C" yMS Data Shape
; t7 O- i; q$ r* s"Provider=MSDataShape;Data Provider=SQLOLEDB;Data Source=Aron1;Initial Catalog=pubs;User ID=sa;Password=asdasd;" , N' ~: ?1 y# }( I, s3 s

: \5 S* x8 e" U5 e/ ?) G& vAccess
) ?# X8 K! L- N6 ^ODBC
5 B+ C2 j( j2 o
, ^- S$ k+ d& L. Q9 A# C+ VStandard Security:$ C5 M6 y. A( U( v$ X, {& H3 }# p
"Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\mydatabase.mdb;Uid=Admin;Pwd=;" : S  m" s/ E0 P& t! h, T
; ]% @1 D0 I8 d/ v+ C

9 b- |* |; L# e3 _4 eWorkgroup:. U0 u" ]( h0 Q
"Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\mydatabase.mdb;SystemDB=C:\mydatabase.mdw;" # _0 F0 m; I  [7 F( b/ \
  a2 Q5 L/ @1 n( h" E( J% v
/ N, i% C: \9 L/ A' v+ B6 d
Exclusive:
7 j, k3 o- p2 ]0 C" _) D"Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\mydatabase.mdb;Exclusive=1;Uid=admin;Pwd=" 6 j8 k9 x" u& t- M3 f1 p% b
OLE DB, OleDbConnection (.NET)
' |, D4 x4 g' ?
8 e- T& j. [9 u8 N6 H6 eStandard security:
* w( @8 K$ T0 G6 f3 P# x4 g+ `"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\somepath\mydb.mdb;User Id=admin;Password=;"
- m( q9 Z0 L" a" |8 Y; r/ E% p4 ?: K3 ?1 z' P/ P3 H

1 j! e+ O/ ^" k0 F& MWorkgroup (system database):
) p/ o% j( L- d"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\somepath\mydb.mdb;Jet OLEDB:System Database=system.mdw;" 9 [. R+ X  _/ G2 p$ j( b
0 U, x* v1 A# N; y0 u! C

' z8 S; H, A* y5 M4 r9 IWith password:" B% x# @" Y" R: N2 V4 F
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\somepath\mydb.mdb;Jet OLEDBatabase Password=MyDbPassword;"
* i9 i5 ~: I+ W! R
返回列表