1.c# Connecting to access
Program code:
-------------------------------------------------------------------------------
Using System.Data;
Using System.Data.OleDb;
..
String strconnection= "provider=microsoft.jet.oledb.4.0;";
[Email protected] "Data Source=c:begaspnetnorthwind.mdb";
OleDbConnection objconnection=new OleDbConnection (strconnection);
..
Objconnection.open ();
Objconnection.close ();
--------------------------------------------------------------------------------
Explain:
Connecting to an Access database requires importing additional namespaces, so it is essential to have the top two using commands!
Strconnection This variable contains the connection string needed to connect to the database, he specifies the data provider to use and the data source to use.
"Provider=Microsoft.Jet.OLEDB.4.0;" Refers to the data provider, which uses the Microsoft Jet engine, the data engine in access, which is connected to the database of access.
"Data Source=c:\begaspnet\northwind.mdb" is the location of the source, and his standard form is "Data Source=mydrive:mypath\myfile.mdb".
You can also write the address System.Web.HttpContext.Current.Server.MapPath ("Data/database.mdb");
Ps:
1. The "@" symbol after "+ =" Prevents the "\" in subsequent strings from resolving to escape characters.
2. If the database file you want to connect to is in the same directory as the current file, you can also connect using the following method:
strconnection+= "Data source=";
Strconnection+=mappath ("Northwind.mdb");
This will save you from writing a lot of things!
3. Be aware that the parameters in the connection string are separated by semicolons.
"OleDbConnection objconnection=new OleDbConnection (strconnection);" This is the use of a defined connection string to establish a linked object, the future operation of the database we have to deal with this object.
"Objconnection.open ();" This is used to open the connection. At this point, the connection to the Access database is complete.
If you have a password:
String connstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + application.startuppath+ @ "\ database. mdb; Persist Security Info=false; Jet oledb:userid= your name; Database password= your password ";
--------------------------------------------------------------------------------
2.c# Connecting to SQL Server
Program 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 ();
--------------------------------------------------------------------------------
Explain:
The mechanism for connecting to a SQL Server database is not much different from the mechanism for connecting to access, except for changing the parameters in the Connection object and the connection string.
First, the namespaces used to connect to SQL Server are not "System.Data.OleDb", but "System.Data.SqlClient".
Next is his connection string, we introduce a parameter (note: The parameters are separated by semicolons):
"User Id=sa": The authenticated user name for the connection database is SA. He also has an alias "UID", so this sentence we can also write "Uid=sa".
"Password=": The authentication password for the connection database is empty. His nickname is "pwd", so we can write "pwd=".
Note here that your SQL Server must already have a username and password to log in, or you cannot log in in such a way. If your SQL Server is set to Windows logon, you do not need to use "User ID" and "password" here. Such a way to log in, and you need to use "TRUSTED_CONNECTION=SSPI" to log in.
"Initial Catalog=northwind": The data source used is the database "Northwind". His nickname is "database", this sentence can be written as "Database=northwind".
"Server=yoursqlserver": Use a server named "YourSQLServer". His nickname is "Data Source", "Address", "Addr". If you are using a local database and you have defined an instance name, you can write as " server= (local) \ instance name, or, if it is a remote server, replace "(local)" with the name or IP address of the remote server.
"Connect timeout=30": The connection time-out is 30 seconds.
Here, the constructor used to establish the connection object is: SqlConnection.
--------------------------------------------------------------------------------
3.c# Connect Oracle
Program code:
--------------------------------------------------------------------------------
Using System.Data.OracleClient;
using System.Data;
//Add a button to the form called Button1, double-click Button1, enter the following code
private void Button1_Click (object sender, System.EventArgs e)
{
String connectionstring= "Data source=sky;user=system;password=manager;"; /write connection string
OracleConnection conn=new oracleconnection (ConnectionString);//Create a new connection
try
{
Conn. Open ();
OracleCommand Cmd=conn. CreateCommand ();
cmd.commandtext= "SELECT * from MyTable";//write SQL statements here
OracleDataReader Odr=cmd. ExecuteReader ();//Creates a Oracledatereader object
while (ODR. Read ())//reads data if ODR. Read () returns false to indicate the end of the recordset
{
Response.Write (ODR. Getoraclestring (1). ToString ());//Output field 1, this number is the field index, how to use the field name still needs to study the
}
ODR. Close ();
}
catch (Exception ee)
{
Response.Write (EE. Message); If there is an error, output error message
}
finally
{
Conn. Close (); Close connection
}
}
-------------------------------------------------------------------------------
4.c# connecting MySQL
Program code:
--------------------------------------------------------------------------------
Using Mysqldrivercs;
Establishing a database connection
Mysqlconnection Dbconn;
Dbconn = new Mysqlconnection (new mysqlconnectionstring ("localhost", "MySQL", "Root", "", 3306). asstring);
Dbconn.open ();
Execute Query statement
Mysqlcommand Dbcomm;
Dbcomm = new Mysqlcommand ("Select Host,user from User", dbconn);
Reading data
Mysqldatareader dbreader = Dbcomm.executereaderex ();
Show data
Try
{
while (Dbreader.read ())
{
Console.WriteLine ("Host = {0} and User = {1}", dbreader.getstring (0), dbreader.getstring (1));
}
}
Finally
{
Dbreader.close ();
Dbconn.close ();
}
To close a database connection
Dbconn.close ();
--------------------------------------------------------------------------------
5.c# Connecting IBM DB2
Program code:
--------------------------------------------------------------------------------
Oledbconnection1.open ();
Open a database connection
Oledbdataadapter1.fill (DataSet1, "Address");
Fill in the dataset with the data you have obtained
Datagrid1.databind ();
Binding Data
Oledbconnection1.close ();
Close connection
Increase database data
Add a TextBox with a number of fields to the Web form, and a button to increment the Click Response Event code for the button:
This.OleDbInsertCommand1.CommandText = "Insertsintosaddress (NAME,
EMAIL, age, ADDRESS) VALUES
(' "+textbox1.text+" ', ' "+textbox2.text+" ', ' "+textbox3.text+" ', ' "+textbox4.text+" ') ";
OleDbInsertCommand1.Connection.Open ();
Open connection
Oledbinsertcommand1.executenonquery ();
Execute the SQL statement
OleDbInsertCommand1.Connection.Close ();
Close connection
--------------------------------------------------------------------------------
6.c# connecting Sybase
Program code: (OLE DB)
--------------------------------------------------------------------------------
provider=sybase.aseoledbprovider.2;initial catalog= database name; User id= username; data source= source; Extended properties= ""; Server NAME=IP address; Network Protocol=winsock; Server Port address=5000;
--------------------------------------------------------------------------------
7.c# Connection FireBird
Program code: (fbconnection)
--------------------------------------------------------------------------------
Fbconnectionstringbuilder sb = new Fbconnectionstringbuilder (); Sb. ServerType = Fbservertype.default; SYSDBA sb. UserID = "SYSDBA"; Masterkey sb. Password = "Masterkey"; Sb. Database = "F:\\data\\testdb." FDB "; Sb. Charset = "gb_2312"; Fbconnection Connection1 = new Fbconnection (sb.) ToString ()); Connection1. Open ();
--------------------------------------------------------------------------------
8.c# Connection InterBase
Program code: (OLE DB)
--------------------------------------------------------------------------------
Oledbconnectionstringbuilder cb = new Oledbconnectionstringbuilder (); Cb. Provider = "LCPI. Ibprovider "; Cb. ADD ("Location", @ "192.168.1.208:e:\keyu.gdb"); Cb. ADD ("User ID", "sysdba"); Cb. ADD ("Password", "Masterkey"); Cb. ADD ("CType", "win1251"); Label1. Text = cb. ToString (); OleDbConnection con = new OleDbConnection (CB. ToString ()); Con. Open (); OleDbTransaction trans = con. BeginTransaction (); OleDbCommand cmd = new OleDbCommand ("SELECT count (*) from TableName", con, trans); Label2. Text = string. Format ("total {0} bar data", CMD.) ExecuteScalar (). ToString ()); Trans.commit (); Con. Close ();
NET (C #) connect to various databases-highlights