我不熟悉使用 C# 作为前端来查看和编辑数据库。我可以使用下面的代码使用数据网格视图调出数据库查询并显示结果。
private void btnconnect_Click(object sender, EventArgs e)
{
dataGridView1.AllowUserToAddRows = false;
dataGridView1.Columns.Add("Accode", "Accode");
dataGridView1.Columns.Add("CaseNO", "Case NO");
dataGridView1.Columns.Add("Doccumentname", "Document Name");
dataGridView1.Columns.Add("CreatedDate", "Created Date");
string strFile = txtFileName.Text;
string strPass = "********";
OleDbConnection connect = new OleDbConnection();
connect.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strFile + ";Jet OLEDB:Database Password=" + strPass + ";";
connect.Open();
if (txtCaseNo.Text != "" && txtAccode.Text != "")
{
OleDbCommand command = new OleDbCommand();
command.Connection = connect;
command.CommandText = "SELECT CorrespondenceHistory.Accode, CorrespondenceHistory.CaseNo,CorrespondenceHistory.DocumentName, CorrespondenceHistory.CreatedDate FROM CorrespondenceHistory WHERE (((CorrespondenceHistory.CaseNo)=" + txtCaseNo.Text + "),((CorrespondenceHistory.Accode)=" + txtAccode.Text + "))";
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
dataGridView1.Rows.Add();
dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["Accode"].Value = reader[0].ToString();
dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["CaseNO"].Value = reader[1].ToString();
dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["Doccumentname"].Value = reader[2].ToString();
dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["CreatedDate"].Value = reader[3].ToString();
}
}
else
MessageBox.Show("Enter a search criteria", "Enter a search criteria");
connect.Close();
}
但我不确定如何让它将数据库表更新为数据网格视图中所做的任何更改。由于该程序将在多台计算机上使用(都具有相同的数据库和密码),因此数据库的位置会发生变化,因此在制作连接字符串时它会从文本框中获取。