ACCESS数据库访问组件(二)
2024-09-07 19:05:00
供稿:网友
access数据库访问组件(二)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 "delete * 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;
}
}
}