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)
{
}
浙公网安备 33010602011771号