Because individuals are learning and using the .NET, in order to facilitate the connection and operation of the database, they will be written according to their own needs to be written according to their needs. Function: Connect to the MS SQL Database to read the INI file operation database and return the corresponding result
INI file content: [dbconnection] server = localhostdatabase = mydatabaseDB_User = SADB_PASS = file name: mydb.cs
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls ; using System.Web.UI.HtmlControls; using System.Data.OleDb; using System.Text.RegularExpressions; using System.IO; using System.Web.Mail; using System.Text; using System.Xml; using System.Runtime .Interopservices;
namespace Linkdb {public class Mydb {public string path; // INI file name [DllImport ( "kernel32")] private static extern long WritePrivateProfileString (string section, string key, string val, string filePath); [DllImport ( "kernel32") ] private static extern int GetPrivateProfileString (string section, string key, string def, StringBuilder retVal, int size, string filePath); public string ConnStr; private int Index; public mydb () {//IniFile(HttpContext.Current.Server.MapPath ("/webwork/dbconn.ini")); inIfile (System.Environment.ini "); String Server = ReadValue (" DBConnection "," Server "); string database = readvalue (" dbconnection " , "database"); string db_user = ReadValue ( "dbconnection", "db_user"); string db_pass = ReadValue ( "dbconnection", "db_pass"); ConnStr = "Provider = SQLOLEDB; Data Source =" server "; Initial Catalog = " Database "; user ID = " DB_USER "; Password = " DB_Pass " ";} public bool testlink () {TRY {OLEDBCONNECTION CONN; conn = new OLEDBCO Nnection (conn.open (); return true;} catch (exception) {returnaf false;}}
Public void inIfile (string inipath) {path = inipath;
Public void WriteValue (String Section, String Key, String Value) {WritePrivateProfileString (Section, Key, Value, this.path);} // Write ini
public string ReadValue (string Section, string Key) {StringBuilder temp = new StringBuilder (255); int i = GetPrivateProfileString (Section, Key, "", temp, 255, this.path); return temp.ToString ();} / / read ini public OleDbDataReader GetDataReader (string SelectSQL) {OleDbConnection Conn; OleDbCommand Cmd; OleDbDataReader myDataReader; Conn = new OleDbConnection (ConnStr); Conn.Open (); Cmd = new OleDbCommand (SelectSQL, Conn); myDataReader = cmd.ExecuteReader ( ); return myDataReader;} public DataView GetDataView (string SelectSQL) {OleDbConnection Conn; OleDbDataAdapter Cmd; DataSet ds; string TableName; Conn = new OleDbConnection (ConnStr); Cmd = new OleDbDataAdapter (SelectSQL, Conn); Index = 1; TableName = "Table" index; ds = new dataset (); cmd.fill (DS, TABLENAME); return (DS.TABLES [TABLENAME]);} public void executesql (String ActionQuery) {OLEDBConnection Conn; conn = New OLEDBConnectio N (conn.open (); OLEDBCommand cmd; cmd = new oledbcommand (actionQuery, conn); cmd.executenonquery ();}}}
Compiled as a DLL file
In the console input: CSC / Target: library mydb.cs mydb.dll
Apply this class; first reference this class in the project Reference Namespace in the page: use linkDb; inherit this class: protected mydb db = new mydb ();
Such as execution query: string sqlstr = "select * from myTable"; OLEDBDATAREADER DR1 = DB.GetDataReader (SQLSTR); if (Dr1.Read ()) {...}