随笔 - 113  文章 - 0  评论 - 85  阅读 - 54万

Asp.net综合手册

1.拼接sql语句,用string.format代替string

strsql = string.Format("Update T_Adves Set AdTitle='{0}',AdState={1} where AdId={2}",
tb_AdTitle.Text, ckb_haspic.Checked == true ? 1 : 0, Request.QueryString["id"]);

 

strsql = string.Format("insert Into T_News (title,contents,haspic) values ('{0}','{1}',{2})",
tb_title.Text,ftb_News.Text.Replace("'", "''"), ckb_haspic.Checked == true ? 1 : 0);

 2.去除最后一个字符

s = s.Substring(0,s.Length -1)

3、在第二窗体将查询结果返回到主窗体DataGridView中

举个例子:
假设窗体:
public string str="";
主窗体单击查询按钮事件:
Form2 f2 = new Form2();
f2.Show(this); //这里的"this"很重要
在Form2中确认按键中事件:
Form1 f1;
f1 = (Form1)this.Owner;
f1.str= "确认";
还有其他的方式:
比如:代理方法

 4、无意中编写了一段爬网页的脚本

try
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | (SecurityProtocolType)3072 | (SecurityProtocolType)768 | SecurityProtocolType.Tls;
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://www.qq.com/");
req.Timeout = 2000;
req.UserAgent = "Code Sample Web Client";
req.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse respone = (HttpWebResponse)req.GetResponse();

StreamReader sr = new StreamReader(respone.GetResponseStream(), Encoding.UTF8);
var htmlinfo = sr.ReadToEnd();
sr.Close();
respone.Dispose();
}
catch (Exception err)
{

}

5、获取本机客户端的公网IP

如果是路由上网的,想获取网关的外网IP,只能通过访问一些公网的地址来获取外网IP了

步骤:先获取到含有本机外网ip的页面信息,再通过正则获取到ip信息;

string tempip = "";
try
{
WebClient MyWebClient = new WebClient();
Encoding encode = Encoding.GetEncoding("utf-8");
MyWebClient.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36");
MyWebClient.Credentials = CredentialCache.DefaultCredentials;//获取或设置用于向Internet资源的请求进行身份验证的网络凭据
Byte[] pageData = MyWebClient.DownloadData("https://www.ip.cn/"); //从指定网站下载数据
var htmlinfo = encode.GetString(pageData);
//匹配IP的正则表达式
var r = new Regex("((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)\\.){3}(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|[1-9])", RegexOptions.None);
var mc = r.Match(htmlinfo);
//获取匹配到的IP
tempip = mc.Groups[0].Value;
}
catch (Exception ex)
{

}

posted on   林枫山  阅读(365)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
< 2011年11月 >
30 31 1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 1 2 3
4 5 6 7 8 9 10

点击右上角即可分享
微信分享提示