.net技术小知识点
1、repeater控件绑定时间非空判断
<%#Convert.IsDBNull(Eval("Time_Ok")) ? "--" : Eval("Time_Ok")%>
2、aspx页面通过jquery选中设定值的下拉框
var key=<%=locked %>; $("#sellcokId option[value='"+key+"']").attr("selected","selected");
3、jquery获取下拉框选中的值
var id = $('#select_id option:selected').val();
4、页面跳转时页面嵌套重叠问题解决方法
if (window != window.top.window) { window.top.window.location.href = '/admin/login.aspx'; }
5、修改sql server 自增主键ID初始值
DBCC CHECKIDENT ('Person',RESEED,100000000)
6、vs使用快捷键小集
Ctrl+j 智能提示、Ctrl+e+d 对齐、 F6 生成解决方案、 Ctrl+shift+空格 显示方法参数
7、sql 根据时间查询
Sql Server中查询今天、昨天、本周、上周、本月、上月数据 在做Sql Server开发的时候有时需要获取表中今天、昨天、本周、上周、本月、上月等数据,这时候就需要使用DATEDIFF()函数及GetDate()函数了。 DATEDIFF ( datepart , startdate , enddate ) 释义:计算时间差 datepare值:year | quarter | month | week | day | hour | minute | second | millisecond startdate:开始日期 enddate :结束日期 GetDate() 释义:获取当前的系统日期 下面例子中表名为tablename,条件字段名为inputdate 查询今天 SELECT * FROM tablename where DATEDIFF(day,inputdate,GETDATE())=0 查询昨天 SELECT * FROM tablename where DATEDIFF(day,inputdate,GETDATE())=1 查询本周 SELECT * FROM tablename where datediff(week,inputdate,getdate())=0 查询上周 SELECT * FROM tablename where datediff(week,inputdate,getdate())=1 查询本月 SELECT * FROM tablename where DATEDIFF(month,inputdate,GETDATE())=0 查询上月 SELECT * FROM tablename where DATEDIFF(month,inputdate,GETDATE())=1 查询本季度的 select * from T_InterViewInfo where datediff(QQ,inputdate,getdate())=0
sqlserver中如何实现时间按月,日,小时分组查询 --按照月份统计 select count(id) cnt,datepart(mm,time) [Month] from [table] where [time] between '2007/09/08 09:10:43' and '2007/10/09 04:32:37' group by datepart(mm,time) --按照日统计 select count(id) cnt,datepart(dd,time) [Day] from [table] where time between '2007/09/08 09:10:43' and '2007/10/09 04:32:37' group by datepart(dd,time) --按照小时统计 select count(id) cnt,datepart(hh,time) [Hour] from [table] where [time] between '2007/09/08 09:10:43' and '2007/10/09 04:32:37' group by datepart(hh,time) ========================================================================== 1、每年 select year(ordertime) AS '年', sum(Total) '销售合计' from order_list group by year(ordertime) 2、每月 select year(ordertime) '年', month(ordertime) '月', sum(Total) '销售合计' from order_list group by year(ordertime), month(ordertime) 3、每日 select year(ordertime) '年', month(ordertime) '月', day(ordertime) '日', sum(Total) '销售合计' from order_list group by year(ordertime), month(ordertime), day(ordertime) 另外每日也可以这样: select convert(char(8),ordertime,112) dt, sum(Total) '销售合计' from order_list group by convert(char(8),ordertime,112) 另外,每月(年、日)的记录条数 select year(ordertime) '年', month(ordertime) '月', count(*) '销售记录' from order_list group by year(ordertime), month(ordertime)
8、执行webapi项目命令
在cmd命令行窗口中,切换到webapi项目文件下面,输入 dotnet watch run 命令就可以将程序执行起来,就不用每一次修改完api接口,都要去重新启动项目
如(目录需要指定到具体的api项目根目录):D:\project\rr\xx\xx\ww\api dotnet watch run
9、sql server maanger studio下载地址
https://docs.microsoft.com/zh-cn/sql/ssms/download-sql-server-management-studio-ssms?view=sql-server-2017
10、visual studio 老版本下载地址
https://visualstudio.microsoft.com/zh-hans/vs/older-downloads/
11、使项目脱离svn管理
建一个记事本文件,然后吧这句话复制进去 for /r . %%a in (.) do @if exist "%%a\.svn" rd /s /q "%%a\.svn" 然后保存,在吧记事本文件从命名 叫 删除SVN信息.bat ,这时候要注意后缀名不是txt而是bat 了,然后双击这个批处理文件就可以解决 要在那个项目的根目录
12、在安装是vs目录:D:\install\vs2017\Common7\IDE 里面有一个WcfTestClient.exe可执行文件,该程序可测试wcf测试客户端
13、vscode快捷键
代码行向左或向右缩进: Ctrl+[ 、 Ctrl+] 复制或剪切当前行/当前选中内容: Ctrl+C 、 Ctrl+V 代码格式化: Shift+Alt+F 向上或向下移动一行: Alt+Up 或 Alt+Down 向上或向下复制一行: Shift+Alt+Up 或 Shift+Alt+Down 在当前行下方插入一行: Ctrl+Enter 在当前行上方插入一行: Ctrl+Shift+Enter
14、webstorm快捷键
ctrl + / : 单行注释 ctrl + shift + / : 块注释 ctrl+alt+L 格式化代码 Ctrl+Shift+Up/Down 代码向上/下移动。 ctrl + d: 行复制
15、.NET CORE项目部署
案例:https://www.cnblogs.com/w5942066/p/13411646.html
16、根据地址还原文件(图片)
static public byte[] GetBytesFromUrl(string url) { byte[] b; HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url); WebResponse myResp = myReq.GetResponse(); Stream stream = myResp.GetResponseStream(); //int i; using (BinaryReader br = new BinaryReader(stream)) { //i = (int)(stream.Length); b = br.ReadBytes(500000); br.Close(); } myResp.Close(); return b; } static public void WriteBytesToFile(string fileName, byte[] content) { FileStream fs = new FileStream(fileName, FileMode.Create); BinaryWriter w = new BinaryWriter(fs); try { w.Write(content); } finally { fs.Close(); w.Close(); } }
17、解决webapi项目新建的map.json文件中文乱码问题
可通过Notepad++将其打开,然后编码方式选择utf-8编码方式
18、SQLite分页
降序分页 Select * From CodeUsed Where CodeUsedId <=( Select CodeUsedId From CodeUsed Order By CodeUsedId desc limit 6,1 ) Order By CodeUsedId desc limit 3 升序分页 Select * From CodeUsed Where CodeUsedId >=( Select CodeUsedId From CodeUsed Order By CodeUsedId limit 6,1 ) Order By CodeUsedId limit 3
//升序
Select * From 表 Where 主键ID>=(
Select 主键ID From 表 Order By 主键ID limit (pageIndex -1) * pagesize,1
) Order By 主键ID limit pagesize
//降序
Select * From 表 Where 主键ID<=(
Select 主键ID From 表 Order By 主键ID desc limit (pageIndex -1) * pagesize,1
) Order By 主键ID desc limit pagesize
通过关键字limit进行分页,limit n,m 表示跳过n行查询m条数据
或者使用 limit m offset n 同上,表示跳过n行查询m条数据
int pagesize = m = 3
int pageIndex = 1
n = (pageIndex - 1) * pagesize
19、操作不同服务器数据库数据
https://www.cnblogs.com/zhaoyl9/p/11527090.html
20、读取文件转为byte
//将一个file文件转化为byte[] public static byte[] GetBytesFromUrl1(string url) { byte[] b; FileWebRequest myReq = (FileWebRequest)WebRequest.Create(url); WebResponse myResp = myReq.GetResponse(); Stream stream = myResp.GetResponseStream(); //int i; using (BinaryReader br = new BinaryReader(stream)) { b = br.ReadBytes(500000); br.Close(); } myResp.Close(); return b; }
21,加密解密操作
http://encode.chahuo.com/
22、后台系统跳转页面时,主页出现了嵌套解决方法
if (window != window.top.window) { window.top.window.location.href = '/login.aspx'; }
23、使用TransactionScope进行事务回滚,只需要将代码放入到里面就行了,Complete()方法表示事务完成
try { using (TransactionScope ts = new TransactionScope()) { OP_UserInfo info1 = OP_UserInfoBLL.GetModel(100); info1.Remark = "测试事务"; OP_UserInfoBz.Update(info1); int m = 0; int n = 11; int c = n / m; Console.WriteLine(c); ts.Complete(); } } catch (Exception ex) { Console.WriteLine(ex.Message); }
24、asp.net导出word
public void Download() { Random rd = new Random(); string fileName = DateTime.Now.ToString("yyyyMMddhhmm") + rd.Next() + ".doc"; //存储路径 string path = Server.MapPath(fileName); //创建字符输出流 StreamWriter sw = new StreamWriter(path, true, System.Text.UnicodeEncoding.UTF8); //需要导出的内容 // string str = "<html><head><title>无标题文档</title></head><body>这里放从数据库导出的word文档内容</body></html>"; string str = ""; str += "<html><head><title>无标题文档</title></head><body>"; str += "<div>阅读报表</div>"; str += "<table border='1'><tr>"; str += "<td>20000</td>"; str += "<td>10000</td></tr><tr>"; str += "<td>30000</td>"; str += "<td>30000</td><tr>"; str += "</table></body></html>"; //写入 sw.Write(str); sw.Close(); Response.Clear(); Response.Buffer = true; this.EnableViewState = false; Response.Charset = "utf-8"; Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); Response.ContentType = "application/octet-stream"; Response.WriteFile(path); Response.Flush(); Response.Close(); Response.End(); }
25、asp.net导出pdf
string fileurl = Server.MapPath("/file/" + model.DailyId +"-"+ DateTime.Now.ToString("yyyyMMddHHmmss")+ ".pdf"); string filename = model.DailyId + "-" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf"; PDFHelper pdf = new PDFHelper(); pdf.Open(new FileStream(fileurl, FileMode.Create)); pdf.SetBaseFont(@"C:\Windows\Fonts\SIMHEI.TTF"); pdf.AddImage("/images/a.jpg", 0, 200, 50); /// <summary> /// 添加段落 /// </summary> /// <param name="content">内容</param> /// <param name="fontsize">字体大小</param> /// <param name="Alignment">对齐方式(1为居中,0为居左,2为居右)</param> /// <param name="SpacingAfter">段后空行数(0为默认值)</param> /// <param name="SpacingBefore">段前空行数(0为默认值)</param> /// <param name="MultipliedLeading">行间距(0为默认值)</param> pdf.AddParagraph(model.DailyContent, 15, 1, 10, 0, 0); pdf.AddParagraph(model.Remark + " 时间:" +model.PostTime.ToString("yyyy/MM/dd HH:mm:ss"), 25, 2, 20, 0, 0); pdf.Close(); //取文件大小 FileStream MyFileStream; uint FileSize1; MyFileStream = new FileStream(fileurl, FileMode.Open, FileAccess.Read, FileShare.None); int iConverTemp = Convert.ToInt32(MyFileStream.Length); FileSize1 = (uint)(iConverTemp); MyFileStream.Close(); Response.ContentType = "APPLICATION/OCTET-STREAM"; Response.AddHeader("Content-length", FileSize1.ToString());//下载文件长度 Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8)); Response.WriteFile(fileurl); Response.Flush(); Response.End();
26、获取ip地址
public static IPAddress GetIP() { NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface adapter in nics) { foreach (var uni in adapter.GetIPProperties().UnicastAddresses) { if (uni.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { return uni.Address; } } } return null; }
27、去掉序列化为json字符串中携带的反斜杠
string responsedata = JsonConvert.SerializeObject(obj); var json = new JsonSerializer().Deserialize(new JsonTextReader(new StringReader(responsedata))); string res = "{\"code\":0,\"messgae\":\"xxxx\",\"data\":" + json + "}";
这样最终的res 字符串就不带反斜杠了
如有需要可联系(qq:2831804923 / 微信号:wh0612910)
----------------------------------------------------------------------------------------------------------------------
愿本文对阁下有帮助,在下谢之: