C # simple generation and application DLL file (database connection)

Source: Internet
Author: User

1. First create a project class library in C # and add the files opertedatabase. CS and cache. CS.

The Code is as follows:

File opertedatabase. CS

Using system; <br/> using system. collections. generic; <br/> using system. text; <br/> using system. data; <br/> using system. data. SQL; <br/> using system. data. sqlclient; <br/> using system. configuration; <br/> using system. collections; <br/> using system. XML. LINQ; </P> <p> namespace databasebing <br/> {<br/> public class opertedatabase <br/>{< br/> Private Static string connectionstirngname; <br/> private stat IC sqlconnection conn; <br/> Private Static int count; <br/> Public static int count <br/> {<br/> Get <br/> {<br/> return count; <br/>}< br/> set <br/> {<br/> COUNT = value; <br/>}< br/> Public static string connstrname <br/>{< br/> Get <br/>{< br/> return connectionstirngname; <br/>}< br/> set <br/>{< br/> If (! String. isnullorempty (value) <br/>{< br/> connectionstirngname = value; <br/>}< br/> Private Static sqlconnection getsqlconnection () <br/>{< br/> string constr = (string) cache. getdata (connectionstirngname); <br/> If (string. isnullorempty (constr) <br/>{< br/> try <br/>{< br/> constr = convert. tostring (configurationmanager. connectionstrings [connectionstirngname]. connectionstring; <B R/>}< br/> catch (exception ex) <br/>{< br/> throw new exception (ex. message, ex); <br/>}< br/> cache. cachingdata (connectionstirngname, constr); <br/>}< br/> return New sqlconnection (constr); <br/>}< br/> Public static sqlcommand getsqlcommand (string strsql) <br/>{< br/> If (! String. isnullorempty (strsql) <br/>{< br/> conn = getsqlconnection (); <br/> If (Conn. state = connectionstate. closed) <br/> Conn. open (); <br/> sqlcommand cmd = new sqlcommand (strsql, Conn); <br/> return cmd; <br/>}< br/> else <br/> return NULL; <br/>}< br/> Public static sqlcommand getsqlcommand (string procname, Params sqlparameter [] PARAM) <br/>{< br/> conn = getsqlconnection (); <br/> If (Conn. s Tate = connectionstate. closed) <br/> Conn. open (); <br/> sqlcommand cmd = new sqlcommand (procname, Conn); <br/> cmd. commandtype = commandtype. storedprocedure; <br/> If (Param! = NULL) <br/>{< br/> foreach (sqlparameter paramerter In Param) <br/>{< br/> cmd. parameters. add (paramerter); <br/>}</P> <p >}< br/> return cmd; </P> <p >}< br/> Public static void executesqlcommand (string strsql) <br/>{< br/> If (! String. isnullorempty (strsql) <br/>{< br/> getsqlcommand (strsql ). executenonquery (); <br/> If (Conn. state = connectionstate. open) <br/> Conn. close (); <br/>}< br/> Public static void executesqlcommand (string procname, sqlparameter [] PARAM) <br/>{< br/> getsqlcommand (procname, Param ). executenonquery (); <br/> If (Conn. state = connectionstate. open) <br/> Conn. close (); </P> <p >}< br/> Public St Atic dataset getdataset (string strsql) <br/>{< br/> If (! String. isnullorempty (strsql) <br/>{< br/> sqlcommand cmd = getsqlcommand (strsql); <br/> COUNT = (INT) cmd. executescalar (); <br/> sqldataadapter SDA = new sqldataadapter (CMD); <br/> dataset DS = new dataset (); <br/> SDA. fill (DS); <br/> If (Conn. state = connectionstate. open) <br/> Conn. close (); <br/> return Ds; <br/>}< br/> else <br/> return NULL; <br/>}< br/> Public static dataset getdataset (Str Ing procname, sqlparameter [] PARAM) <br/>{< br/> sqlcommand cmd = getsqlcommand (procname, Param); <br/> COUNT = (INT) cmd. executescalar (); <br/> sqldataadapter SDA = new sqldataadapter (getsqlcommand (procname, Param); <br/> dataset DS = new dataset (); <br/> SDA. fill (DS); <br/> If (Conn. state = connectionstate. open) <br/> Conn. close (); <br/> return Ds; <br/>}< br/> Private Static sqlparameter creat Eparam (string paramename, sqldbtype dbtype, int32 size, parameterdirection direction, object Value) <br/>{< br/> sqlparameter Param; <br/> If (size> 0) <br/>{< br/> param = new sqlparameter (paramename, dbtype, size ); <br/>}< br/> else <br/> param = new sqlparameter (paramename, dbtype); <br/> param. direction = direction; <br/> If (! (Direction = parameterdirection. output & value = NULL) <br/>{< br/> param. value = value; <br/>}< br/> return Param; <br/>}< br/> Public static sqlparameter createinputparam (string paramename, sqldbtype dbtype, int32 size, object value) <br/>{< br/> return createparam (paramename, dbtype, size, parameterdirection. input, value); <br/>}< br/> Public static sqlparameter createoutputparam (string paramename, sqldbtype dbtype, int32 size) <br/>{< br/> return createparam (paramename, dbtype, size, parameterdirection. output, null); <br/>}< br/> Public static sqlparameter createreturnvalueparam (string paramename, sqldbtype dbtype, int32 size) <br/>{< br/> return createparam (paramename, dbtype, size, parameterdirection. returnvalue, null); <br/>}</P> <p >}< br/>}</P> <p>

File Cache. CS

Using system; <br/> using system. collections. generic; <br/> using system. text; <br/> using system. collections; </P> <p> namespace databasebing <br/> {<br/> class cache <br/>{< br/> Private Static hashtable cache = hashtable. synchronized (New hashtable (); <br/> Public static void cachingdata (string key, object Value) <br/>{< br/> cache [Key] = value; <br/>}< br/> Public static object getdata (string key) <br/>{< br/> return (object) cache [Key]; <br/>}</P> <p>

2. Test the preceding two files to the C:/Windows/Microsoft. NET/framework/v2.0.50727 directory. The C # compiler, CSC. EXE, is provided under this directory.

3. Open the command window-> Enter cmd to the console-> Cd C:/Windows/Microsoft. NET/framework/v2.0.50727

4. Run the CSC command CSC/Target: Library/out: databasebingdll. dll opertedatabase. CS cache. CS.

5. After completing the preceding steps, you can find the DLL file databasebingdll. dll in this directory.

Note:

/Target: the Library compiler option notifies the compiler to output the DLL file instead of the EXE file. The/out compiler option followed by the file name is used to specify the DLL file name.

6. Create a new website. In Solution Explorer, right-click the project name and choose "add reference"> "add reference" to view databasebingdll. dll. Click "add.

7. Drag a gridview control on the default page of the project and run the following code in default. CS:Using system; <br/> using system. collections; <br/> using system. configuration; <br/> using system. data; <br/> using system. LINQ; <br/> using system. web; <br/> using system. web. security; <br/> using system. web. ui; <br/> using system. web. UI. htmlcontrols; <br/> using system. web. UI. webcontrols; <br/> using system. web. UI. webcontrols. webparts; <br/> using system. XML. LINQ; </P> <p> Public partial class _ default: system. web. UI. page <br/>{< br/> protected void page_load (Object sender, eventargs e) <br/>{</P> <p> opertedatabase. connstrname = "onlineresconnectionstring"; <br/> opertedatabase. getdataset ("select * from customer"); <br/> gridview1.datasource = opertedatabase. getdataset ("select * from customer "). tables [0]; <br/> gridview1.databind (); </P> <p >}< br/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.