小技巧汇总

获取勾选的复选框:

$("input[name='show_product_line']:checked").each(function() {
  console.log($(this).val());
});

获取未勾选的复选框:

$("input[name='id']:not(:checked)").each(function() {
  console.log($(this).val());
});

根据checkbox选中来取对应行中的控件值

$(".mycheck:checked").each(function ()
{
var obj = new Object();

obj.id = $(this).parent().parent().find(".pkid").val().toString().Int();
obj.userId = $(this).parent().parent().find(".userId").val().toString();
obj.socialInsuranceMoney = $(this).parent().parent().find(".socialInsuranceMoney").val().toString().Float();
obj.pacMoney = $(this).parent().parent().find(".pacMoney").val().toString().Float();
obj.incomeTax = $(this).parent().parent().find(".incomeTax").val().toString().Float();

arrList.push(obj);
});

 

 

JavaScript中的对象序列化

JSON.stringify()和JSON.parse()用来序列化和还原JavaScript对象

 

1、ArrayList与数组转换

ArrayList arrListSQL = new ArrayList();

string[] sqls = (string[])arrListSQL.ToArray(typeof(string));

 

 

每一次操作select的时候,总是要出来翻一下资料,不如自己总结一下,以后就翻这里了。

比如<select class="selector"></select>

1、设置value为pxx的项选中

     $(".selector").val("pxx");

2、设置text为pxx的项选中

    $(".selector").find("option[text='pxx']").attr("selected",true);

    这里有一个中括号的用法,中括号里的等号的前面是属性名称,不用加引号。很多时候,中括号的运用可以使得逻辑变得很简单。

3、获取当前选中项的value

    $(".selector").val();

4、获取当前选中项的text

    $(".selector").find("option:selected").text();

    这里用到了冒号,掌握它的用法并举一反三也会让代码变得简洁。

很多时候用到select的级联,即第二个select的值随着第一个select选中的值变化。这在jquery中是非常简单的。

如:$(".selector1").change(function(){

     // 先清空第二个

      $(".selector2").empty();

     // 实际的应用中,这里的option一般都是用循环生成多个了

      var option = $("<option>").val(1).text("pxx");

      $(".selector2").append(option);

});

 

ASP.NET获取文件名,后缀名

using System.IO; //引入命名空间      

       string path = "text.aspx";
        string pathName = Path.GetFileName(path);      //获取文件全名 text.aspx
        string pathName1 = Paht.GetFileNameWithoutExtension(path);   //获取文件名 text
        string pathName2 = Path.GetExtension(path);   //获取文件后缀名 aspx

 

2、ArrayLIst 拼接成字符串,拼接后,后面没有“,”不需要再处理

ArrayList arrList = new ArrayList();
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
  arrList.Add(ds.Tables[0].Rows[i]["GUID"].ToString());
}

string LinkGuid = string.Join(",", arrList.ToArray());

 

 

块级元素

div、 hr、 p、 h1~h6 、ul 、ol 、dl、 form 、table

 

 

 

3、在 C# 中可以对字符串使用 PadLeft 和 PadRight 进行轻松地补位。

int i=10;
方法1:Console.WriteLine(i.ToString("D5"));
方法2:Console.WriteLine(i.ToString().PadLeft(5,'0'));//推荐
方法3:Console.WriteLine(i.ToString("00000")); 
 

 

4、超链接样式

  具体某个a 的样式

   a.atest {
            color: red;
            text-decoration: none;
        }
         a.atest:linked {}
         a.atest:visited {}
         a.atest:hover{}
        a.atest:active {}
 5

1.绝对定位的盒子距中不能通过margin:0 auto实现
只能通过下面方法实现
绝对定位的盒子距中
div{
position:absolute;
left:50%;
top:50%;
margin-top:-150px;
margin-left:-150px;
width:300px;
height:300px;
background:rgba(0,0,0,.3);
}

2.定位特殊性
2.1行内元素添加绝对或者固定定位,可以直接设置高度和宽度
否则不能设置高度的。
2.2块级元素添加绝对或者固定定位,如果不给宽度或者高度,默认大小是内容的大小。

 6 css生成三角型

.box1{
width:0;
height:0;

line-height:0; /*低版本兼容性使用*/

font-size:0; /*低版本兼容性使用*/

border-top:50px solid transparent;
border-right:50px solid red;
border-bottom:50px solid blue;
border-left:50px solid green;
}

初见 string.Format("{0,-10}", 8)

做 .net 开发也若干年了,如此写法(下面代码中黄色高亮部分)确是我第一次见(更别提用了):

1
2
3
4
var s1 = string.Format("{0,-10}", 8);
var s2 = string.Format("{0,10}", 8);
var s3 = string.Format("{0,20:yyyy-MM-dd}", DateTime.Today);
var s4 = string.Format("4G 内存便宜了{0,12:C2},我打算买{1,4}条", 145, 2);

大括号中,索引后 分号前,有一个逗号和一个整数(减号表示负数)。

会格式化成为什么样子呢?看下调试截图吧:

image

从上图中能大致看出此端倪,没错:

string.Format("{0,-10}", 8) 等同于 string.Format("{0}", 8).PadRight(10)

string.Format("{0,10}", 8) 等同于 string.Format("{0}", 8).PadLeft(10)。

MSDN上的说明

String.Format 方法 format 参数由零或多个文本序列与零或多个索引占位符混合组成,其中索引占位符称为格式项,对应于与此方法的参数列表中的对象。 格式设置过程将每个格式项替换为相应对象值的字符串表示形式。

格式项的语法如下:

{index[,length][:formatString]}

方括号中的元素是可选的。 下表描述每个元素。 有关复合格式设置功能(包括格式项的语法)的更多信息,请参见复合格式

元素

说明

索引

要设置格式的对象的参数列表中的位置(从零开始)。 如果由 index 指定的对象为null,则格式项将被 String.Empty 替换。 由于该重载在其参数列表中只有单个对象,index 的值必须始终为 0。 如果 index 位置没有参数,将引发FormatException

,length

参数的字符串表示形式中包含的最小字符数。 如果该值是正的,则参数右对齐;如果该值是负的,则参数左对齐。

:formatString

要设置格式的对象支持的标准或自定义格式字符串。 formatString 的可能值与该对象的 ToString(format) 方法支持的值相同。 如果没有指定 formatString,并且要设置格式的对象实现了 IFormattable 接口,则将传递 null 作为用作IFormattable.ToString 格式字符串的 format 参数的值。

MSDN 原文链接:Format 方法 (String, Object)

优点和缺点

优点:

,length 其实把 String.PadLeft 和 String.PadRight 的功能融合在的 String.Format 方法中,简化了代码的编写。

如果没有这种格式,代码写起来麻烦多了:

1
2
3
var s5 = string.Format("4G 内存便宜了{0},我打算买{1}条",
    145.ToString("C2").PadLeft(12),
    2.ToString().PadLeft(4));

缺点:

看起来不直观,复杂,如文首代码中的 “我打算买{1,4}条”。

实际使用少,没用过的看了很纳闷。

 

4、取字符串实际长度

Encoding.GetEncoding("gb2312").GetBytes(test).Length

 

C#中Split分隔字符串的应用(C#、split、分隔、字符串)

 
1、用字符串分隔: 
using System.Text.RegularExpressions;
string str="aaajsbbbjsccc";

string[] sArray=Regex.Split(str,"js",RegexOptions.IgnoreCase);

foreach (string i in sArray) Response.Write(i.ToString() + "<br>");


输出结果:
aaa
bbb
ccc
 

2、用多个字符来分隔:

string str="aaajbbbscccjdddseee";

string[] sArray=str.Split(new char[2]{'j','s'}); 

foreach(string i in sArray) Response.Write(i.ToString() + "<br>"); 



输出结果:
aaa
bbb
ccc
ddd
eee
 

3、用单个字符来分隔:

string str="aaajbbbjccc";

string[] sArray=str.Split('j');

foreach(string i in sArray) Response.Write(i.ToString() + "<br>");

输出结果:
aaa
bbb
ccc
 
 
 
常用SQL 语句
 
多表关联删除语句

delete xml_users
from xml_users a
inner join
(select id,person_id from xml_users where password='abc') b on b.id=a.id and b.person_id=a.person_id

 
 

用SQL语句添加删除修改字段 
1.增加字段 
alter table docdsp add dspcode char(200) 
2.删除字段 
ALTER TABLE table_NAME DROP COLUMN column_NAME 
3.修改字段类型 
ALTER TABLE table_name ALTER COLUMN column_name new_data_type 
4.sp_rename 改名 
更改当前数据库中用户创建对象(如表、列或用户定义数据类型)的名称。 
语法 
sp_rename [ @objname = ] 'object_name' , 
[ @newname = ] 'new_name' 
[ , [ @objtype = ] 'object_type' ] 
如:EXEC sp_rename 'newname','PartStock' 
5.sp_help 显示表的一些基本情况 
sp_help 'object_name' 如:EXEC sp_help 'PartStock' 
6.判断某一表PartStock中字段PartVelocity是否存在 
if exists (select * from syscolumns where id=object_id('PartStock') and name='PartVelocity') 
print 'PartVelocity exists' 
else print 'PartVelocity not exists' 
另法: 
判断表的存在性: 
select count(*) from sysobjects where type='U' and name='你的表名' 
判断字段的存在性: 
select count(*) from syscolumns 
where id = (select id from sysobjects where type='U' and name='你的表名') 
and name = '你要判断的字段名'

一个小例子 
--假设要处理的表名为: tb 
--判断要添加列的表中是否有主键 
if exists(select 1 from sysobjects where parent_obj=object_id('tb') and xtype='PK') 
begin 
print '表中已经有主键,列只能做为普通列添加' 
--添加int类型的列,默认值为0 
alter table tb add 列名 int default 0 
end 
else 
begin 
print '表中无主键,添加主键列' 
--添加int类型的列,默认值为0 
alter table tb add 列名 int primary key default 0 
end

7.随机读取若干条记录 
Access语法:SELECT top 10 * From 表名 ORDER BY Rnd(id) 
Sql server:select top n * from 表名 order by newid() 
mysql select * From 表名 Order By rand() Limit n 
8.说明:日程安排提前五分钟提醒 
SQL: select * from 日程安排 where datediff(minute,f开始时间,getdate())>5

9.前10条记录 
select top 10 * form table1 where 范围

10.包括所有在 TableA 中但不在 TableB和TableC 中的行并消除所有重复行而派生出一个结果表 
(select a from tableA ) except (select a from tableB) except (select a from tableC)

11.说明:随机取出10条数据 
select top 10 * from tablename order by newid()

12.列出数据库里所有的表名 
select name from sysobjects where type=U 
13.列出表里的所有的字段名 
select name from syscolumns where id=object_id(TableName) 
14.说明:列示type、vender、pcs字段,以type字段排列,case可以方便地实现多重选择,类似select 中的case。 
select type,sum(case vender when A then pcs else 0 end),sum(case vender when C then pcs else 0 end),sum(case vender when B then pcs else 0 end) FROM tablename group by type 
15.说明:初始化表table1 
TRUNCATE TABLE table1 
16.说明:几个高级查询运算词 
  A: UNION 运算符 
  UNION 运算符通过组合其他两个结果表(例如 TABLE1 和 TABLE2)并消去表中任何重复行而派生出一个结果表。当 ALL 随 UNION 一起使用时(即 UNION ALL),不消除重复行。两种情况下,派生表的每一行不是来自 TABLE1 就是来自 TABLE2。

  B: EXCEPT 运算符 
  EXCEPT 运算符通过包括所有在 TABLE1 中但不在 TABLE2 中的行并消除所有重复行而派生出一个结果表。当 ALL 随 EXCEPT 一起使用时 (EXCEPT ALL),不消除重复行。

  C: INTERSECT 运算符 
  INTERSECT 运算符通过只包括 TABLE1 和 TABLE2 中都有的行并消除所有重复行而派生出一个结果表。当 ALL 随 INTERSECT 一起使用时 (INTERSECT ALL),不消除重复行。 
  注:使用运算词的几个查询结果行必须是一致的。


17.说明:在线视图查询(表名1:a ) 
select * from (SELECT a,b,c FROM a) T where t.a > 1;

18.说明:between的用法,between限制查询数据范围时包括了边界值,not between不包括 
select * from table1 where time between time1 and time2 
select a,b,c, from table1 where a not between 数值1 and 数值2

19.说明:in 的使用方法 
select * from table1 where a [not] in (‘值1','值2','值4','值6')

20.说明:两张关联表,删除主表中已经在副表中没有的信息 
delete from table1 where not exists ( select * from table2 where table1.field1=table2.field1 ) 
21. 说明:复制表(只复制结构,源表名:a 新表名:b) (Access可用) 
  法一:select * into b from a where 1<>1 
  法二:select top 0 * into b from a

22.说明:拷贝表(拷贝数据,源表名:a 目标表名:b) (Access可用) 
insert into b(a, b, c) select d,e,f from b;

23.说明:跨数据库之间表的拷贝(具体数据使用绝对路径) (Access可用) 
insert into b(a, b, c) select d,e,f from b in ‘具体数据库' where 条件 
  例子:..from b in "&Server.MapPath(".")&"\data.mdb" &" where.. 
24.创建数据库 
CREATE DATABASE database-name

25.说明:删除数据库 
drop database dbname 
26.说明:备份sql server 
  --- 创建 备份数据的 device 
USE master 
EXEC sp_addumpdevice disk, testBack, c:\mssql7backup\MyNwind_1.dat

  --- 开始 备份 
BACKUP DATABASE pubs TO testBack

27.说明:创建新表 
create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..) 
  根据已有的表创建新表: 
A:create table tab_new like tab_old (使用旧表创建新表) 
B:create table tab_new as select col1,col2… from tab_old definition only

28.说明: 
  删除新表:drop table tabname

29.说明: 
  增加一个列:Alter table tabname add column col type 
  注:列增加后将不能删除。DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。

30.说明: 
  添加主键:Alter table tabname add primary key(col) 
  说明: 
  删除主键:Alter table tabname drop primary key(col)

31.说明: 
  创建索引:create [unique] index idxname on tabname(col….) 
  删除索引:drop index idxname 
  注:索引是不可更改的,想更改必须删除重新建。

32.说明: 
  创建视图:create view viewname as select statement 
  删除视图:drop view viewname

33 初始化自增列

    DBCC CHECKIDENT(表名, RESEED,1)

 

posted @ 2018-04-08 23:01  星火卓越  阅读(248)  评论(0编辑  收藏  举报