所以我根据点击的图像来显示访问数据库中的数据。我已经提出了两种不同的方法来做到这一点,我很好奇哪种方式更有效率。
以下是我目前正在做的两种不同方式。如果你能推荐一个更好的解决方案,我很乐意听到它。
解决方案1使用绑定到访问数据库的datagridview。
private void risk1_Click(object sender, EventArgs e)
{
pointid.Text = "7";
int numval = Convert.ToInt32(pointid.Text);
//To get the correct row
int id = numval - 1;
//Point Id of the clicked point
textBox1.Text = id.ToString();
pointlbl.Text = dataGridView1.Rows[id].Cells[2].Value.ToString();
//Cat ID of the clicked point
catId.Text = dataGridView1.Rows[id].Cells[5].Value.ToString();
webBrowser1.DocumentText = dataGridView1.Rows[id].Cells[4].Value.ToString();
webBrowser1.Document.ExecCommand("SelectAll", false, null);
webBrowser1.Document.ExecCommand("Copy", false, null);
using (RichTextBox box = new RichTextBox())
{
box.Paste();
box.Rtf = box.Rtf.Replace("<>", "");
this.htmlRichTextBox1.Rtf = box.Rtf;
}
pointPanel.Visible = true;
homePanel.Visible = false;
}
解决方案2使用select语句
private void risk2_Click(object sender, EventArgs e)
{
pointid.Text = "2";
int numval = Convert.ToInt32(pointid.Text);
//Setup Connection to access db
string cnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Adam\\Desktop\\braithwaite.mdb";
//declare Connection, command and other related objects
OleDbConnection conGet = new OleDbConnection(cnString);
OleDbCommand cmdGet = new OleDbCommand();
//try
//{
//open connection
conGet.Open();
//String correctAnswer;
cmdGet.CommandType = CommandType.Text;
cmdGet.Connection = conGet;
cmdGet.CommandText = "SELECT * FROM Points WHERE point_id = "+numval+"";
OleDbDataReader reader = cmdGet.ExecuteReader();
reader.Read();
pointlbl.Text = reader["point"].ToString();
webBrowser1.DocumentText = reader["Description"].ToString();
conGet.Close();
pointPanel.Visible = true;
homePanel.Visible = false;
}