从数据库获取价值到标签访问数据库的问题

时间:2018-12-17 14:18:10

标签: c# html asp.net select

大家好,我需要在数据库中的标签上显示单词,但是我遇到了问题,它对我不起作用,并且不显示单词... 我正在使用Select from并且它没有显示...

        string connectionStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\WordssTable.mdb;
    Persist Security Info=False";
    OleDbConnection connectObj = new OleDbConnection(connectionStr);
    connectObj.Open();
    OleDbCommand command = new OleDbCommand();
    command.Connection = connectObj;
    command.CommandText = "SELECT * FROM WordsTable WHERE EnglishWord='" + hebw1.Text + "'";
    command.ExecuteNonQuery();

2

    <form runat="server">
<div id = "Words">
<asp:Label runat="server" id="Engw"></asp:Label>
<br />
<asp:Label id="hebw1" runat="server"></asp:Label>
<asp:Label id="hebw2" runat="server"></asp:Label>
<asp:Label id="hebw3" runat="server"></asp:Label>
<asp:Label id="hebw4" runat="server"></asp:Label>
</div>
</form>

2 个答案:

答案 0 :(得分:1)

首先要做的是在一次性物品周围使用适当的using statement。然后,更改命令文本以使用参数占位符(@text)代替字符串串联。 (这绝对不能被低估。参数可以防止解析错误和sql注入hack)

现在您将被请求一个OleDbDataReader,并用它来填充来自 OleDbDataReader

的第一条记录的数据。
Option Explicit

    Sub Test()

        'In "with statement" we include the name of workbook & the name of worksheet we want to execute the code.
        With ThisWorkbook.Worksheets("Sheet1")

            'Using cell referencing
            .Cells(1, 1).Interior.Color = vbRed

            'Using range referencing
            .Range("A1").Interior.Color = vbRed

        End With

    End Sub

我假设您的记录包含读者索引器使用的列 hebwX ,并且这些字段都不为空。

答案 1 :(得分:0)

您的麻烦在于,您调用command.ExecuteNonQuery(),它仅返回受影响的行数。 您需要将其更改为

command.CommandText = "SELECT * FROM WordsTable WHERE EnglishWord='" + hebw1.Text + "'";
OleDbReader reader = command.ExecuteReader();
while(reader.Read()){
  // set your label values here
}