奇怪的问题 串口通信程序将txt数据保存进access中产生
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.InitialDirectory = @"c:";
openFileDialog1.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
int n = 0;
string fname = openFileDialog1.FileName;
StreamReader sr = new StreamReader(@fname, System.Text.Encoding.GetEncoding("gb2312"));
char[] parsChar = { ' ' };
string water = string.Empty;
string date1 = string.Empty;
string time1 = string.Empty;
string temp = string.Empty;
string date2 = string.Empty;
string time2 = string.Empty;
string space = string.Empty;
string sctemp = textBox1.Text;
string tmpLine;
string splitline;
string[] line; //string[] line2;
while ((tmpLine = sr.ReadLine()) != null)
{
n++;
linebuilder.Append(tmpLine + " ");
if (n % 2 == 0)
{
splitline = linebuilder.ToString();
line = splitline.Split(parsChar);
water = line[0];
date1 = line[1];
time1 = line[2];
space = line[3];
temp = line[4];
date2 = line[5];
time2 = line[6];
OleDbCommand cmd = new OleDbCommand("Insert Into txttest ([water],[date1],[time1],[temp],[date2],[time2],[sctemp]) values ('" + water + "','" + date1 + "','" + time1 + "','" + temp + "','" + date2 + "','" + time2 + "','" + sctemp + "') ");
using (OleDbConnection conn = new OleDbConnection(connStr))
{
cmd.CommandType = CommandType.Text;
if (conn.State != ConnectionState.Open)
{ conn.Open(); }
cmd.Connection = conn;
cmd.ExecuteNonQuery();
}
}
}
}
}