我使用相同的按钮" Save"更新名为AnalysisExperiments的表,并将数据插入名为" Analysis"的表中。但更新无效。这是代码:
#region Insert Data into Analysis Table
if (checkIfRepeatedJobNumber(tbJobNumber.Text.Trim()))
{
MessageBox.Show("Job Number is already exist.", "Repeated Data");
return;
}
string query = "insert into Analysis (ID, WellName, EstimatedStartDate, SOWComments, ProgressComments, Field, FocalPoint, StudyCompleted, Company, ReservoirPressure, ReservoirTemp, SelectedSamples) " +
"values (@ID, @WellName, @SamplingDate, @SOWComments, @ProgressComments, @Field, @FocalPoint, @StudyCompleted, @Company, @ReservoirPressure, @ReservoirTemp, @SelectedSamples)";
OleDbCommand cmd = new OleDbCommand(query, conn);
cmd.Parameters.AddWithValue("@ID", tbJobNumber.Text);
cmd.Parameters.AddWithValue("@WellName", tbWellName.Text);
cmd.Parameters.AddWithValue("@SamplingDate", dtpSamplingDate.Text);
cmd.Parameters.AddWithValue("@SOWComments", tbSOW_Comments.Text);
cmd.Parameters.AddWithValue("@ProgressComments", tbProgressComments.Text);
cmd.Parameters.AddWithValue("@Field", tbFieldName.Text);
cmd.Parameters.AddWithValue("@FocalPoint", tbFocalName.Text);
if (radYes.Checked)
cmd.Parameters.AddWithValue("@StudyCompleted", "Yes");
else
cmd.Parameters.AddWithValue("@StudyCompleted", "No");
cmd.Parameters.AddWithValue("@Company", tbCompany.Text);
cmd.Parameters.AddWithValue("@ReservoirPressure", tbReservoirPressure.Text);
cmd.Parameters.AddWithValue("@ReservoirTemp", tbReservoirTemp.Text);
cmd.Parameters.AddWithValue("@@SelectedSamples", tbSelectedSamples.Text);
try
{
conn.Open();
int j = cmd.ExecuteNonQuery();
if (j == 1)
{
MessageBox.Show("Done");
this.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
#endregion
#region update analysis Exp but still no working
#region Update database
try
{ int k = 0;
OleDbDataAdapter da;
da = new OleDbDataAdapter("select* from [AnalysisExperiments]", conn);
string ExpQuery = "update AnalysisExperiments set SampleNumber = @SampleNumber, Status = @Status where ID = '" + tbJobNumber.Text + "' and Experiment = '";
foreach (DataGridViewRow row in dgvExperiments.Rows)
{
ExpQuery += row.Cells["Experiment"].Value.ToString() + "'";
OleDbCommand updateCommand = new OleDbCommand(ExpQuery, conn);
updateCommand.Parameters.Add("@SampleNumber", OleDbType.VarWChar);
MessageBox.Show(row.Cells["SampleNumber"].Value.ToString() + " | " + row.Cells["Status"].Value.ToString() + " | " + row.Cells["Experiment"].Value.ToString());
updateCommand.Parameters["@SampleNumber"].Value = row.Cells["SampleNumber"].Value.ToString();
updateCommand.Parameters.Add("@Status", OleDbType.Boolean);
updateCommand.Parameters["@Status"].Value = row.Cells["Status"].Value;
da.UpdateCommand = updateCommand;
conn.Open();
k = da.UpdateCommand.ExecuteNonQuery();
conn.Close();
}
if (k == 1)
MessageBox.Show("Done");
else
{
MessageBox.Show("Nothing Updated!");
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
#endregion
#endregion
知道编译器跳过更新区域中的循环。
答案 0 :(得分:1)
在第二个循环中,查询语法变为无效,因为+ =将前一个文本与新文本连接
Dim newQuery = ExQuery + row.Cells["Experiment"].Value.ToString() + "'";
然后将新字符串用于ExecuteNonQuery。
这种方法仍有许多问题。您应该使用不同的方法分隔此代码,并将参数也用于最后一个值
答案 1 :(得分:0)
您是否尝试调试foreach行以查看 dgvExperiments 中是否有任何行,以及它们的类型(或子类型) DataGridViewRow ?