您的位置:首页 > 数据库

我在HIT第一次.net实验中用到的sql语句

2012-03-18 23:02 232 查看
这次实验主要是开发基于.net framework的窗体应用程序。我感觉这次实验让我学到了挺多东西。我做的是一个加州招待所管理系统,就是瞎扯淡。。囧。。。

第一就是尽量减少模块(不知道写了个这么水的实验能不能称得上是模块,暂时先这么叫着吧,显得牛B一点)之间的耦合性,这个特别重要。之前总是听一些什么减少模块之间的耦合的观点,没有意识到有什么用,以为是用来装B的,但是这次让我深深地意识到了这个advice的重要性。我对这个advice的理解就是各部分分工要非常明确,负责processing data的专门processing data,负责操作UI的专门来操作UI,诸如此类。这个在android的开发中也有所体现,即intent的机制。

此外感觉总是对数据库的增删改查没啥意思,于是就想通过将sql语句设计的复杂一点来帮助分担一些程序逻辑的复杂程度。

第一在这个实验中完全是用来装B的,就是数据库的分页查询与分页显示,这个功能在一些实际的应用中比较有用,例如当数据量很大时,不可能一次都读出来。

"select top " + NUMBER_IN_A_PAGE + " * from " + DBKEYS.ROOM_INFO_TABLE_NAME
                + " where RoomNumber not in (select top " + page * NUMBER_IN_A_PAGE + " RoomNumber from " + DBKEYS.ROOM_INFO_TABLE_NAME + ")";
其中NUMBER_IN_A_PAGE是每页的数量,对应的C#代码如下:

这个函数是查询数据库,返回一个DataSet供填充DataGridView使用:

/// <summary>
        /// 根据给定的sql字符串查询数据,返回结果DataSet,flag为区分函数重载标志
        /// </summary>
        /// <param name="sqlString"></param>
        /// <param name="flag"></param>
        /// <returns></returns>
        public DataSet inquiry(String sqlString,Boolean flag)
        {
            SqlCommand command;
            SqlDataAdapter adapter;
            DataSet dataSet;
            try
            {
                command = new SqlCommand(sqlString, connection);
                adapter = new SqlDataAdapter(command);
                dataSet = new DataSet();
                adapter.Fill(dataSet);
                return dataSet;
            }
            catch (SqlException e)
            {
                Console.WriteLine(e.Message + flag);
                return null;
            }
        }
        /// <summary>
/// 设置setDataGridView的显示
/// </summary>
private Boolean setDataGridView(int page)
{
String sqlString = "select top " + NUMBER_IN_A_PAGE + " * from " + DBKEYS.ROOM_INFO_TABLE_NAME + " where RoomNumber not in (select top " + page * NUMBER_IN_A_PAGE + " RoomNumber from " + DBKEYS.ROOM_INFO_TABLE_NAME + ")";
DataSet dataSet = dao.inquiry(sqlString, true);
// 说明没有查到结果,到达了页首或者页尾
if (dataSet == null || dataSet.Tables.Count == 0 || dataSet.Tables[0].Rows.Count <= 0)
{
return false;
}
DataTable tableFirst = dataSet.Tables[0]; // 用DataTable 取 DataSet中的第一个表
// 设置DataGridView的列名
foreach (DataColumn dc in tableFirst.Columns)
{
RoomInfoDataGridView.Columns.Add(dc.ColumnName, dc.ColumnName);
}
// 每次新建一行将数据填到DataGridView中
foreach (DataRow dr in tableFirst.Rows)
{
DataGridViewRow vr = new DataGridViewRow();
foreach (DataGridViewColumn dc in RoomInfoDataGridView.Columns)
{
vr.Cells.Add(dc.CellTemplate.Clone() as DataGridViewCell);
vr.Cells[vr.Cells.Count - 1].Value = dr[dc.Name];
}
RoomInfoDataGridView.Rows.Add(vr);
}
// 设置RoomInfoDataGridView禁止根据列排序
int i;
for (i = 0; i < this.RoomInfoDataGridView.Columns.Count; i++) { }
this.RoomInfoDataGridView.Columns[--i].SortMode = DataGridViewColumnSortMode.NotSortable;
return true;
}


还有一个sql语句是日期的比较,简单说来就是给定个date,选出date在ExpectiveLiveDate和从ExpectiveLiveDate算起ExpextiveLastingTime天后之间的记录。

"select * from VisitorBookInfo where ( '" + date + "'"
                +" between ExpectiveLiveDate and dateadd(day,ExpextiveLastingTime,ExpectiveLiveDate))";


对应的C#函数:

        /// <summary>
/// 根据查询日期填充表格
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
private Boolean setDataGridView(String date)
{
String sqlString = "select * from VisitorBookInfo where ( '" + date + "'" +" between ExpectiveLiveDate and dateadd(day,ExpextiveLastingTime,ExpectiveLiveDate))";
DataSet dataSet = dao.inquiry(sqlString, true);
// 说明没有查到结果
if (dataSet == null || dataSet.Tables.Count == 0 || dataSet.Tables[0].Rows.Count <= 0)
{
return false;
}
DataTable tableFirst = dataSet.Tables[0]; // 用DataTable 取 DataSet中的第一个表
// 设置DataGridView的列名
foreach (DataColumn dc in tableFirst.Columns)
{
RoomInfoDataGridView.Columns.Add(dc.ColumnName, dc.ColumnName);
}
// 每次新建一行将数据填到DataGridView中
foreach (DataRow dr in tableFirst.Rows)
{
DataGridViewRow vr = new DataGridViewRow();
foreach (DataGridViewColumn dc in RoomInfoDataGridView.Columns)
{
vr.Cells.Add(dc.CellTemplate.Clone() as DataGridViewCell);
vr.Cells[vr.Cells.Count - 1].Value = dr[dc.Name];
}
RoomInfoDataGridView.Rows.Add(vr);
}
// 设置RoomInfoDataGridView禁止根据列排序
int i;
for (i = 0; i < this.RoomInfoDataGridView.Columns.Count; i++) { }
this.RoomInfoDataGridView.Columns[--i].SortMode = DataGridViewColumnSortMode.NotSortable;
return true;
}


这个sql语句是选出某一层的房间,就是比如我要选出7楼的房间,也就是选出所有房间号以7开头的,比如702,711等:

"select * from RoomInfo where(ASCII(RoomNumber) = ASCII('" + level +"'))"


对应的C#函数:

/// <summary>
        /// 根据楼层填充表格,flag为重载函数标志
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        private Boolean setDataGridView(String level,Boolean flag)
        {
            String sqlString = "select * from RoomInfo where(ASCII(RoomNumber) = ASCII('" + level +"'))";
            DataSet dataSet = dao.inquiry(sqlString, true);
            // 说明没有查到结果
            if (dataSet == null || dataSet.Tables.Count == 0 || dataSet.Tables[0].Rows.Count <= 0)
            {
                return false;
            }
            DataTable tableFirst = dataSet.Tables[0]; // 用DataTable 取 DataSet中的第一个表
            // 设置DataGridView的列名
            foreach (DataColumn dc in tableFirst.Columns)
            {
                RoomInfoDataGridView.Columns.Add(dc.ColumnName, dc.ColumnName);
            }
            // 每次新建一行将数据填到DataGridView中
            foreach (DataRow dr in tableFirst.Rows)
            {
                DataGridViewRow vr = new DataGridViewRow();
                foreach (DataGridViewColumn dc in RoomInfoDataGridView.Columns)
                {
                    vr.Cells.Add(dc.CellTemplate.Clone() as DataGridViewCell);
                    vr.Cells[vr.Cells.Count - 1].Value = dr[dc.Name];
                }
                RoomInfoDataGridView.Rows.Add(vr);
            }
            // 设置RoomInfoDataGridView禁止根据列排序
            int i;
            for (i = 0; i < this.RoomInfoDataGridView.Columns.Count; i++) { }
            this.RoomInfoDataGridView.Columns[--i].SortMode = DataGridViewColumnSortMode.NotSortable;
            return true;
        }


这个是比较牛B的一条语句了,同时查询两个表,实现的功能就是我想预定房间,给你一个预定的日期和我要预定的天数,你给我找出现在为空的并且没有在装修的,而且那天没有预定记录的房间(貌似逻辑有点错误,我要预定那天的,你为啥限制我现在必须也得为空和现在不能在装修?!其实删掉那两个等于0的查询限制就好,不过不影响这条语句的牛B程度):

select * from RoomInfo where (RoomInfo.RoomNumber not in ((select VisitorBookInfo.RoomNumber from VisitorBookInfo where ('" + dateBegin + "' between ExpectiveLiveDate and dateadd(day,ExpextiveLastingTime,ExpectiveLiveDate)) and (ExpectiveLiveDate between '" + dateBegin + "' and '" + dateEnd + "')or ('" + dateBegin + "'=ExpectiveLiveDate)or('" + dateEnd + "'=dateadd(day,ExpextiveLastingTime,ExpectiveLiveDate)))))"


对应的C#函数:

/// <summary>
        /// 根据开始日期和结束日期查询在该日期内房间的预定情况
        /// </summary>
        /// <param name="dateBegin"></param>
        /// <param name="dateEnd"></param>
        /// <returns></returns>
        private Boolean setDataGridView(String dateBegin,String dateEnd)
        {
            //String sqlString = "select * from RoomInfo where (RoomInfo.RoomNumber not in ((select VisitorBookInfo.RoomNumber from VisitorBookInfo where not ('" + dateBegin + "' not between ExpectiveLiveDate and dateadd(day,ExpextiveLastingTime,ExpectiveLiveDate)) and (ExpectiveLiveDate not between '" + dateBegin + "' and '" + dateEnd + "'))))";
            String sqlString = "select * from RoomInfo where (RoomInfo.RoomNumber not in ((select VisitorBookInfo.RoomNumber from VisitorBookInfo where ('" + dateBegin + "' between ExpectiveLiveDate and dateadd(day,ExpextiveLastingTime,ExpectiveLiveDate)) and (ExpectiveLiveDate between '" + dateBegin + "' and '" + dateEnd + "')or ('" + dateBegin + "'=ExpectiveLiveDate)or('" + dateEnd + "'=dateadd(day,ExpextiveLastingTime,ExpectiveLiveDate)))))";
            DataSet dataSet = dao.inquiry(sqlString, true);
            // 说明没有查到结果
            if (dataSet == null || dataSet.Tables.Count == 0 || dataSet.Tables[0].Rows.Count <= 0)
            {
                return false;
            }
            DataTable tableFirst = dataSet.Tables[0]; // 用DataTable 取 DataSet中的第一个表
            // 设置DataGridView的列名
            foreach (DataColumn dc in tableFirst.Columns)
            {
                RoomInfoDataGridView.Columns.Add(dc.ColumnName, dc.ColumnName);
            }
            // 每次新建一行将数据填到DataGridView中
            foreach (DataRow dr in tableFirst.Rows)
            {
                DataGridViewRow vr = new DataGridViewRow();
                foreach (DataGridViewColumn dc in RoomInfoDataGridView.Columns)
                {
                    vr.Cells.Add(dc.CellTemplate.Clone() as DataGridViewCell);
                    vr.Cells[vr.Cells.Count - 1].Value = dr[dc.Name];
                }
                RoomInfoDataGridView.Rows.Add(vr);
            }
            // 设置RoomInfoDataGridView禁止根据列排序
            int i;
            for (i = 0; i < this.RoomInfoDataGridView.Columns.Count; i++) { }
            this.RoomInfoDataGridView.Columns[--i].SortMode = DataGridViewColumnSortMode.NotSortable;
            return true;
        }


此外,记住不能用关键字(如“user”)作为表和表中列的名字,否则你会很麻烦。

下面是我的界面(功能略显单薄):







其实实验做到什么成度不重要,重要的是能提高自己的思考能力和代码水平,从中有所收获。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: