简体   繁体   中英

How to store data in a SQL Server table when the data comes from a while loop function and shown in a list view?

I have a loop like this:

 while (axCZKEM1.SSR_GetGeneralLogData(iMachineNumber, out sdwEnrollNumber, out idwVerifyMode,
                                       out idwInOutMode, out idwYear, 
                                       out idwMonth, out idwDay, out idwHour,
                                       out idwMinute, out idwSecond, 
                                       ref idwWorkcode))   // get records from the memory
{
    iGLCount++;

    lvLogs.Items.Add(iGLCount.ToString());
    lvLogs.Items[iIndex].SubItems.Add(sdwEnrollNumber);//modify by Darcy on Nov.26 2009
    lvLogs.Items[iIndex].SubItems.Add(idwVerifyMode.ToString());
    lvLogs.Items[iIndex].SubItems.Add(idwInOutMode.ToString());
    lvLogs.Items[iIndex].SubItems.Add(idwYear.ToString() + "-" + idwMonth.ToString() + "-" + idwDay.ToString() + " " + idwHour.ToString() + ":" + idwMinute.ToString() + ":" + idwSecond.ToString());
    lvLogs.Items[iIndex].SubItems.Add(idwWorkcode.ToString());

    iIndex++;
}

This will show the output in the list view format with seven columns.

I want to store the data returned from the while loop in a SQL Server table. What should I do?

I tried the coding as below outside the loop function and also inside

SqlDataAdapter _sqldataadapter = new SqlDataAdapter("insert into Record (sdwEnrollNumber,idwInOutMode,idwVerifyMode)values" +
                //    "('" + lvLogs.Items[iIndex].SubItems.Add(sdwEnrollNumber) + "','" + lvLogs.Items[iIndex].SubItems.Add(idwInOutMode.ToString()) + "','" + lvLogs.Items[iIndex].SubItems.Add(idwVerifyMode.ToString()) + "')", con);

                //MessageBox.Show("Inserted successfully");
                //con.Close();

but I am not getting the data in the table.

Please help me to resolve this

I cannot check the function by myself, because I don't have the table definition, but from my point of view:

You loop over a table using iIndex as index number. Then you add 1 to iIndex ( iIndex++ )

So of course tha data can't be accessed by the index anymore. So put your _sqldataadapter before iIndex++ .

Should look like this:

{
  while (axCZKEM1.SSR_GetGeneralLogData(iMachineNumber, out 
                 sdwEnrollNumber, out idwVerifyMode,
                   out idwInOutMode, out idwYear, out idwMonth, out 
                   idwDay, out idwHour, out idwMinute, out idwSecond, 
                  ref idwWorkcode))//get records from the memory
        {
            iGLCount++;
            lvLogs.Items.Add(iGLCount.ToString());
            lvLogs.Items[iIndex].SubItems.Add(sdwEnrollNumber);//modify by Darcy on Nov.26 2009
            lvLogs.Items[iIndex].SubItems.Add(idwVerifyMode.ToString());
            lvLogs.Items[iIndex].SubItems.Add(idwInOutMode.ToString());
            lvLogs.Items[iIndex].SubItems.Add(idwYear.ToString() + "-" + idwMonth.ToString() + "-" + idwDay.ToString() + " " + idwHour.ToString() + ":" + idwMinute.ToString() + ":" + idwSecond.ToString());
            lvLogs.Items[iIndex].SubItems.Add(idwWorkcode.ToString());

SqlDataAdapter _sqldataadapter = new SqlDataAdapter("insert into Record (sdwEnrollNumber,idwInOutMode,idwVerifyMode)values" +
                //    "('" + lvLogs.Items[iIndex].SubItems.Add(sdwEnrollNumber) + "','" + lvLogs.Items[iIndex].SubItems.Add(idwInOutMode.ToString()) + "','" + lvLogs.Items[iIndex].SubItems.Add(idwVerifyMode.ToString()) + "')", con);

                //MessageBox.Show("Inserted sucessfully");
                //con.Close();


            iIndex++;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM