Я пытаюсь создать простой список строк из DataGridView
в базу данных.Вставить список строк
Я сделал checkedbox
, что после проверки элемент будет добавлен в DataGridView
. Теперь я пытаюсь сделать часть INSERT. Это то, что я придумал до сих пор:
try
{
string strAppointment = "SELECT appointmentID FROM APPOINTMENT WHERE [email protected]";
SqlCommand cmdAppointment = new SqlCommand(strAppointment, connection);
cmdAppointment.Parameters.AddWithValue("@searchappointmentID", txtAppointmentID.Text);
connection.Open();
for (int i = 0; i < dataPrescription.Rows.Count; i++)
{
string firstColumn = dataPrescription[0, dataPrescription.CurrentCell.RowIndex].Value.ToString();
string strMedications = "SELECT medicationID FROM MEDICATION WHERE medicationName= ('" + firstColumn + "')";
SqlCommand cmdMedications = new SqlCommand(strMedications, connection);
SqlDataReader readMedications = cmdMedications.ExecuteReader();
if (readMedications.Read())
{
string getDrugID = readMedications["medicationID"].ToString();
string strPrescriptions = "INSERT INTO PRESCRIPTION (appointmentID, medicationID, quantity) " +
"VALUES (@insertAppointment, "
+ getDrugID + ", "
+ dataPrescription.Rows[i].Cells["columnQuantity"].Value + ");";
SqlCommand cmdPrescriptions = new SqlCommand(strPrescriptions, connection);
cmdPrescriptions.Parameters.AddWithValue("@insertAppointment", txtAppointmentID.Text);
prescriptionsResult = cmdAppointment.ExecuteNonQuery();
}
readMedications.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error");
}
finally
{
connection.Close();
}
Сейчас он дает мне эту ошибку: «Существует уже открытая DataReader, связанные с командой, которая должна быть закрыта первая». Я не знаю, что я сделал неправильно
Так что вы можете сделать с д-р тогда? – user3195396
Вы пробовали это? – Nacho