我的列表框中有3个元素(脉率,氧气水平,日期/时间)。我的列表框中的数据正在从COM端口读取。我想将这3个元素插入数据库表的3个不同的字段。我怎么做?插入列表框项目到数据库中的多个字段c#
int numberOfBytesToRead = 125;
if (bBuffer.Count >= numberOfBytesToRead)
{
this.Invoke(new Action(() =>
listBox1.Items.Add("SP: " + (bBuffer[43].ToString()) + " " + " HR: "
+ bBuffer[103].ToString() +" " + " Time: " + DateTime.Now.ToString())
));
bBuffer.RemoveRange(0, numberOfBytesToRead);
}
string strConnString = ConfigurationManager.ConnectionStrings["BMS.Properties.Settings.BMS"].ConnectionString;
SqlConnection myConnect = new SqlConnection(strConnString);
try
{
string strCmd = "INSERT INTO DimRecords (PulseRate, OxygenLevel, PatientID, DateOfMonitoring) ";
strCmd += " VALUES (@PR, @OL, @ID, @Date)";
SqlCommand cmd = new SqlCommand(strCmd, myConnect);
cmd.Parameters.AddWithValue("@PR", listBox1.Text);
cmd.Parameters.AddWithValue("OL",listBox1.Text);
cmd.Parameters.AddWithValue("@ID", labelID.Text);
cmd.Parameters.AddWithValue("@Date",listBox1.Text);
myConnect.Open();
int result = cmd.ExecuteNonQuery();
if (result > 0)
{
MessageBox.Show("Data Inserted");
}
else
{
MessageBox.Show("Error!");
}
myConnect.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
到目前为止,你做了什么。 ?分享您的代码 – 2011-12-23 06:18:11
对不起,我的代码被上传。 – fb69 2011-12-23 06:21:05
你已经发布了一些代码。什么不工作? – 2011-12-23 06:26:13