Oracle+ado.net (b)

Source: Internet
Author: User

Features added:

Added public new, update function in Basedal;

Add the following code to the model layer:

<summary>
Identify self-growth
</summary>
private bool isautoid = true;

public bool Isautoid
{
get {return isautoid;}
set {isautoid = value;}
}

private String columns = "*";

public string Columns
{
get {return columns;}
set {columns = value;}
}

private string where;

public string Where
{
get {return where;}
set {where = value;}
}

Add a parameterized Query class under namespace Myoracle.dal:

Using System.Data;
Using System.Data.OracleClient;
<summary>
Parameterized Query Classes
</summary>
public class Dbparam
{
<summary>
Parameter key
</summary>
private string _paramname = "";
<summary>
Parameter type
</summary>
Private OracleType _paramdbtype;
<summary>
Parameter values
</summary>
Private object _paramvalue = null;


public string paramname
{
get {return _paramname;}
set {_paramname = value;}
}

Public OracleType Paramdbtype
{
get {return _paramdbtype;}
set {_paramdbtype = value;}
}

public Object Paramvalue
{
get {return _paramvalue;}
set {_paramvalue = value;}
}
}

Specific code at the Basedal layer (using reflection technology):

StringBuilder sb = new StringBuilder ();
StringBuilder paramstr = new StringBuilder ();
Sb. AppendFormat ("insert into {0} (", TableName);
list<dbparam> list = new list<dbparam> ();

propertyinfo[] Propertys = typeof (T). GetProperties (BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public);
for (int i = 0; i < Propertys. Length; i++)
{
if (t.isautoid)
{
if (Propertys[i]. Name = = PrimaryKey)
Continue
}
DateTime type processing (minimum date cannot be less than 1900.1.1)
if (Propertys[i]. PropertyType = = typeof (DateTime) && ((datetime) propertys[i]. GetValue (model, NULL)) < new DateTime (1900, 1, 1))
{
Propertys[i]. SetValue (model, New DateTime (1900, 1, 1), NULL);
}
Sb. Append (Propertys[i]. Name+ ",");
Paramstr.append (":" +propertys[i). Name+ ",");
Dbparam param = new Dbparam () {
Paramname= ":" +propertys[i]. Name,
Paramdbtype=typeconvert.getoracledbtype (Propertys[i]. PropertyType),
Paramvalue=propertys[i]. GetValue (Model,null)
};
List. ADD (param);
}
Sb. Replace (",", ")", SB. length-1,1);
Paramstr.replace (",", ")", paramstr.length-1,1);
Sb. Append ("values");
Sb. Append (PARAMSTR);//In Plsql Although you can add a semicolon ";", but here you cannot add a semicolon ";"
if (t.isautoid)
{
/*
* Get the next value of a sequence first:
Select Myseq.nextval from dual;

This value is then inserted into the data table as a primary key value:
INSERT INTO MyTable (ID, ...) VALUES (Id_val, ...)
* */
}
OracleConnection Conn=dbaction.getconn ();
OracleCommand com = new OracleCommand (sb.) ToString (), conn);
foreach (Dbparam item in list)
{

Com. Parameters.Add (Dbhelper.createparam (item. Paramname,item. Paramvalue));
Com. Parameters.Add (P);
}
OracleString rowID;
Conn. Open ();
Com. Executeoraclenonquery (out rowID);
Conn. Close ();

As for updating the Update method, we need to encapsulate a method first:

<summary>
Whether the field value is updated by the columns definition of Basemodel ===> infers whether the reflected property needs to be updated
</summary>
<param name= "model" >columns defined columns </param>
<param name= "Val" > Properties of Reflection </param>
<returns> whether updates </returns>
private bool Isupdateproperty (T model,string val)
{
BOOL result = FALSE;
String Strs=model. Columns;
if (strs== "*")
{
return true;
}
String[] Cols=strs. Split (', ');
for (int i = 0; i < cols. Length; i++)
{
if (Val. Equals (Cols[i],stringcomparison.ordinalignorecase))
{
result = true;
Jump out of the loop
Break
}
}
return result;
}

In the Update method: you need to determine if

if (this. Isupdateproperty (Model,ps[i]. Name))
{
Sb. Append (Ps[i]. Name+ "=" + ":" +ps[i]. Name+ ",");
List. ADD (Dbhelper.createparam (ps[i). Name, Ps[i]. GetValue (model, NULL)));
}

Update is roughly the same as insert;

Summary: In the stitching of SQL statements need to be very careful, Chinese and English input method, there are SQL statements finally do not add ";"

Oracle+ado.net (b)

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.