Add several Entity Framework functions (record memo)

Source: Internet
Author: User

Public static class databaseextensions
{
Public static datatable sqlqueryfordatatatable (this database dB, string SQL, Params sqlparameter [] parameters)
{

Sqlconnection conn = new system. Data. sqlclient. sqlconnection ();
Conn. connectionstring = dB. Connection. connectionstring;
If (conn. State! = Connectionstate. open)
{
Conn. open ();
}
Sqlcommand cmd = new sqlcommand ();
Cmd. Connection = conn;
Cmd. commandtext = SQL;

If (parameters. length> 0)
{
Foreach (VAR item in parameters)
{
Cmd. Parameters. Add (item );
}
}

Sqldataadapter adapter = new sqldataadapter (CMD );
Datatable table = new datatable ();
Adapter. Fill (table );
Return table;
}

Public static ienumerable sqlqueryfordynamic (this database dB, string SQL, Params object [] parameters)
{
Idbconnection defaultconn = new system. Data. sqlclient. sqlconnection ();
Return sqlqueryfordynamicotherdb (dB, SQL, defaultconn, parameters );
}

Public static ienumerable sqlqueryfordynamicotherdb (this database dB, string SQL, idbconnection Conn, Params object [] parameters)
{
Conn. connectionstring = dB. Connection. connectionstring;
If (conn. State! = Connectionstate. open)
{
Conn. open ();
}

Idbcommand cmd = conn. createcommand ();
Cmd. commandtext = SQL;

Idatareader datareader = cmd. executereader ();

If (! Datareader. Read ())
{
Return NULL; // null is returned If no result is returned.
}

# Region build dynamic fields

Typebuilder builder = databaseextensions. createtypebuilder (
"Ef_dynamicmodelassembly ",
"Dynamicmodule ",
"Dynamictype ");

Int fieldcount = datareader. fieldcount;
For (INT I = 0; I <fieldcount; I ++)
{
// DIC. Add (I, datareader. getname (I ));

// Type = datareader. getfieldtype (I );

Databaseextensions. createautoimplementedproperty (
Builder,
Datareader. getname (I ),
Datareader. getfieldtype (I ));
}

# Endregion

Datareader. Close ();
Datareader. Dispose ();
Cmd. Dispose ();
Conn. Close ();
Conn. Dispose ();

Type returntype = builder. createtype ();

If (parameters! = NULL)
{
Return dB. sqlquery (returntype, SQL, parameters );
}
Else
{
Return dB. sqlquery (returntype, SQL );
}
}

Public static typebuilder createtypebuilder (string assemblyname, string modulename, string typename)
{
Typebuilder = appdomain. currentdomain. definedynamicassembly (
New assemblyname (assemblyname ),
Assemblybuilderaccess. Run). definedynamicmodule (modulename). definetype (typename,
Typeattributes. Public );
Typebuilder. definedefaultconstructor (methodattributes. Public );
Return typebuilder;
}

Public static void createautoimplementedproperty (typebuilder builder, string propertyname, type propertytype)
{
Const string privatefieldprefix = "M _";
Const string getterprefix = "GET _";
Const string setterprefix = "SET _";

// Generate the field.
Fieldbuilder = builder. definefield (
String. Concat (
Privatefieldprefix, propertyname ),
Propertytype,
Fieldattributes. Private );

// Generate the property
Propertybuilder = builder. defineproperty (
Propertyname,
System. reflection. propertyattributes. hasdefault,
Propertytype, null );

// Property getter and setter attributes.
Methodattributes propertymethodattributes = methodattributes. Public
| Methodattributes. specialname
| Methodattributes. hidebysig;

// Define the getter method.
Methodbuilder gettermethod = builder. definemethod (
String. Concat (
Getterprefix, propertyname ),
Propertymethodattributes,
Propertytype,
Type. emptytypes );

// Emit the Il code.
// Ldarg.0
// Ld1_, _ Field
// RET
Ilgenerator getterilcode = gettermethod. getilgenerator ();
Getterilcode. emit (Opcodes. ldarg_0 );
Getterilcode. emit (Opcodes. lddes, fieldbuilder );
Getterilcode. emit (Opcodes. Ret );

// Define the setter method.
Methodbuilder settermethod = builder. definemethod (
String. Concat (setterprefix, propertyname ),
Propertymethodattributes,
Null,
New Type [] {propertytype });

// Emit the Il code.
// Ldarg.0
// Ldarg.1
// Stfld, _ Field
// RET
Ilgenerator setterilcode = settermethod. getilgenerator ();
Setterilcode. emit (Opcodes. ldarg_0 );
Setterilcode. emit (Opcodes. ldarg_1 );
Setterilcode. emit (Opcodes. stdes, fieldbuilder );
Setterilcode. emit (Opcodes. Ret );

Propertybuilder. setgetmethod (gettermethod );
Propertybuilder. setsetmethod (settermethod );
}

}

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.