我想将Access数据库中的数据添加到label
。使用FORM1
上的查询搜索数据。
OleDbConnection connection = new OleDbConnection();
connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Ty\Documents\Uni\Level6\Project\App\software\Databases\Boards.accdb;Persist Security Info=False;";
connection.Open();
OleDbCommand commandBoards = new OleDbCommand();
commandBoards.Connection = connection;
string queryBoardsLength = "select * from [Boards] where [Length] >=" + TxtLength.Text;
string queryBoardsWidth = "select * from [Boards] where [Width] >=" + TxtWidth.Text;
commandBoards.CommandText = queryBoardsLength;
commandBoards.CommandText = queryBoardsWidth;
OleDbDataReader readerBoards = commandBoards.ExecuteReader();
while (readerBoards.Read())
{
ComBoards.Items.Add(readerBoards["ID"].ToString());
}
这会在FORM1
上填充一个包含搜索结果的组合框。然后,当选择它时,我将组合框中的选定结果设置为变量。
private void ComBoards_SelectedIndexChanged(object sender, EventArgs e)
{
Boards = this.ComBoards.GetItemText(this.ComBoards.SelectedItem);
}
在FORM2
我运行了类似的查询,根据所选的ID
从Access数据库中收集确切的数据。我想将长度显示为label
。
private void Form2_Load(object sender, EventArgs e)
{
lblBoardname.Text = Form1.Boards;
OleDbConnection connectionBoards = new OleDbConnection();
connectionBoards.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Ty\Documents\Uni\Level6\Project\App\software\Databases\Boards.accdb;Persist Security Info=False;";
connectionBoards.Open();
OleDbCommand commandBoardsget = new OleDbCommand();
commandBoardsget.Connection = connectionBoards;
string queryBoardsget = "select * from [Boards] where [ID] =" + lblBoardname;
commandBoardsget.CommandText = queryBoardsget;
OleDbDataReader readerBoardsget = commandBoardsget.ExecuteReader();
while (readerBoardsget.Read())
{
lblBoardsizel.Text = readerBoardsget["Length"].ToString();
}
除Reader
FORM2
之外的所有内容都有效,我收到以下错误:
查询表达式中的语法错误(逗号)' [ID] = System.Windows.Forms.Label,Text:Rhino Boards'。
我真的很感激一些帮助。感谢。