Link
Before using dataprovider, you must configure the following nodes in Web. config and add them between <configsections> </configsections>:
< Isline. Data. Configuration >
< Databaseconnection > < Dbtype > Isline. Data. datatypeenum. databasetype. sqlserver </ Dbtype >
< Server > Server IP </ Server >
< Userid > SA </ Userid >
< Pwdtype > Cryptography. Decrypting </ Pwdtype > // Or <pwdtype> cryptography. strengthdecrypting </pwdtype> or <pwdtype> cryptography. None </pwdtype>
< PWD > 111111 </ PWD >
< Database > UML </ Database > // Sqlserver only
</ Databaseconnection >
</ Isline. Data. Configuration >
Databaseconnection indicates the database type, pwdtype indicates the Password Encryption Policy, and isline provider has a built-in 3 Policy for selection, as shown in the configuration file. The preceding example is for sqlserver. The oralce node configuration method is as follows:
< Isline. Data. Configuration >
< Databaseconnection > < Dbtype > Isline. Data. configuration. oracleconnectstring </ Dbtype >
< Server > TNS alias </ Server >
< Userid > SA </ Userid >
< Pwdtype > Cryptography. Decrypting </ Pwdtype > // Or <pwdtype> cryptography. strengthdecrypting </pwdtype> or <pwdtype> cryptography. None </pwdtype>
< PWD > 111111 </ PWD >
< Otherplus > Provider = Msdaora. 1 ; Persist Security info = False; </ Otherplus > // Oracle only
</ Databaseconnection >
</ Isline. Data. Configuration >
Alternatively, you can use the following simple configuration method to save the trouble of node configuration:
Sqlserver:
<Add key = "isline. data. configuration. sqlserverconnectstring "value =" Data Source = Server IP; user; Password = 111111; Integrated Security = no; enlist = false; pooling = true; "/>
ORACLE:
<Add key = "isline. data. configuration. oracleconnectstring "value =" Data Source = TNS alias; user; Password = 111111; Integrated Security = no; enlist = false; pooling = true; "/>
After the node is configured, call it using the following methods: connection-oriented and non-connection-oriented (for example, dataset/Reader/adapter) it must be instantiated before use and called after use. the dispost () method releases resources. dispost () is a method rewritten by dataprovider. Once called, all resources, including connection, command, and datareader, will be released, for non-connection operations, you can directly call them. If datareader and adapter are used for connection-oriented operations, simply execute SQL statements as non-connection operations. For example, to access a database, simply execute the following SQL statements:
Oracleprovider. executenonquery ();
Or
Sqlprovider. executenonquery ();
Example:
Oracleprovider op = new oracleprovider ();
Op. somefunc ();
The method list is as follows:
Method Name |
Description |
Instantiation required? |
Orabit2bool |
|
No |
Executescalar |
4 reloads. After an SQL statement is passed in, the stored procedure name returns the first element. |
No |
Bool2orabit |
|
No |
Executenonquery |
Five reloads. After an SQL statement is passed in, the stored procedure name is executed in the database. |
No |
Executereader |
Five reloads. After an SQL statement is passed in, the stored procedure name returns the corresponding type of datareader. |
Yes |
Executedataset |
Five reloads. After an SQL statement is passed in, the stored procedure name returns the corresponding dataset type. |
Yes |
Executedataadapter |
Five reloads. After an SQL statement is passed in, the stored procedure name returns the corresponding type of dataadapter. |
Yes |
Dispose |
Release all resources |
Yes |
Conn |
Specify connection for the instance |
Yes |
CMD |
Specify command for instance |
Yes |
Input parameter list: (descriptions of input parameters for the above overload methods)
Parameter Name |
Parameter type |
Description |
Plain text |
String |
SQL statement or stored procedure name |
Connectionkey |
String |
Connection string (only when web is not used. config is used to configure database connections in nodes. For example, in a project, many databases, primary business databases, and log databases are sometimes used, in this case, you can configure the primary business database as the default database, and use the log database to pass this parameter) |
Isusingoracletransaction |
Bool |
Whether to enable Transaction Processing ("!" is used between SQL statements in the same transaction Connection) |
Partition type |
Commandtype |
This parameter specifies whether the "plain text" parameter is an SQL statement or a stored procedure. |
Commandparameters |
Params system. Data. oracleclient. oracleparameter [] |
Use Params |
Connection |
Oracleconnection |
The function is the same as the connectionkey, but it is instantiated. |
Connection |
Sqlconnection |
Same as above |
Srctable |
String |
Fill in the table name when using the "executedataset" Method |
Commandparameters |
Params system. Data. sqlclient. sqlparameter [] |
Use Params |
Instance:
1. Execute an SQL statement:
Oracleprovider. executenonquery (SQL );
2. Execute a batch of SQL statements using transactions: oracleprovider. executenonquery (SQL, true, commandtype. Text, null );
3. Execute a stored procedure:
Oracleparameter [] oracleparam = New Oracleparameter [ 6 ];
Oracleparam [ 0 ] = New Oracleparameter ( " Parametername1 " , Oracletype. varchar, 32 );
Oracleparam [ 0 ]. Value = " A " ;
Oracleparam [ 1 ] = New Oracleparameter ( " Parametername2 " , Oracletype. varchar, 20 );
Oracleparam [ 1 ]. Value = " B " ;
Oracleprovider. executenonquery (storedprocedurename, Null , Commandtype. storedprocedure, oracleparam );
Returns a datareader.
New oracleprovider (). executereader (SQL );
- Returns a dataset.
Oracleprovider (). executedataset (SQL );
- Returns a dataset using the specified connection.
New oracleprovider (). executedataset (SQL, "connectionstr", 0 );
- Call the stored procedure and return a dataset
New oracleprovider (). executedataset (SQL, commandtype. storedprocedure, "testtable", null );
The above are just a few simple examples. In short, using dataprovider, you can control the database through configuration.CodeThere are only operation statements at the level, and there is no database connection or configuration statement.
Dataprovider also has a class "dbprovider". This provider does not specify whether it is sqlserver, Oracle, or other databases. You need to comply with the interface contract mentioned above in advance to implement configution. DLL, and placed in the agreed location (this location is in the web. config ),ProgramThe method is automatically reflected and used. In the future, when you change the database, you only need to replace the DLL and do not need to modify the main program.
"Dbprovider" also encapsulates Microsoft's dbproviderfactory. As you may know, I will not introduce it much. The call method name and method are the same as those described above.
To be continued
I amAicken)Welcome to my next articleArticle.