[英]How to insert wifi scan data into SQL Server table
我已经用C#扫描了wifi数据,并将其分为所需的MAC,SSID和RSSi格式。 当我按下按钮时,结果应该插入到我已经创建的SQL Server表中。 如果我再次按下该按钮,它将停止插入。 我已经尝试了几种方法,但无法正常工作,不胜感激
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
namespace WICED_SERIALPORT_TEST
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string[] ports = SerialPort.GetPortNames();
foreach (string port in ports)
{
comboBox1.Items.Add(port);
}
}
string t;
private void button2_Click(object sender, EventArgs e)
{
t = comboBox1.Text.ToString();
sErial(t);
}
SerialPort sp;
void sErial(string Port_name)
{
sp = new SerialPort(Port_name, 115200, Parity.None,8,
StopBits.One);
sp.DataReceived += new
SerialDataReceivedEventHandler(DataReceivedHandler);
sp.Open();
}
private void DataReceivedHandler(object sender,
SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string msg = string.Empty;
bool canCont = false;
while (!canCont)
{
msg += sp.ReadLine();
if (msg.Contains("Scan complete "))
{
canCont = true;
}
}
//string w = sp.ReadLine();
//string w = sp.ReadExisting();
// string msg = sp.ReadExisting();
string[] msgArr = msg.Split('\r');
Invoke(new Action(() => listBox1.Items.Clear()));
List<string[]> list = new List<string[]>();
List<Networks> Scan = new List<Networks>();
for (int i = 0; i < msgArr.Length; i++)
{
list.Add(msgArr[i].Split(new string[] { " " },
StringSplitOptions.RemoveEmptyEntries));
}
for (int i = 0; i < list.Count; i++)
{
if (i > 2)
{
if (list[i].Length > 4)
{
int numOfSplits = 0;
List<string> tempList = new List<string>();
for (int ii = 0; ii < list[i].Length; ii++)
{
if (numOfSplits < 6)
{
string[] temp1 = list[i][ii].Split(new char[] { ' ' },
StringSplitOptions.RemoveEmptyEntries);
numOfSplits += temp1.Length;
for (int iii = 0; iii < temp1.Length; iii++)
{
tempList.Add(temp1[iii]);
}
}
else
{
tempList.Add(list[i][ii]);
}
}
Scan.Add(new Networks()
{
ID = Convert.ToInt32(tempList[0]),
NetworkType = tempList[1],
MAC = tempList[2],
RSSi = Convert.ToInt32(tempList[3]),
Rate = Convert.ToDouble(tempList[4]),
Channel = Convert.ToInt32(tempList[5]),
Security = tempList[6],
SSID = tempList[7],
});
}
}
}
if (msg != String.Empty)
{
Invoke(new Action(() => richTextBox1.AppendText(msg)));
}
for (int i = 0; i < Scan.Count; i++)
{
Invoke(new Action(() => listBox1.Items.Add(Scan[i].MAC)));
Invoke(new Action(() => listBox2.Items.Add(Scan[i].RSSi)));
Invoke(new Action(() => listBox3.Items.Add(Scan[i].SSID)));
msg = string.Empty;
list.Clear();
}
}
public SqlConnection con = new SqlConnection(@"Data Source=
(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\AP_SCAN_DATA.mdf;
Integrated Security=True");
private void button3_Click(object sender, EventArgs e)
{
using (SqlConnection connection = new SqlConnection(@"Data Source=
(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\AP_SCAN_DATA.mdf;
Integrated Security=True"))
{
using (SqlCommand command = new SqlCommand())
{
command.Connection = connection; // <== lacking
command.CommandType = CommandType.Text;
command.CommandText = "INSERT into LOCATIONSCAN ((Scan[i].SSID), (Scan[i].MAC),(Scan[i].RSSi)) VALUES (@SSID, @MAC, @RSSi)";
command.Parameters.AddWithValue("@SSID", listBox1);
command.Parameters.AddWithValue("@MAC", listBox2);
command.Parameters.AddWithValue("@RSSi",listBox3);
try
{
connection.Open();
int recordsAffected = command.ExecuteNonQuery();
}
catch(SqlException)
{
// error here
}
finally
{
connection.Close();
}
}
}
}
}
首先,您有一个空的CATCH
块,它将导致您的SqlExceptions静默消失。 删除CATCH,该异常将对您有所帮助。 其次,您的CommandText看起来很奇怪,不知道您的数据库模式,我想应该是这样的。
command.CommandText = "INSERT into LOCATIONSCAN (SSID, MAC, RSSi) VALUES ('@SSID', '@MAC', '@RSSi')";
下一个问题是,添加参数时,您必须指定包含值的列表框的属性。 例如,它可能是SelectedValue属性。
command.Parameters.AddWithValue("@SSID", listBox1.SelectedValue);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.