我试图删除用户在C#WinForms应用程序的DataGridView中选择的行(已连接到已经有记录的本地数据库)。
我已经实现了下面的代码,并且没有错误,但是删除从未被执行(即使消息框显示记录已被“删除”,但事实并非如此,因为记录保留在DataGrid中)
请注意,删除功能在搜索功能内-请参见以下代码。
public void DisplayData()
{
string ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename= C:\Program Files\Microsoft SQL Server\MSSQL14.SQLEXPRESS\MSSQL\DATA\Library System Project.mdf ;Integrated Security=True;Connect Timeout=30";
string Query = "select * from Customers";
SqlConnection DBCon = new SqlConnection(ConnectionString);
SqlCommand DBCommand = new SqlCommand(Query, DBCon);
SqlDataReader DBReader;
try
{
DBCon.Open();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(DBCommand);
da.Fill(dt);
dgv_CustomerDetails.DataSource = dt;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
// *** If you're going to be opening a connection be sure to close it ***
// *** Finally blocks work well for this ***
DBCon.Close();
}
}
private void SearchCustomerRecordForm_Load(object sender, EventArgs e)
{
DisplayData();
}
private void btnDeleteCustomerRecord_Click(object sender, EventArgs e)
{
string ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename= C:\Program Files\Microsoft SQL Server\MSSQL14.SQLEXPRESS\MSSQL\DATA\Library System Project.mdf ;Integrated Security=True;Connect Timeout=30";
int rowIndex = dgv_CustomerDetails.CurrentCell.RowIndex;
string Query = "delete from Customers where CustomerName = '"+ dgv_CustomerDetails.CurrentCell(rowIndex) +"'";
SqlConnection DBCon = new SqlConnection(ConnectionString);
SqlCommand DBCommand = new SqlCommand(Query, DBCon);
SqlDataReader DBReader;
try
{
DBCon.Open();
DBReader = DBCommand.ExecuteReader();
MessageBox.Show("Customer record removed from the system.", "Library System", MessageBoxButtons.OK);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
// *** If you're going to be opening a connection be sure to close it ***
// *** Finally blocks work well for this ***
DBCon.Close();
DisplayData();
}
}
答案 0 :(得分:0)
您确定查询创建良好吗?似乎该行中缺少单元格值属性:
driver.findElement(By.xpath("THE VALUE FROM DROPDOWN")).click();
尝试查看Query变量的值,因为它已建立。