Asp.net 在Visual Studio中选择对Access数据库的查询
这是我的密码:Asp.net 在Visual Studio中选择对Access数据库的查询,asp.net,sql,visual-studio-2010,ms-access,select,Asp.net,Sql,Visual Studio 2010,Ms Access,Select,这是我的密码: Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load ltRooms.Text = CInt(Session("numOfRooms")) 'Calculate total cost Dim intPrice As Integer Dim intTotal As Integer intPrice = Access
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
ltRooms.Text = CInt(Session("numOfRooms"))
'Calculate total cost
Dim intPrice As Integer
Dim intTotal As Integer
intPrice = AccessDataSource1.SelectCommand = "SELECT [Price] FROM [Rooms] WHERE ([RoomType] = 'RoomType')"
intTotal = intPrice * intRooms
ltPrice.Text = intTotal.ToString
End Sub
还有我的数据源
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="Hostel.accdb"
SelectCommand="SELECT * FROM [Bookings]"></asp:AccessDataSource>
我试图存储select查询中的值,然后使用它计算出总价,然后将其存储在文本中。到目前为止,我只得到0分。没有编译错误
有人知道为什么这不起作用吗?为了清晰起见,我正在将各种评论整合成一个答案,以阻止页面抱怨讨论时间过长。这完全是我的想法,所以代码中可能有一些错误。不过,这应该足以指出问题所在 首先,现在我再看一遍,我认为您的代码只是设置数据源的属性,而不是实际查询数据库。我认为你需要使用这种方法 您的代码可能最终看起来像这样:
AccessDataSource1.SelectCommand = "SELECT [Price] FROM [Rooms] WHERE ([RoomType] = '" & RoomType & "')"
Dim intPrices As List(Of Integer) = AccessDataSource1.Select(DataSourceSelectArguments.Empty)
' Now do stuff with your price data.
如果上述方法无效,那么我将检查Select调用返回的intPrice
值。您还应该检查RoomType设置是否正确
如果从数据库返回错误的数据,那么您应该能够修复SQL查询以检索正确的数据。如果您需要进一步的帮助,请发布SQL查询和表结构
如果返回了正确的数据,请检查定义了
intRooms
的位置。如果为零,则无论SELECT命令返回的intPrice值是多少,您的总计都将计算为零?如果它是错误的,那么我将检查传递给查询的RoomType的值。它是0,因此它甚至无法从数据库检索值。RoomType是一个字符串变量,这是将变量传递给select语句的正确方法吗?我想您需要将select命令的格式设置为这样:“从[Rooms]选择[Price]其中([RoomType]='“&RoomType&')”。如果在起始“intTotal=”的行上放置断点并运行应用程序,您应该能够看到SQL查询是什么。为注释干杯,仍然得到0。我也放了一个断点,但是没有给我想要的信息。你是对的,我需要数据源。选择的方法也是。谢谢你的帮助。