Access database accessing component (II)
Last Update:2017-02-28
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;
}
}
}