添加新类qSaveConfigServer 服务器信息保存类
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
public class qSaveConfigServer //服务器信息保存
{
public static bool SaveConfigServer(string ipadd, string port)
{ //保存服务器名和端口
try
{
StringBuilder str = new StringBuilder();
string ConfigFileUrl = "config/ipConfig.dat";
str.Append("
str.Append("
str.Append("
str.Append("
StreamWriter save = new StreamWriter(ConfigFileUrl);
save.Write(str.ToString());
save.Close();
return (true);
}
catch
{
return (false);
}
}
public static bool SaveConfigDate(string ip, string database, string uid, string pwd)
{ //保存数据库连接地址
try
{
StringBuilder str = new StringBuilder();
string FileUrl = "config/dataConfig.dat";
str.Append("
str.Append("
str.Append("
str.Append("
str.Append("
str.Append("
StreamWriter save = new StreamWriter(FileUrl);
save.Write(str.ToString());
save.Close();
return (true);
}
catch
{
return (false);
}
}
public static string GetValueByNode(string XmlFile, string NodePath)
{ //读取结点内容
if (File.Exists(XmlFile))
{
XmlDocument SerInf = new XmlDocument();
SerInf.Load(XmlFile);
XmlNode FirstNode = SerInf.SelectSingleNode(NodePath).FirstChild;
return (FirstNode.InnerText);
}
else
{
return ("-1");
}
}
}
StringBuilder.Append:在此实例的结尾追加指定对象的字符串表示形式。
StreamWriter.Write:写入流
File.Exists:确定指定的文件是否存在
XmlDocument:xml文档
XmlDocument.Load :加载指定的 XML 数据。
SelectSingleNode(XPath):选择匹配 XPath 表达式的第一个 XmlNode
XmlNode:表示 XML 文档中的单个节点
XmlNode.FirstChild :获取节点的第一个子级。
XmlNode.InnerText:获取或设置节点及其所有子节点的串联值。
添加新类 SqlConnectionTest //数据库链接类
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.IO;
public class SqlConnectionTest
{
private static string constr = "";
public static string ConStr
{
set
{
if (value != "")
{
constr = value;
}
}
}
public static bool ConnectionTest(string Server, string Database, string Uid, string Pwd) //连接数据库
{
try
{
string strCon = "Database=" + Database + ";Server=" + Server + ";uid=" + Uid + ";pwd=" + Pwd + ";";
SqlConnection con = new SqlConnection(strCon);
con.Open();
return (true);
}
catch
{
return (false);
}
}
public static string GetConnString()
{ //从xml文件中,读取数据,返回连接字符串
try
{
string XmlUrl = "config\\dataConfig.dat";
if (File.Exists(XmlUrl))
{
string Server, Database, Uid, Pwd, ConnStr;
Server = qSaveConfigServer.GetValueByNode(XmlUrl, "//root//server//ip");
Database = qSaveConfigServer.GetValueByNode(XmlUrl, "//root//server//database");
Uid = qSaveConfigServer.GetValueByNode(XmlUrl, "//root//server//uid");
Pwd = qSaveConfigServer.GetValueByNode(XmlUrl, "//root//server//pwd");
if (Server == "-1" || Database == "-1" || Uid == "-1" || Pwd == "-1")
{
return ("-1");
}
else
{
ConnStr = "Database=" + Database + ";Server=" + Server + ";uid=" + Uid + ";pwd=" + Pwd + ";";
return (ConnStr);
}
}
else
{
return (constr);
}
}
catch
{
return ("-1");
}
}
public static SqlConnection GetConnection()
{ //打开数据库
string ConStr = GetConnString();
if (ConStr != "-1")
{
SqlConnection con = new SqlConnection(ConStr);
return (con);
}
else
{
return (null);
}
}
}
添加新类 regx 验证用户提交数据合法性类
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
public class regx
{
public static bool isIpaddres(string ip)
{ // 验证该字符串是否是合法的ip地址
Regex reg = new Regex("(((2[0-4]\\d)|(25[0-5]))|(1\\d{2})|([1-9]\\d)|(\\d))[.](((2[0-4]\\d)|(25[0-5]))|(1\\d{2})|([1-9]\\d)|(\\d))[.](((2[0-4]\\d)|(25[0-5]))|(1\\d{2})|([1-9]\\d)|(\\d))[.](((2[0-4]\\d)|(25[0-5]))|(1\\d{2})|([1-9]\\d)|(\\d))");
if (reg.IsMatch(ip, 0)) //检查IP
{
return (true);
}
else
{
return (false);
}
}
public static bool isNull(string check)
{// 验证该字符串是否是空值
if (check.Trim() == "" || check == "")
{
return (true);
}
else
{
return (false);
}
}
public static bool IsThisQQNumber(string QNumber)
{// 验证该字符串是否是合法的QQ号码
Regex reg = new Regex("\\d{8}");
if (reg.IsMatch(QNumber, 0))
{
return (true);
}
else
{
return (false);
}
}
public static bool IsThisNumber(string Number)
{// 验证该字符串是否为数字
Regex reg=new Regex("[1-9]\\d*");
if(reg.IsMatch(Number,0))
{
return(true);
}
else
{
return(false);
}
}
}
Regex:位于System.Text.RegularExpressions下。表示不可变的正则表达式。
Regex.IsMatch:指示正则表达式在输入字符串中是否找到匹配项。
0 评论:
发表评论