数据库配置假设如下:
<connectionStrings>
<add name="Conn" connectionString="Server=.;database=CQSpace;uid=sa;pwd=123456"/>
<add name="CYQ" connectionString="Server=.;database=CQSpace;uid=sa;pwd=123456"/>
</connectionStrings>
<add name="Conn" connectionString="Server=.;database=CQSpace;uid=sa;pwd=123456"/>
<add name="CYQ" connectionString="Server=.;database=CQSpace;uid=sa;pwd=123456"/>
</connectionStrings>
操作类的实例方法:
方法一:默认取配置文件Conn的链接字符串
MAction action = new MAction(TableNames.CQ_BlogSet);
方法二:取自定义名称如CYQ的链接字符串:
MAction action = new MAction(TableNames.CQ_BlogSet,"CYQ");
方法三:直取链接字符串
MAction action = new MAction(TableNames.CQ_BlogSet,"Server=.;database=CQSpace;uid=sa;pwd=123456");
接下来看看都有什么操作方法更新:
1:单条数据查询方法:
方式一:直接传ID
MAction action = new MAction(TableNames.CQ_BlogSet);
if (action.Fill(888))
{
action.SetTo(lblAlbumType);
action.Close();
}
if (action.Fill(888))
{
action.SetTo(lblAlbumType);
action.Close();
}
方式二:传where条件
MAction action = new MAction(TableNames.CQ_BlogSet);
if (action.Fill("Title='xxxx'"))
{
action.SetTo(lblAlbumType);
action.Close();
}
if (action.Fill("Title='xxxx'"))
{
action.SetTo(lblAlbumType);
action.Close();
}
2:数据删除:
方式一:先填充再删除
if (action.Fill(888))
{
action.Delete();
action.Close();
}
{
action.Delete();
action.Close();
}
方式二:根据ID删除
MAction action = new MAction(TableNames.CQ_Album);
action.Delete(888);
action.Close();
action.Delete(888);
action.Close();
方式三:根据条件删除
MAction action = new MAction(TableNames.CQ_Album);
action.Delete("id>888");
action.Close();
action.Delete("id>888");
action.Close();
3:数据更新
方式一:先填充再更新
MAction action = new MAction(TableNames.CQ_Album);
if (action.Fill(111))
{
action.GetFrom(txtAlbumName);
action.Update();
action.Close();
}
if (action.Fill(111))
{
action.GetFrom(txtAlbumName);
action.Update();
action.Close();
}
方式二:根据ID更新
MAction action = new MAction(TableNames.CQ_Album);
action.GetFrom(txtAlbumName);
action.Update(111);
action.Close();
action.GetFrom(txtAlbumName);
action.Update(111);
action.Close();
方式三:根据条件更新
MAction action = new MAction(TableNames.CQ_Album);
action.GetFrom(txtAlbumName);
action.Update("id=299 or name='路过秋天'");
action.Close();
action.GetFrom(txtAlbumName);
action.Update("id=299 or name='路过秋天'");
action.Close();
4:数据列表查询和多表联合查询没变化,和上上节一致。
5:新增加计数查询:
MAction action = new MAction(TableNames.CQ_BlogSet);
lblAlbumType.Text = action.GetCount("id>2 and Name='路过秋天'").ToString();
action.Close();
lblAlbumType.Text = action.GetCount("id>2 and Name='路过秋天'").ToString();
action.Close();
6:为了进一步隐藏索引取值与赋值而产生的Get与Set方法:
在上节的升级中,新增加的GetFrom与SetTo只能对控件赋值进行操作,如果我们只是需要取值,或设置值,中间没有控件呢?
在上节之前,我们只能又回到索引的方式取值了如:
string albumType=action.Data[CQ_Album.AlbumName].Value.ToString();
狂热升级之后:用上新加的Get方法取值,效果就变成了:
string albumType = action.Get<string>(CQ_Album.AlbumName);
而Set方法赋值则为:
action.Set(CQ_Album.AlbumName, "路过秋天";);
于是,对于取值与赋值,我们目前有3种方法可用了:
1:对控件型的:GetFrom与SetTo
2:对变量型的:Get与Set
3:保留的:索引数据/枚举与字段名(字符串)取值
2:对变量型的:Get与Set
3:保留的:索引数据/枚举与字段名(字符串)取值