Sub loadcommandlist () Dim objconn as new ODBC. odbcconnection Dim objcmd as new ODBC. odbccommand Dim objdatareader as ODBC. odbcdatareader Commandlist. Items. Clear () Objconn. connectionstring = "driver = {Microsoft Access Driver (*. mdb)}; DBQ =" & application. startuppath & "\ database. mdb" Debug. Print (application. startuppath & "\ database. mdb ") Objconn. open () 'connect to the database Objcmd. Connection = objconn Objcmd. commandtext = "select * From commands" Objdatareader = objcmd. executereader () 'Run the SQL statement While objdatareader. Read () 'reads data. The following is irrelevant to the database connection. Commandlist. Items. Add (objdatareader. getstring (0) & "," & objdatareader. getstring (1 )) End while Objconn. Close () End sub II Asp.net connection SQL Server2000 data warehouse routine details: <% @ Import namespace = "system. Data" %> <% @ Import namespace = "system. Data. sqlclient" %> <SCRIPT Laguage = "VB" runat = "server"> Sub page_load (sender as object, e as eventargs) Dim myconnection as sqlconnection Dim mycommand as sqlcommand Dim ds as Dataset '1. Connect to the database Myconnection = new sqlconnection ("Server = localhost; database = pubs; uid = ueytjdf; Pwd = doekdf ") Myconnection. open () La1.text = "connection opened! " '2. Create a new table Mycommand = new sqlcommand ("create table [test] ([ID] [int] identity (1, 1) not null, [name] [char] (10) Collate chinese_prc_ci_as null, [sex] [char] (10) Collate chinese_prc_ci_as null) ", myconnection) Mycommand. executenonquery () La2.text = "new table created! " '2 add record Mycommand = new sqlcommand ("insert into [test] (name, sex) values ('huang Zhiwen ', 'male')", myconnection) Mycommand. executenonquery () La3.text = "New Record inserted! " '3 update data Mycommand = new sqlcommand ("Update [test] Set Name = 'Smith 'Where name = 'lilim'", myconnection) Mycommand. executenonquery () La4.text = "record updated! " '4 deleting data Mycommand = new sqlcommand ("delete from [test] Where name = 'Smith '", myconnection) Mycommand. executenonquery () La5.text = "record deleted! " '5 use a DataGrid to display data mycommand = new sqlcommand ("select * from [test]", myconnection) mydatagrid. datasource = mycommand. executereader () mydatagrid. databind () end sub bordercolor = "black" borderwidth = "1" gridlines = "both" cellpadding = "3" cellspacing = "0" font-name = "verdana" font-size = "10pt" headerstyle-backcolor = "# aaaadd " alternatingitemstyle-backcolor =" # eeeeee " </Body> </Html> Bytes ---------------------------------------------------------------------------------------------------- Asp.net access data warehouse routine <% @ Import namespace = "system. Data" %> <% @ Import namespace = "system. Data. oledb" %> <SCRIPT Laguage = "VB" runat = "server"> Dim myconnection as oledbconnection Dim mycommand as oledbcommand Sub page_load (sender as object, e as eventargs) '1. Connect to the database Dim dbname as string Dbname = server. mappath ("authors. mdb ") Myconnection = new oledbconnection ("provider = Microsoft. Jet. oledb.4.0; Data Source =" & dbname) Myconnection. open () La1.text = "connection opened! " '2. Add record Mycommand = new oledbcommand ("insert into authors (authors, country) values ('simson', 'usa')", myconnection) Mycommand. executenonquery () La2.text = "New Record inserted! " '3. Access) Mycommand = new oledbcommand ("Update authors set authors = 'bennett 'Where authors = 'simson'", myconnection) Mycommand. executenonquery () La3.text = "record updated! " '4. Access) Mycommand = new oledbcommand ("delete from authors where authors = 'David'", myconnection) Mycommand. executenonquery () La4.text = "record deleted! " '5 use dategrid to display data Mycommand = new oledbcommand ("select * from authors", myconnection) Mydatagrid. datasource = mycommand. executereader () Mydatagrid. databind () end sub bordercolor = "black" borderwidth = "1" gridlines = "both" cellpadding = "3" cellspacing = "0" font-name = "verdana" font-size = "10pt" headerstyle-backcolor = "# aaaadd " alternatingitemstyle-backcolor =" # eeeeee " 1.c# connect to access [ copy this Code ] Code: using system. data; using system. data. oledb; .. string strconnection = "provider = Microsoft. jet. oledb.4.0; "; strconnection + = @" Data Source = C: begaspnetnorthwind. mdb "; oledbconnection objconnection = new oledbconnection (strconnection); .. objconnection. open (); objconnection. close (); Explanation: To connect to the Access database, you need to import additional namespaces. Therefore, the first two using commands are required! The strconnection variable stores the connection string required to connect to the database. It specifies the data provider to be used and the data source to be used. "Provider = Microsoft. jet. oledb.4.0; "is the index data provider. Here we use the Microsoft Jet Engine, which is the Data Engine in access. Asp.net is connected to the access database. "Data Source = c: \ begaspnet \ northwind. mdb" indicates the location of the data source. Its standard format is "Data Source = mydrive: mypath \ myfile. mdb ". PS: 1. the "@" symbol after "+ =" prevents the "\" symbol in the subsequent string from being parsed as an escape character. 2. If the database file to be connected is in the same directory as the current file, you can use the following method to connect: Strconnection + = "Data Source = "; Strconnection + = mappath ("northwind. mdb "); In this way, you can write a lot of things! 3. Note that parameters in the connection string must be separated by semicolons. "Oledbconnection objconnection = new oledbconnection (strconnection);" this sentence uses the defined connection string to create a link object. In the future, we will deal with this object for database operations. "Objconnection. open ();" this is used to open the connection. At this point, the connection to the ACCESS database is complete. 2. C # connect to SQL Server
[ Copy this code ] Code: using system. Data; Using system. Data. sqlclient; .. String strconnection = "User ID = sa; Password = ;"; Strconnection + = "Initial catalog = northwind; server = yoursqlserver ;"; Strconnection + = "Connect timeout = 30 "; Sqlconnection objconnection = new sqlconnection (strconnection ); .. Objconnection. open (); Objconnection. Close ();Explanation: There is no major difference between the mechanism for connecting to the SQL Server database and the access mechanism, but it only changes the different parameters in the connection object and connection string. First, the namespace used to connect to SQL Server is not "system. Data. oledb", but "system. Data. sqlclient ". The second is his connection string. We will introduce them one by one (note: the parameters are separated by semicolons ): "User ID = sa": the authenticated user name used to connect to the database is Sa. It also has an alias "uid", so we can also write this statement as "uid = sa ". "Password =": The verification password for connecting to the database is blank. Its alias is "PWD", so we can write it as "Pwd = ". Note that your SQL server must have a user name and password set to log on. Otherwise, you cannot log on using this method. if your SQL Server is set to Windows logon, you do not need to use the "user ID" and "password" methods to log on here, you need to use "trusted_connection = sspi" to log on. "Initial catalog = northwind": the data source used is "northwind". Its alias is "Database". This statement can be written as "database = northwind ". "Server = yoursqlserver": Use a server named "yoursqlserver. its alias is "Data Source", "Address", "ADDR ". if the local database is used and the Instance name is defined, you can enter "Server = (local) \ Instance name". If it is a remote server) "Replace with the name or IP address of the remote server. "Connect timeout = 30": the connection timeout is 30 seconds. Here, the constructor used to create a connection object is sqlconnection. 3. C # connect to Oracle [ Copy this code ] Code: using system. Data. oracleclient; Using system. Data; // Add a button on the form called button1, double-click button1, and enter the following code Private void button#click (Object sender, system. eventargs E) { String connectionstring = "Data Source = sky; user = system; Password = manager;"; // write the connection string Oracleconnection conn = new oracleconnection (connectionstring); // create a new connection Try { Conn. open (); Oraclecommand cmd = conn. createcommand (); Cmd. commandtext = "select * From mytable"; // write an SQL statement here Oracledatareader ODR = cmd. executereader (); // create an oracledatereader object While (ODR. Read () // read data. If ODR. Read () returns false, it indicates the end of the record set. { Response. Write (ODR. getoraclestring (1). tostring (); // output field 1. This number is a field index. How to use the field name remains to be studied. } ODR. Close (); } Catch (exception ee) { Response. Write (EE. Message); // if an error occurs, the error message is output. } Finally { Conn. Close (); // close the connection } } 4. C # connect to MySQL[Copy this code] Code: Using mysqldrivercs; // Establish a database connection Mysqlconnection dbconn; Dbconn = new mysqlconnection (New mysqlconnectionstring ("localhost", "MySQL", "root", "", 3306). asstring ); Dbconn. open (); // Execute the query statement Mysqlcommand dbcomm; Dbcomm = new mysqlcommand ("select host, user from user", dbconn ); // Read data Mysqldatareader dbreader = dbcomm. executereaderex (); // Display data Try { While (dbreader. Read ()) { Console. writeline ("host = {0} and user = {1}", dbreader. getstring (0), dbreader. getstring (1 )); } } Finally { Dbreader. Close (); Dbconn. Close (); } // Close the database connection Dbconn. Close (); 5. C # connect to IBM DB2
[ Copy this code ] Code: oledbconnection1.open (); // Open the database connection Oledbdataadapter1.fill (dataset1, "Address "); // Enter the obtained data in dataset. Datagrid1.databind (); // Bind data Oledbconnection1.close (); // Close the connection // Add database dataAdd a textbox and a button corresponding to the number of fields on the web form. The code for adding a click RESPONSE event to the button is as follows :[Copy this code] Code: This. oledbinsertcommand1.commandtext = "insertsintosaddress (name, Email, age, address) Values ('"+ Textbox1.text +"', '"+ textbox2.text +"', '"+ textbox3.text +"', '"+ textbox4.text + "')"; Oledbinsertcommand1.connection. open (); // Open the connection Oledbinsertcommand1.executenonquery (); // Execute this SQL statement Oledbinsertcommand1.connection. Close (); // Close the connection 6. C # connect to Sybase (Oledb )[Copy this code] Code: provider = Sybase. aseoledbprovider.2; initial catalog = database name; user id = username; Data Source = data source; extended properties = ""; server name = IP address; network protocol = Winsock; server port address = 5000; |