110,536
社区成员
发帖
与我相关
我的任务
分享
private static string insertStr = "";
private static string usingStr = "";
private static string RealName = "";
private static void DataReceviedHandler(
object sender,
SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
insertStr += indata;
if (insertStr.Length > 12)
{
string insertStr2 = insertStr.Split('o')[0];
insertStr = insertStr2;
}
foreach (char ch in insertStr)
{
if (ch != 'E' && ch != 'S'&& ch != 'o')
{
usingStr += ch;
}
}
switch (usingStr)
{
case "2011072069\0":
RealName = "a";
break;
case "2010073065\0":
RealName = "b";
break;
default:
break;
}
if (RealName != "")
{
if (insertStr.Contains("E"))
{
string Sqlcom = "INSERT INTO tb_out(RealName, Num, Date) VALUES('" + RealName + "'," + "'" + usingStr + "', GetDate());";
ExecuteSQL(Sqlcom);
usingStr = string.Empty;
insertStr = string.Empty;
RealName = string.Empty;
}
if (insertStr.Contains("S"))
{
string Sqlcom = "INSERT INTO tb_in(RealName, Num, Date) VALUES('" + RealName + "'," + "'" + usingStr + "', GetDate());";
ExecuteSQL(Sqlcom);
usingStr = string.Empty;
insertStr = string.Empty;
RealName = string.Empty;
}
}
}
public static void ExecuteSQL(string SQLwords)
{
//执行插入、删除、更新的函数
string myConStr = "user id=sa; password=sa;";
myConStr += "Initial Catalog = AccessControl; Server=PC2012050719LFQ;";
//myConStr += "Connect Timeout = 10";
SqlConnection myCon = new SqlConnection(myConStr);
myCon.Open();
SqlCommand myCom = new SqlCommand();
myCom.CommandType = CommandType.Text;
myCom.CommandText = SQLwords;
myCom.Connection = myCon;
try
{
myCom.ExecuteNonQuery();
}
catch
{
}
myCon.Close();
}