月度归档:1999年11月

.net mvc html form beginform beginrouteform

FormExtensions类
该类定了3种类型的扩展方法,它们分别是BeginForm,BeginRouteForm,EndForm
BeginForm共有13种重载方法,这里参数不一一介绍。
BeginRouteForm共有12种重载方法,主要表现定义表单的开始部分,其中是以路由的方式设置action的值
EndForm 主要表现在表单的结尾,生成</form>
如下表单使用的几种方式:
方式1:

<%=Html.BeginForm("Login", "Home", FormMethod.Post, new { id="name"})%>
姓名<%=Html.TextBox("name", null, new { id="name",width="200px"})%><br />
密码<%=Html.Password("pass", null, new { id = "pass", width = "200px" })%><br />
<input type="submit" id="btnSubmit" value="Submit" />
<%Html.EndForm(); %>

这里注意<%=Html.BeginForm() %><%Html.EndForm();%>后者有 " ; "
Login:是指Action,Home是指Conroller,FormMethod.Post是指用Post方式来提交表单
new{id="name"} 是指表单元素属性。<form id="name" action="Home/Login" method="post"></form>

方式2:

<fieldset>
<%=Html.BeginRouteForm("Start", new { controller = "Home", action = "Login" }, FormMethod.Post)%>
姓名<%=Html.TextBox("name", null, new { id="name",width="200px"})%><br />
密码<%=Html.Password("pass", null, new { id = "pass", width = "200px" })%><br />
<input type="submit" id="Submit1" value="Submit" />
<%Html.EndForm(); %>
</fieldset>

这种方式的表单是以路由的方式设置action 的,"Start" 是路由的名称:

routes.MapRoute(
"Start",
"{controller}/{action}",
new { controller="Home",action="Index"}
);

方式3:

<fieldset>
<%using (Html.BeginForm("Login", "Home", FormMethod.Post, new { id = "name" }))
{
%>
姓名<%=Html.TextBox("name", null, new { id="name",width="200px"})%><br />
密码<%=Html.Password("pass", null, new { id = "pass", width = "200px" })%><br />
<input type="submit" id="btnSubmit" value="Submit" />
<%
} %>
</fieldset>

这种方式不需要<%Html.EndForm();%> 其余的方式基本相同

方式4:
就是普通的html代码

<form id="name" method="post" action="Home/Login">
</form>

这里不做介绍

C# 合并两个 list

using System;
using System.Collections.Generic;
class Program
{
  static void Main()
  {
    List a = new List();
    a.Add(1);
    a.Add(2);
    a.Add(5);
    a.Add(6);
    // Contains:
    // 1
    // 2
    // 5
    // 6
    int[] b = new int[3];
    b[0] = 7;
    b[1] = 6;
    b[2] = 7;
    a.AddRange(b);
    // Contains:
    // 1
    // 2
    // 5
    // 6
    // 7 [added]
    // 6 [added]
    // 7 [added]
    foreach (int i in a)
    {
      Console.WriteLine(i);
    }
  }
}
=== Output of the program ===
1
2
5
6
7
6
7

NHibernate ICriteria多表一对多关联查询

配置文件

Customer.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Entity.CustomerEntity, Entity" table="Customer" lazy="false" >
    <id name="CustomerID" column="CustomerID" type="Int32">
      <generator class="identity" />
    </id>
    <property name="CustomerName" column="CustomerName" type="String" length="10" />
    <bag name="Files" table="File" cascade="all">
      <key column="FileID" foreign-key="FileID"></key>
      <one-to-many class="Entity.FileEntity, Entity"/>
    </bag>
  </class>
</hibernate-mapping>

File.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Entity.FileEntity, Entity" table="File1"  lazy="false">
    <id name="FileID" column="FileID" type="Int32">
      <generator class="identity" />
    </id>
    <property name="FileSize" column="FileSize" type="Int32" length="4" />
    <property name="CustomerID" column="CustomerID" type="Int32" length="4" />
    <many-to-one name="Customer" column="CustomerID" class="Entity.CustomerEntity, Entity" insert="false"/>
    <bag name="DownloadLogs" table="DownloadLog" cascade="all">
      <key column="FileID"/>
      <one-to-many class="Entity.DownloadLogEntity, Entity" />
    </bag>
  </class>
</hibernate-mapping>

DownloadLog.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Entity.DownloadLogEntity, Entity" table="DownloadLog" lazy="false" >
    <id name="DownloadLogID" column="DownloadLogID" type="Int32">
      <generator class="identity" />
    </id>
    <property name="FileID" column="FileID" type="Int32" length="4" />
    <property name="Times" column="Times" type="Int32" length="4" />
    <many-to-one name="File" column="FileID" class="Entity.FileEntity, Entity" insert="false"/>
  </class>
</hibernate-mapping>

从配置文件上可以看出
每个customer对应多个file,每个file对应多个downloadlog
如果使用icriteria查询customer对应的downloadlog
可以这样写:

public IList<DownloadLogEntity> GetByCustomerID(int customerID)
{
  ICriteria criteria = NHibernateSession.CreateCriteria(typeof(DownloadLogEntity));
  criteria.CreateAlias("File", "file");
  criteria.CreateAlias("file.Customer", "customer");
  criteria.Add(Expression.Eq("customer.CustomerID",customerID));
  return criteria.List<DownloadLogEntity>();
}

Nhibernate ICriteria sum字段求和

ICriteria criteria = NHibernateSession.CreateCriteria(typeof(DownloadLogEntity));
criteria.CreateAlias("Enduser", "enduser");
criteria.Add(Expression.Eq("enduser.EnduserID",enduserID));
criteria.Add(Expression.Between("DownloadTime", day, day.AddDays(1)));
criteria.SetProjection(Projections.Sum("FileSize"));
return (int)criteria.UniqueResult();

sql join inner join on, left join on, right join on讲解

1.理论

    只要两个表的公共字段有匹配值,就将这两个表中的记录组合起来。

    个人理解:以一个共同的字段求两个表中符合要求的交集,并将每个表符合要求的记录以共同的字段为牵引合并起来。


语法

FROM table1 INNER JOIN table2 ON table1 . field1 compopr table2 . field2

INNER JOIN 操作包含以下部分:

部分

说明

table1, table2

要组合其中的记录的表的名称。

field1field2

要联接的字段的名称。如果它们不是数字,则这些字段的数据类型必须相同,并且包含同类数据,但是,它们不必具有相同的名称。

compopr

任何关系比较运算符:“=”“<”“>”“<=”“>=”或者“<>”


说明

    可以在任何 FROM 子句中使用 INNER JOIN 操作。这是最常用的联接类型。只要两个表的公共字段上存在相匹配的值,Inner 联接就会组合这些表中的记录。

    可以将 INNER JOIN 用于 Departments Employees 表,以选择出每个部门的所有雇员。而要选择所有部分(即使某些部门中并没有被分配雇员)或者所有雇员(即使某些雇员没有分配到任何部门),则可以通过 LEFT JOIN 或者 RIGHT JOIN 操作来创建外部联接如果试图联接包含备注OLE 对象数据的字段,将发生错误。

    可以联接任何两个相似类型的数字字段。例如,可以联接自动编号长整型字段,因为它们均是相似类型。然而,不能联接单精度型双精度型类型字段。

    下例展示了如何通过 CategoryID 字段联接 Categories Products 表:

SELECT CategoryName, ProductName

FROM Categories INNER JOIN Products

ON Categories.CategoryID = Products.CategoryID;


在前面的示例中,CategoryID 是被联接字段,但是它不包含在查询输出中,因为它不包含在 SELECT 语句中。若要包含被联接字段,请在 SELECT 语句中包含该字段名,在本例中是指 Categories.CategoryID


也可以在 JOIN 语句中链接多个 ON 子句,请使用如下语法:

SELECT fields
FROM table1 INNER JOIN table2
ON table1.field1 compopr table2.field1 AND
ON table1.field2 compopr table2.field2) OR
ON table1.field3 compopr table2.field3)];


也可以通过如下语法嵌套 JOIN 语句:

SELECT fields
FROM table1 INNER JOIN
(table2 INNER JOIN [( ]table3
[INNER JOIN [( ]tablex [INNER JOIN …)]
ON table3.field3 compopr tablex.fieldx)]
ON table2.field2 compopr table3.field3)
ON table1.field1 compopr table2.field2;

LEFT JOIN RIGHT JOIN 可以嵌套在 INNER JOIN 之中,但是 INNER JOIN 不能嵌套于 LEFT JOIN RIGHT JOIN 之中。


2.
操作实例


A记录如下:

aID               aNum
1                  a20050111
2                  a20050112
3                  a20050113
4                  a20050114
5                  a20050115

B记录如下:
bID               bName

1                   2006032401
2                  2006032402
3                  2006032403
4                  2006032404
8                  2006032408

实验如下:
1.left join

sql语句如下:
select * from A
left join B
on A.aID = B.bID

结果如下:
aID               aNum                          bID                  bName
1                   a20050111                1                      2006032401
2                   a20050112                2                     2006032402
3                   a20050113                3                     2006032403
4                   a20050114                4                     2006032404
5                   a20050115                NULL              NULL
(所影响的行数为 5 行)

结果说明:
left join
是以A表的记录为基础的,A可以看成左表,B可以看成右表,left join是以左表为准的换句话说,左表(A)的记录将会全部表示出来,而右表(B)只会显示符合搜索条件的记录(例子中为: A.aID = B.bID)。B表记录不足的地方均为NULL.

2.right join
sql语句如下:
select * from A
right join B
on A.aID = B.bID
结果如下:
aID               aNum                          bID                  bName
1                   a20050111                1                      2006032401
2                   a20050112                2                     2006032402
3                   a20050113                3                     2006032403
4                   a20050114                4                     2006032404
NULL           NULL                          8                     2006032408
(所影响的行数为 5 行)


结果说明
:
仔细观察一下,就会发现,left join的结果刚好相反,这次是以右表(B)为基础的,A表不足的地方用NULL填充.


3.inner join
sql语句如下:
select * from A
innerjoin B
on A.aID = B.bID

结果如下:
aID               aNum                          bID                  bName
1                   a20050111                1                      2006032401
2                   a20050112                2                     2006032402
3                   a20050113                3                     2006032403
4                   a20050114                4                     2006032404

结果说明:
很明显,这里只显示出了 A.aID = B.bID的记录.这说明inner join并不以谁为基础,它只显示符合条件的记录.

group by time span

SELECT count(*),
DateAdd(second, -DatePart(second, clientTime) ,
DateAdd(ms, -DatePart(ms, clientTime), clientTime))
FROM dbo.V_COMBINED
WHERE (sessionId = '122b')
AND (type = N'sys_goodaction')
AND (paraName = 'value')
GROUP BY DateAdd(second, -DatePart(second, clientTime) % 5
,DateAdd(ms, -DatePart(ms, clientTime), clientTime))

group by time span 一个时间段

SELECT sum(sizeamount) as counts
,[ShopID]
,dateadd(hh, - datepart(hour,downloadtime) % 24 ,downloadtime)
FROM [abc].[dbo].[ShopDownloadAmount]
group by shopid, dateadd(hh, - datepart(hour,downloadtime) % 24 ,downloadtime)
order by dateadd(hh, - datepart(hour,downloadtime) % 24 ,downloadtime)

结果如下
36730    2    2009-12-15 00:00:00.000
72342    2    2009-12-16 00:00:00.000
76858    2    2009-12-17 00:00:00.000
34094    2    2009-12-18 00:00:00.000
58178    2    2009-12-19 00:00:00.000