个人的web开发心得(五)----------非常适合入门新手,都是常识


41.很好的排序sql语句
要返回查询结果的第三页(页大小是10),

SELECT TOP 10 * FROM
(SELECT TOP 30 * FROM Customers ORDER BY Id ASC) AS Table1
ORDER BY Id DESC

42.先按 A 排序 再按 B排序

所以如果要安A、B降序排列则可以这样写:
select top 5 *
from table
order by A asc,B desc

43.去除重复项
_sql = "select  DISTINCT zrbm from cgskpi_bmfj"


44.在使用 gridview 时,用到每行的id时将 id字段显示出来 用完后关闭显示.
GridView1.Columns[0].Visible = true;


45.
datagrid 绑定 数据后, 根据绑定的数据修改 显示数据.
        //绑定多个协作部门


        for (int i = 0; i < DataGrid1.Items.Count; i++)
        {
            string string_temp = DataGrid1.Items[i].Cells[6].Text.Trim();

            if (string_temp != "")
            {
                string[] Deps = string_temp.Split(',');

                bool bool_temp = false;     //去掉 多余的逗号

                for (int int_temp = 0; int_temp < Deps.Length; int_temp++)
                {
                    if (bool_temp == false)
                    {
                        DataGrid1.Items[i].Cells[6].Text = oCon.GetDepartmentNameByDepId(Deps[int_temp]);
                        bool_temp = true;
                    }
                    else
                    {
                        DataGrid1.Items[i].Cells[6].Text += "," + oCon.GetDepartmentNameByDepId(Deps[int_temp]);
                    }
                }

            }
        }

gridview 修改数据这样找

 for(int i=0;i<GridView1.Rows.Count;i++)
        {
            CheckBox cb=(CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1");
            if (cb.Checked)
            {
                string PlanID = GridView1.Rows[i].Cells[1].Text;
                string Dept = dept;
                string Date = year;
                string PlanName = GridView1.Rows[i].Cells[2].Text;
                string Object = GridView1.Rows[i].Cells[4].Text;
                string ObjectAssess = GridView1.Rows[i].Cells[5].Text;
                string HopeObject = GridView1.Rows[i].Cells[6].Text;
                string HopeObjectAssess = GridView1.Rows[i].Cells[7].Text;

                string _sql = "select count(*) from year_deptplanorder where planid='" + PlanID + "'";
                int _count = Convert.ToInt32(DBC.ExecuScalar(_sql));

                if (_count == 0)
                {

                }
            }
        }


        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            _sql = "select name from gb_vpart where workerid='" + GridView1.Rows[i].Cells[2].Text + "'";
            GridView1.Rows[i].Cells[2].Text = DBC.ExecuScalar(_sql);
        }


45.小的SQL语句 都是写在数据连接层 同 update  insert 写在一起

46.抛出一段异常
 if(textContent=="")
   throw new Exception("留言内容为空");

47.DataReader使用方法
using(SqlDataReader dr=SqlHelper.ExecuteReader(cmdText, CommandType.StoredProcedure, parameters.ToArray()))
   {
    if(dr.Read()) 
     mail=(Mail)SqlHelper.PutObjectProperty(new Mail(), dr);
   }
48.读取web.config文件的数据
 <connectionStrings>
    <add name="PTMMSConnectionString" connectionString="Data Source=10.158.21.49;Initial Catalog=PTMMS;Persist Security Info=True;User ID=sa;Password=lnhr" providerName="System.Data.SqlClient" />
  </connectionStrings>

首先要先在bin文件夹中 添加System.Configuration.dll文件(重要)

然后 using System.Configuration;

        protected static string connectionString = ConfigurationManager.ConnectionStrings["PTMMSConnectionString"].ConnectionString;
读取 连接字符串.

49.绝对不能乱用Static

50.获得当前在线人数

 Application["user_sessions"].ToString()

在Global.asax文件中添加
    void Application_Start(object sender, EventArgs e)
    {
        // 在应用程序启动时运行的代码
        Application["user_sessions"] = 0;
    }
    void Session_Start(object sender, EventArgs e)
    {
        // 在新会话启动时运行的代码
        Application.Lock();
        Application["user_sessions"] = (int)Application["user_sessions"] + 1;
        Application.UnLock();
    }

    protected void Session_End(object sender, EventArgs e)
    {
        // 在会话结束时运行的代码。
        // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
        // InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer
        // 或 SQLServer,则不会引发该事件。
 // 或者设置Session的有效时间 ,超过有效时间则自动执行该代码
 //    <sessionState timeout="120">
 //    </sessionState>
        Application.Lock();
        Application["user_sessions"] = (int)Application["user_sessions"] - 1;
        Application.UnLock();
    }

posted on 2006-09-22 14:07  天上  阅读(746)  评论(1编辑  收藏  举报