Using System; Using System. Collections. Generic; Using System. Data; Using System. Data. Common; Using System. Linq; Using System. Text; Using System. Data. OleDb; Using System. Data. SqlClient; Using System. Data. SQL; Namespace ado { Public class ConnectionDemo1 { Public void GetOleDbTable () { # Region namespace // Using System. Data. OleDb; # Endregion # Region OLEDB Access // OLEDB // C # Access method // Access 2003 Provider = Microsoft. Jet. OLEDB.4.0 // Access 2007 Provider = Microsoft. Ace. OleDb.12.0 // (1) No Database Password, no user password, open in sharing mode // "Provider = Microsoft. Jet. OLEDB.4.0; Data Source = D: qjx document GenericDemoDataStudent. accdb ;" // (2) there is a database password and no user password. This mode is enabled exclusively. // "Provider = Microsoft. Jet. OleDB.4.0; Data Source = D: qjx document GenericDemoDataStudent. accdb; Jet OleDb: DataBase Password = 123" // (3) You can use this method if you have a database password. However, even if you have a password, it must be empty. This mode is enabled exclusively. // "Provider = Microsoft. jet. oleDB.4.0; Data Source = D: qjx document GenericDemoDataStudent. accdb; Jet OleDb: DataBase Password = 12345; Persist Security Info = true; password =; user id = Admin" # Endregion # Region OLEDB MSSQL // OLEDB // C # how to connect to MSSQL // Windows Authentication // "Provider = SQLOLEDB; Data Source = PEK7-6TKX23XSQLEXPRESS; Initial Catalog = Test; Trusted_Connection = Yes ;"; // SQL Server Authentication // "Provider = SQLOLEDB; Data Source = 127.0.0.1SQLEXPRESS; Initial Catalog = Test; User Id = sa; Password = 123456 ;"; # Endregion # Region OLEDB Oracle // OLEDB // C # connection to Oracle // "Provider = MSDAORA; Data Source = MyDatabaseAlias; User ID = MyUID; Password = MyPassword "; # Endregion // String strConn = @ "Provider = Microsoft. Ace. OleDb.12.0; Data Source = D: qjx document GenericDemoDataStudent. accdb; Jet OleDb: DataBase Password = 123"; // Access 2007 String strConn = @ "Provider = SQLOLEDB; Data Source = 127.0.0.1SQLEXPRESS; Initial Catalog = Test; User Id = sa; Password = 123456 ;"; OleDbConnection conn = new OleDbConnection (strConn ); OleDbCommand comm = new OleDbCommand (); Comm. CommandText = "SELECT * FROM Users "; Comm. CommandType = CommandType. Text; Comm. Connection = conn; If (conn. State! = ConnectionState. Open) { Conn. Open (); DataSet ds = new DataSet (); OleDbDataAdapter da = new OleDbDataAdapter (comm ); Da. Fill (ds ); Conn. Close (); } } Public void GetSqlConnectionTable () { # Region namespace // Using System. Data. SqlClient; # Endregion # Region MSSQL SqlConnection // SqlConnection // C # how to connect to MSSQL // Windows Authentication // "Data Source = PEK7-6TKX23XSQLEXPRESS; Initial Catalog = Test; Trusted_Connection = Yes ;"; // SQL Server Authentication // "Server = 127.0.0.1SQLEXPRESS; database = Test; Uid = sa; PassWord = 123456 "; # Endregion // String strConn = @ "server = 127.0.0.1SQLEXPRESS; database = Test; Uid = sa; PassWord = 123456 "; String strConn = @ "Data Source = PEK7-6TKX23XSQLEXPRESS; Initial Catalog = Test; Trusted_Connection = Yes ;";
SqlConnection conn = new SqlConnection (strConn ); SqlCommand comm = new SqlCommand (); Comm. CommandText = "SELECT * FROM Users "; Comm. CommandType = CommandType. Text; Comm. Connection = conn; If (conn. State! = ConnectionState. Open) { Conn. Open (); DataSet ds = new DataSet (); SqlDataAdapter da = new SqlDataAdapter (comm ); Da. Fill (ds ); Conn. Close (); } } } } |