自己写的一个通过对象更新数据库的例子

自己很菜,只是想多多交流,希望得到大家的支持

另外想咨询下大家,大家用的绘制UML图的软件除了rose,一般还有什么?

故事是这样的

做ext 的时候,我们老师说json 在ext 最通用,

于是乎,我们后台组和前台组的传递就靠 json

本来是准备用dataset 放数据的

但是有个同学下了个json.net 这个插件 

可以实现 对象转json 的功能

我自己拿dataset 实验,发现不行,

之后我们又搜了个dataset 转json 的东西,

实现了操作,

事后,我们就在想,为什么json.net 本身不提供dataset 与json 互转呢?

这引起了我们的一些想法,是封装的原因? 还是 通过属性的访问器来验证数据?

我们不知道。。。



后来看了下《你必须知道的.NEt》,心里乱了   就做了一个用类更新数据库的东西,准备用json.net 转成对象后做数据库操作。

比如说数据是关于stu 的,我就写个class Stu,

里面的属性包括了数据库本身的所有的字段。。。。(貌似有点画蛇添足?)

代码是这样的
/**/ /*---------------------------------------------
 * 这是为ext和aspx.cs 打交道使用的类,
 * 因为是初学者,所以没什么水平,
 * 希望大家多多指导,
 * 另外非常想知道的一个事情
 * ---------------------------------------------
 * 请问大家做UML图都用什么软件做的?
 * 
 * 
 * 读过《你必须知道的.net》的朋友能看懂对多态的讲解不?
 * ---------------------------------------------
 *
 *
 
*/


using  System;
using  System.Data;
using  System.Configuration;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;
using  System.Data.Sql;
using  System.Data.SqlClient;
using  System.Collections;
using  System.Reflection;
using  System.Text;

/**/ /// <summary>
/// DBcontrol 的摘要说明
/// </summary>



    
// 提供数据操作的相应接口
     public   interface  IDbAction
    
{
        
void update(Queue queue,string tableName);
        
void insert(Queue queue,string tableName);
        
void delete(Queue queue,string tableName);
    }


    
/**/ /// <summary>
    
/// 将数据内容变为sql语句并执行的类
    
/// </summary>

     public   class  DbAction:IDbAction
    
{

        
private string _conStr;
        
private SqlConnection _conn;

        
public DbAction(string conStr)
        
{
            _conStr 
= ConfigurationManager.ConnectionStrings[conStr].ConnectionString;
        }

        
        
public string ConStr
        
{
            
set{_conStr=value;}
            
get return _conStr; }
        }



        
IDbAction 成员#region IDbAction 成员
        

        
/**//// <summary>
        
/// 更新数据库
        
/// </summary>
        
/// <param name="queue">对列</param>
        
/// <param name="tableName">表名</param>

        public void update(Queue queue, string tableName)
        
{
            
string idValue = "";
            StringBuilder sb 
= new StringBuilder();
            sb.Append(
"update ");
            sb.Append(tableName 
+" set ");
            
while (queue.Count > 0)
            
{
                
string str_tmp = queue.Dequeue().ToString();

                
if (str_tmp == "id")
                
{

                     queue.Dequeue();
                    
continue;
                }

                
else
                
{
                    
string temp = "[" + str_tmp + "] = '" + queue.Dequeue() + "'";
                    sb.Append(temp);
                    
if (queue.Count != 0)
                    
{
                        sb.Append(
" , ");
                    }
            
                }

            }

            
//测试时关闭    if(idValue=="0") {throw new Exception("Wrong Id!  Id must nerver be equal to 0");}
            sb.Append(" where id ='" + idValue + "'");
            
string sqlStr = sb.ToString();
            
this.doAction(sqlStr);
            
        }


        
/**//// <summary>
        
/// 插入信息到数据库
        
/// </summary>
        
/// <param name="queue">对列</param>
        
/// <param name="tableName">表名</param>

        public void insert(Queue queue, string tableName)
        
{
            StringBuilder sb 
= new StringBuilder();//在此生成insert 的语句
            sb.Append("insert into ");
            sb.Append(tableName 
+"(");
            Queue quValue 
= new Queue();//value 进行排队
            string idValue="0";
            
while (queue.Count > 0
            
{
                
string str_tmp = queue.Dequeue().ToString();
                
                
if (str_tmp == "id")
                
{
                    idValue 
= queue.Dequeue().ToString() ;
                    
continue;
                    
                }

                sb.Append(
" [" + str_tmp + "");

                quValue.Enqueue(queue.Dequeue());
                
if(queue.Count!=0)
                
{
                    sb.Append(
" , ");
                }
            
            }

            sb.Append(
") Values (");
            
while(quValue.Count>0)
            
{
                sb.Append(
" '"+quValue.Dequeue()+"");
                
if(quValue.Count!=0)
                
{
                    sb.Append(
" , ");
                }

            }


            sb.Append(
")");
            
string sqlStr = sb.ToString();

            
this.doAction(sqlStr);
            
// 要处理下字符串,变成属性名与

        
        }


        
//删除
        public void delete(Queue queue, string tableName)
        
{
            
string idValue = "";
            
while (queue.Count > 0)
            
{
                
string s = queue.Dequeue().ToString();
                
if (s != "id"
                
{
                    queue.Dequeue();
                    
continue;
                }

                
else
                
{
                    idValue 
= queue.Dequeue().ToString();
                    
break;
                }

            }

            
if(idValue=="0")
            
{
                
throw new Exception("Wrong Id!  Id must nerver be equal to 0");
            }

            
string sqlStr = "delete from "+tableName+" where id='"+idValue+"'";
            
this.doAction(sqlStr);
            
        }


        
#endregion


        
private void doAction(string sqlStr)
        
{
            _conn 
= new SqlConnection(_conStr);
            SqlCommand comm 
= new SqlCommand(sqlStr, _conn);
            _conn.Open();
            comm.ExecuteNonQuery();

        }

    }


    
// 实例打包。。
     public   class  ClassBox
    
{
        
/**//// <summary>
        
/// 将实例的属性压缩到一个queue 中
        
/// </summary>
        
/// <param name="theClass">这个实例</param>
        
/// <returns></returns>

        public Queue doBoxing(object theClass)
        
{
             Queue queue
= new Queue();
            Type P 
= theClass.GetType();
            
foreach (PropertyInfo pi in P.GetProperties())
            
{
                queue.Enqueue(pi.Name.ToLower());
                queue.Enqueue(pi.GetValue(theClass, 
null));
            }

            
return queue;
        }

    }


// 此为实例的类实现的接口
     public   interface  IDbClass
    
{
        
int Id
        
{
            
get;
            
set;
        }

        
    }




/**/ /*----------------------------------------------------------
 * add.aspx.cs
----------------------------------------------------------
*/

using  System;
using  System.Data;
using  System.Configuration;
using  System.Collections;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;

public   partial   class  add : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
       

        Person person 
= new Person();

        person.Name 
= Request.Form["name"].ToString();
        person.Age 
= int.Parse(Request.Form["age"].ToString());
        person.Gender 
= Request.Form["gender"].ToString();
        person.Tel 
= Request.Form["tel"].ToString();
        person.Birthday 
= Request.Form["birthday"].ToString();
        person.Homepage 
= Request.Form["homepage"].ToString();

        ClassBox tempBox 
= new ClassBox();
        Queue queue
= tempBox.doBoxing(person);
        DbAction dbAction 
= new DbAction("test1ConnectionString");
        dbAction.insert(queue, 
"person");
        Response.Clear();
        Response.Write(
"{success: true}");

    }

}


数据类的描述 #region 数据类的描述
/**//// <summary>
/// 数据类
/// </summary>

public class Person
{
    
private int _id;
    
public int Id
    
{
        
get
        
{
            
return _id;
        }

        
set
        
{
            _id 
= value;
        }

    }

    
private int _age;
    
public int Age
    
{
        
get return _age; }
        
set
        
{
              _age 
= value;
            
        }

    }

    
private string _name;
    
public string Name
    
{
        
set
        
{
            _name 
= value;
        }

        
get
        
{
            
return _name;
        }

    }

    
private string _gender;
    
public string Gender
    
{
        
get
        
{
            
return _gender;
        }

        
set
        
{
            
            _gender 
= value;
            
        }

    }

    
private string _tel;
    
public string Tel
    
{
        
get return _tel; }
        
set { _tel = value; }
    }

    
private string _birthday;
    
public string Birthday
    
{
        
get return _birthday; }
        
set
        
{
            _birthday 
= value;
        }

    }

    
private string _homepage;
    
public string Homepage
    
{
        
get return _homepage; }
        
set { _homepage = value; }
    }


}

#endregion


转载于:https://www.cnblogs.com/jicheng1014/archive/2008/05/17/1201600.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值