将ACCESS数据库中的值设置为C#中的标签

时间:2017-03-10 10:43:38

标签: c# ms-access oledbconnection

我想将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'。

我真的很感激一些帮助。感谢。

0 个答案:

没有答案