宿舍计费系统105加强版

 

开发环境:vs2008  vc++

数据库:mysql  5.4

做这个系统的需求我们每次宿舍费记录都用一个小本自子,这实在不是一个豆豆的作风。

简单的介绍下功能和遇到的问题。

主界面:

 

主要就是一个list控件显示数据库里面的内容。 数据库连接采用mysql的默认c语言接口,没有使用adoNet技术。

 

 

登陆界面, 简单的数据库比对。

 

 

 

 

 

登陆成功,显示隐藏的操作按钮。

 

 

 

修改数据

 

 

 

自己封装的数据库操作类

CMysqlCon.h

 

if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#pragma comment( lib, "D://CodeLife//vs2008project//Sushe//Sushe//libmysql.lib")

class CMysqlCon  
{
    
public:
    CMysqlCon();
    CString** GetAllData();
    CString * GetColumn();
    void AddNewData(CString  insert_id,CString username,CString insert_money,CString insert_beizhu,CString timenow);
    void UpdateById(CString Updateid,CString Updateusername,CString Updatemoney,CString Updatebeizhu);
    void DeleteById(CString,CString);
    bool HasData();
    bool Querysql(CString  sql);
 
    CMysqlCon(CString);
    virtual ~CMysqlCon();

    MYSQL mysql;
    CString sql;
    MYSQL_ROW row;
    MYSQL_RES * result;
    long colNum;
    long count;

    
};



#endif // !defined(AFX_CONN_H__B0B971E2_A398_4809_88F2_FFD1679B13E4__INCLUDED_)

 

CMysqlCon.cpp

View Code
  1 // Conn.cpp: implementation of the CConn class.
  2 //
  3 //////////////////////////////////////////////////////////////////////
  4 
  5 #include "stdafx.h"
  6 #include "Sushe.h"
  7 #include "Conn.h"
  8 
  9 #ifdef _DEBUG
 10 #undef THIS_FILE
 11 static char THIS_FILE[]=__FILE__;
 12 #define new DEBUG_NEW
 13 #endif
 14 
 15 //////////////////////////////////////////////////////////////////////
 16 // Construction/Destruction
 17 //////////////////////////////////////////////////////////////////////
 18 
 19 //c++参数不确定
 20 //优化无止境的优化
 21 CMysqlCon::CMysqlCon(CString sqll)
 22 {
 23     sql=sqll;
 24     mysql_init(&mysql);
 25     
 26     if( !mysql_real_connect(&mysql,"localhost","root","123456","test",3306,NULL,0))
 27     {
 28         AfxMessageBox("连接数据库失败!");
 29     }
 30 
 31     mysql_set_character_set(&mysql, "gbk"); //设置语言栏支持中文
 32 
 33     if(mysql_real_query(&mysql,sql,sql.GetLength()) )
 34     {
 35         AfxMessageBox(sql);
 36         AfxMessageBox("err query!");
 37         assert(0);
 38     } 
 39 
 40     if(!(result=mysql_store_result(&mysql)))
 41     {
 42         AfxMessageBox("读取数据集失败");
 43         assert(0);
 44     }
 45     else
 46     {
 47         while(row =mysql_fetch_row(result)){;}
 48         count=mysql_num_rows(result);//行数
 49     }
 50 }
 51 
 52 CMysqlCon::~CMysqlCon()
 53 {
 54     
 55     mysql_free_result(result);
 56     mysql_close(&mysql);
 57 }
 58 
 59 
 60 
 61 bool CMysqlCon::HasData()
 62 {
 63     if(count>0)
 64     {
 65         return true;
 66     }
 67     else
 68     {
 69         return false;
 70     }
 71 }
 72 
 73 bool CMysqlCon::Querysql(CString sqll)
 74 {
 75         if( mysql_real_query(&mysql,sql,sql.GetLength()))
 76          {
 77              AfxMessageBox("err query!");
 78              assert(0);
 79              return 0;
 80          } 
 81 
 82         if(!(result=mysql_store_result(&mysql)))
 83         {
 84             AfxMessageBox("读取数据集失败");
 85             assert(0);
 86         }
 87         else
 88         {
 89             while(row =mysql_fetch_row(result)){;}
 90             count=mysql_num_rows(result);    //行数
 91         }
 92          return 1;
 93 }
 94 
 95 void CMysqlCon::DeleteById(CString id,CString biaoname)
 96 {
 97     CString sqll= "delete from" " "+biaoname+ " ""where id ="+id ;
 98     
 99     if( mysql_real_query(&mysql,sqll,sqll.GetLength()) )
100     {
101         AfxMessageBox("err query!");
102         assert(0);
103     } 
104     else
105         AfxMessageBox("删除成功");
106 }
107 
108 void CMysqlCon::UpdateById(CString Updateid,CString Updateusername,CString Updatemoney,CString Updatebeizhu)
109 {
110     
111     
112    CString sqll = "update sushe set username =" "'"+Updateusername+"'" "," "money=" "'"+Updatemoney+"'" "," "beizhu =" "'"+Updatebeizhu+"'" "where id =" "'"+Updateid+"'";
113 if( mysql_real_query(&mysql,sqll,sqll.GetLength()) )
114     {
115         AfxMessageBox("err update query!");
116         assert(0);
117     } 
118  else
119   {
120        AfxMessageBox("更新成功"); 
121   }
122 }
123 
124 void CMysqlCon::AddNewData(CString  insert_id,CString username,CString insert_money,CString insert_beizhu,CString timenow)
125 {
126 
127 
128     //timenow  ="2012-12-9 14:3:9";
129     CString  sqll = "insert into sushe(id,username,money,beizhu,time) values('" +insert_id+"'" "," "'" +username+"'" "," "'"+insert_money+"'" "," "'"+insert_beizhu+"'" "," "'"+timenow+"'" ")";
130 
131 
132     if( mysql_real_query(&mysql,sqll,sqll.GetLength()) )
133     {
134         AfxMessageBox(sqll);
135         assert(0);
136     } 
137 else
138 {
139   AfxMessageBox("插入成功");
140 
141 }
142 }
143 
144 
145 CString** CMysqlCon::GetAllData()
146 {
147     CString **arr;
148     count = mysql_num_rows(result);//行数
149     colNum = mysql_num_fields(result); //列数
150     arr = new CString* [count]; 
151     mysql_data_seek(result,0);
152     
153     for(int ii=0;ii<count;ii++)    
154     {    
155         if(row =mysql_fetch_row(result))
156         {
157             arr[ii]=new CString[colNum];
158 
159             for(int l=0;l<colNum;l++)    
160             {    
161                 if(row[l]==NULL   ||   !strlen(row[l])) 
162                 {
163                 }
164                     //AfxMessageBox("没有记录");   
165                 else    
166                 {
167                     arr[ii][l]=row[l];
168                 }  
169             } 
170         }
171         else
172         {
173             AfxMessageBox("nothing!");
174         }
175     }
176     
177     return arr;
178 }
179 
180 CString * CMysqlCon::GetColumn()
181 {
182     CString* arr;             //指针转换为堆空间 
183     MYSQL_FIELD *field;
184 
185     arr=new CString[colNum];
186     int i=0;
187 
188     while((field = mysql_fetch_field(result)))
189     {
190      arr[i++]=field->name;
191     }
192     return arr;
193 }
194 
195 CMysqlCon::CMysqlCon()
196 {    
197     mysql_init(&mysql);
198     
199     if( !mysql_real_connect(&mysql,"localhost","root","koolma2010","tcims",3306,NULL,0))
200     {
201         AfxMessageBox("连接数据库失败!");
202     }
203     mysql_set_character_set(&mysql, "gbk");
204 }

 

最近的一段时间准备把自己大三到现在写过的一些东西给罗列出来,也算是一种分享吧,本人是个菜鸟,欢迎大家指正。

 

posted @ 2012-12-12 11:41  pipicfan  阅读(419)  评论(0编辑  收藏  举报