1、有没有办法让JavaScript的注释在客户端不可见呢? 答案很简单,就是:JavaScript注释 + 服务器端注释!
行注释写法:
//<%-- 这里写行注释 --%>
块注释写法:
/*<%--
这里写注释语句块,
多行都行。
--%>*/
我们自己的开发人员可以阅读到完整的注释,而编译后的ASP.NET页面将忽略掉<%--到--%>中的注释部分,恶意攻击者在客户端就看不到这些注释了。只能看到:// 或者 /**/
2、C# 保留N位小数 最快的方法
.ToString("fN")
这里的N替换成数字即可,如 .ToString("f2") 即保留2位小数,还是挺方便的。
3、取配置节数据库连接信息
Code
string connstring = string.Empty;
ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings["DataBaseConnection"];
if (settings != null)
{
connstring = settings.ConnectionString;
}
string connstring = string.Empty;
ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings["DataBaseConnection"];
if (settings != null)
{
connstring = settings.ConnectionString;
}
4、操作数据库
Code
public DataSet ExecuteDataSet(string sql, string connectionString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sql, connectionString);
da.Fill(ds);
return ds;
}
}
public int ExecuteNonQuery(string sql, string connectionString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand cmd = new SqlCommand(sql, connection);
return cmd.ExecuteNonQuery();
}
}
public object ExecuteScalar(string sql, string connectionString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand cmd = new SqlCommand(sql, connection);
return cmd.ExecuteScalar();
}
}
public DataSet ExecuteDataSet(string sql, string connectionString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sql, connectionString);
da.Fill(ds);
return ds;
}
}
public int ExecuteNonQuery(string sql, string connectionString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand cmd = new SqlCommand(sql, connection);
return cmd.ExecuteNonQuery();
}
}
public object ExecuteScalar(string sql, string connectionString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand cmd = new SqlCommand(sql, connection);
return cmd.ExecuteScalar();
}
}
5、.修改配置信息
Code
public void SaveConfig(string key, string value)
{
try
{
string fileName = "Web.Config";
XmlDocument document = new XmlDocument();
document.Load(fileName);
((XmlElement)document.SelectSingleNode(@"//AppSetting/add[@key='" + key + "']")). SetAttribute("Value", value);
document.Save(fileName);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
public void SaveConfig(string key, string value)
{
try
{
string fileName = "Web.Config";
XmlDocument document = new XmlDocument();
document.Load(fileName);
((XmlElement)document.SelectSingleNode(@"//AppSetting/add[@key='" + key + "']")). SetAttribute("Value", value);
document.Save(fileName);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}