Access database accessing component (II)

Source: Internet
Author: User
access| Access | data | Database Access database accessing component (II) Access_table.cs

Using System;

Namespace XLang.VideoOnline.Framework.Database.Access
{
<summary>
Summary description for Access_datatable.
</summary>
public class DataTable:System.Data.DataTable
{
private string _tablename;

private string _primarykey;

public string Name
{
get {return _tablename;}
set {_tablename=value;}
}


public string PrimaryKey
{
get {return _primarykey;}
set {_primarykey=value;}
}


Public DataTable ()
{
}


Public DataTable (String tablename)
{
_tablename=tablename;
}


public string CreateCommand (database.access.datarow[] rows)
{
String temp= "CREATE TABLE" +_tablename+ "(";
for (int i=0;i<rows. GetLength (0); i++)
{
temp+= "[" +rows[i]. Name+ "]" +rows[i]. Type+ (Rows[i]. IsNull? ",": "Not NULL,");
}
temp+= "CONSTRAINT [Index1] PRIMARY KEY ([" +rows[0]. Name+ "])";
_primarykey=rows[0]. Name;
return temp;
}


public string InsertCommand (database.access.datarow[] rows)
{
String temp= "";
temp= "INSERT into" +_tablename+ "(";
for (int i=0;i<rows. GetLength (0); i++)
{
temp+= "[" +rows[i]. Name+ "]" + ",";
}
Temp=temp. Substring (0,temp. LENGTH-1);
temp+= ") VALUES (";
for (int i=0;i<rows. GetLength (0); i++)
{
if (rows[i). Type.equals ("boolean"))
temp+= ((rows[i). Value.tostring (). ToUpper (). Equals ("YES"))? "Yes": "no") + ",";
else if (rows[i). Type.startswith ("string") | | Rows[i]. Type.equals ("Memo") | |
Rows[i]. Type.equals ("text") | | Rows[i]. Type.startswith ("text"))
temp+= "'" +rows[i]. Value+ "',";
else if (rows[i). Type.equals ("date"))
temp+= "#" +rows[i]. Value+ "#,";
Else
Temp+=rows[i]. Value+ ",";
}
Temp=temp. Substring (0,temp. LENGTH-1);
temp+= ")";
return temp;
}


public string DeleteCommand ()
{
Return "DROP TABLE" +_tablename;
}


public string DeleteCommand (string conditions)
{
Return "Deletes * from" +_tablename+ "WHERE" +conditions;
}


public string UpdateCommand (database.access.datarow[] rows,string conditions)
{
String temp= "UPDATE" +_tablename+ "SET";
Temp+=prosessdifferentdatatype (rows);
temp+= "WHERE" +conditions;
return temp;
}


public string SelectCommand ()
{
Return ' select * from ' +_tablename;
}


public string SelectCommand (string conditions)
{
Return "SELECT * from" +_tablename + "WHERE" +conditions;
}


public string SelectCommand (database.access.datarow[] rows,string conditions)
{
String temp= "SELECT";
for (int i=0;i<rows. GetLength (0); i++)
{
temp+= "[" +rows[i]. Name+ "],";
}
Temp=temp. Substring (0,temp. LENGTH-1);
temp+= "from" +_tablename+ "WHERE" +conditions;
return temp;
}


public string Joincommand (database.access.datarow[] rows,string condition,params string [] tablenames)
{
String temp= "SELECT";
for (int i=0;i<rows. GetLength (0); i++)
{
temp+= "[" +rows[i]. Name+ "],";
}
Temp=temp. Substring (0,temp. LENGTH-1);
temp+= "from" +_tablename+ "WHERE" +conditions;
temp+= "from";
foreach (string table in Tablenames)
Temp+=table+ ",";
Temp=temp. Substring (0,temp. LENGTH-1);

if (condition!=null)
temp+= "WHERE" +condition;

return temp;
}


private string Prosessdifferentdatatype (database.access.datarow[] rows)
{
String temp= "";
for (int i=0;i<rows. GetLength (0); i++)
{
if (rows[i). Type.equals ("boolean"))
temp+= "[" +rows[i]. name+ "] =" +
((rows[i). Value.tostring (). ToUpper (). Equals ("YES"))? "Yes": "No")
+",";
else if (rows[i). Type.startswith ("string") | | Rows[i]. Type.equals ("Memo") | |
Rows[i]. Type.equals ("text") | | Rows[i]. Type.startswith ("text"))
temp+= "[" +rows[i]. name+ "] = '" +rows[i]. Value+ "',";
else if (rows[i). Type.equals ("date"))
temp+= "[" +rows[i]. Name+ "] =#" +rows[i]. Value+ "#,";
Else
temp+= "[" +rows[i]. name+ "] =" +rows[i]. Value+ ",";

}
Temp=temp. Substring (0,temp. LENGTH-1);
return temp;
}
}
}




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.