1、连接Access
代码
1 public static OleDbConnection getConn()
2 {
3 OleDbConnection Conn=null;
4 string StrConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\AccessDataBase\GuangXi.mdb";
5 try
6 {
7 if (File.Exists(@"D:\AccessDataBase\GuangXi.mdb"))
8 {
9 Conn = new OleDbConnection(StrConn);
10 Conn.Open();
11 }
12 return Conn;
13 }
14 catch
15 {
16 return null;
17 }
18 }
2 {
3 OleDbConnection Conn=null;
4 string StrConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\AccessDataBase\GuangXi.mdb";
5 try
6 {
7 if (File.Exists(@"D:\AccessDataBase\GuangXi.mdb"))
8 {
9 Conn = new OleDbConnection(StrConn);
10 Conn.Open();
11 }
12 return Conn;
13 }
14 catch
15 {
16 return null;
17 }
18 }
2、连接Oracle
代码
1 public static OracleConnection getConn()
2 {
3 string strConn = "Data Source=hvmdata;Persist Security Info=True;User ID=sysadmin;Password=adminsys;Unicode=True";
4 try
5 {
6 OracleConnection Conn = new OracleConnection(strConn);
7 if (Conn.State != ConnectionState.Open)
8 {
9 Conn.Open();
10 }
11 return Conn;
12 }
13 catch
14 {
15 return null;
16 }
17 }
2 {
3 string strConn = "Data Source=hvmdata;Persist Security Info=True;User ID=sysadmin;Password=adminsys;Unicode=True";
4 try
5 {
6 OracleConnection Conn = new OracleConnection(strConn);
7 if (Conn.State != ConnectionState.Open)
8 {
9 Conn.Open();
10 }
11 return Conn;
12 }
13 catch
14 {
15 return null;
16 }
17 }
3、连接Sql Server2005
代码
1 public static SqlConnection getConn()
2 {
3 string strConn = "Data Source=JFD\\SQLEXPRESS;database=mydayta;uid=sa;pwd=123456;";
4 try
5 {
6 SqlConnection Conn = new SqlConnection(strConn);
7 if (Conn.State != ConnectionState.Open)
8 {
9 Conn.Open();
10 }
11 return Conn;
12 }
13 catch
14 {
15 return null;
16 }
17 }
2 {
3 string strConn = "Data Source=JFD\\SQLEXPRESS;database=mydayta;uid=sa;pwd=123456;";
4 try
5 {
6 SqlConnection Conn = new SqlConnection(strConn);
7 if (Conn.State != ConnectionState.Open)
8 {
9 Conn.Open();
10 }
11 return Conn;
12 }
13 catch
14 {
15 return null;
16 }
17 }