Asp.NET中数据库基本操作问题???(vb.net)(50分)

  • 主题发起人 主题发起人 halps
  • 开始时间 开始时间
H

halps

Unregistered / Unconfirmed
GUEST, unregistred user!
1.asp.net是将数据库查询的执行结果缓存在本地数据库中,查询完成以后怎么取得查询结果的记录总数?
2.如何将数据集中的数据读出来?就是将数据集中的某一个字段的值赋给控件(textbox...)
我刚刚开始用asp.net,很多问题不明白,请各位大侠多多指点!
 
下现是其中一种做法,VS.NET的代码
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="VBNET.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<asp:TextBox id="TextBox1" style="Z-INDEX: 101;
LEFT: 32px;
POSITION: absolute;
TOP: 64px" runat="server"
Width="168px" Height="40px" Rows="2" Text='<%# DataBinder.Eval(DataSet11, "Tables[Products].DefaultView.[0].ProductName") %>'>
</asp:TextBox>
<asp:Button id="Button1" style="Z-INDEX: 102;
LEFT: 232px;
POSITION: absolute;
TOP: 64px" runat="server"
Width="104px" Height="40px" Text="上一个"></asp:Button>
<asp:Button id="Button2" style="Z-INDEX: 103;
LEFT: 232px;
POSITION: absolute;
TOP: 120px" runat="server"
Width="104px" Height="40px" Text="下一个"></asp:Button>
<asp:Label id="Label1" style="Z-INDEX: 104;
LEFT: 40px;
POSITION: absolute;
TOP: 24px" runat="server"
Width="184px" Height="32px">Label</asp:Label>
<asp:Label id="Label2" style="Z-INDEX: 105;
LEFT: 280px;
POSITION: absolute;
TOP: 24px" runat="server"
Width="200px" Height="32px">Label</asp:Label></FONT>
</form>
</body>
</HTML>
//*************************************************************************//
Public Class WebForm1
Inherits System.Web.UI.Page
#Region " Web 窗体设计器生成的代码 "
'该调用是 Web 窗体设计器所必需的。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.SqlDataAdapter1 = New System.Data.SqlClient.SqlDataAdapter
Me.SqlSelectCommand1 = New System.Data.SqlClient.SqlCommand
Me.SqlConnection1 = New System.Data.SqlClient.SqlConnection
Me.DataSet11 = New VBNET.DataSet1
CType(Me.DataSet11, System.ComponentModel.ISupportInitialize).begin
Init()
'
'SqlDataAdapter1
'
Me.SqlDataAdapter1.SelectCommand = Me.SqlSelectCommand1
Me.SqlDataAdapter1.TableMappings.AddRange(New System.Data.Common.DataTableMapping() {New System.Data.Common.DataTableMapping("Table", "Products", New System.Data.Common.DataColumnMapping() {New System.Data.Common.DataColumnMapping("ProductID", "ProductID"), New System.Data.Common.DataColumnMapping("ProductName", "ProductName"), New System.Data.Common.DataColumnMapping("SupplierID", "SupplierID"), New System.Data.Common.DataColumnMapping("CategoryID", "CategoryID"), New System.Data.Common.DataColumnMapping("QuantityPerUnit", "QuantityPerUnit"), New System.Data.Common.DataColumnMapping("UnitPrice", "UnitPrice"), New System.Data.Common.DataColumnMapping("UnitsInStock", "UnitsInStock"), New System.Data.Common.DataColumnMapping("UnitsOnOrder", "UnitsOnOrder"), New System.Data.Common.DataColumnMapping("ReorderLevel", "ReorderLevel"), New System.Data.Common.DataColumnMapping("Discontinued", "Discontinued")})})
'
'SqlSelectCommand1
'
Me.SqlSelectCommand1.CommandText = "SELECT ProductID, ProductName, SupplierID, CategoryID, QuantityPerUnit, UnitPrice" &amp;
_
", UnitsInStock, UnitsOnOrder, ReorderLevel, Discontinued FROM Products"
Me.SqlSelectCommand1.Connection = Me.SqlConnection1
'
'SqlConnection1
'
Me.SqlConnection1.ConnectionString = "workstation id=MY;packet size=4096;integrated security=SSPI;data source=""MY/NetSD" &amp;
_
"K"";persist security info=False;initial catalog=Northwind"
'
'DataSet11
'
Me.DataSet11.DataSetName = "DataSet1"
Me.DataSet11.Locale = New System.Globalization.CultureInfo("zh-CN")
CType(Me.DataSet11, System.ComponentModel.ISupportInitialize).EndInit()
End Sub
Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents Button2 As System.Web.UI.WebControls.Button
Protected WithEvents SqlDataAdapter1 As System.Data.SqlClient.SqlDataAdapter
Protected WithEvents SqlConnection1 As System.Data.SqlClient.SqlConnection
Protected WithEvents DataSet11 As VBNET.DataSet1
Protected WithEvents SqlSelectCommand1 As System.Data.SqlClient.SqlCommand
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
Protected WithEvents Label2 As System.Web.UI.WebControls.Label
'注意: 以下占位符声明是 Web 窗体设计器所必需的。
'不要删除或移动它。
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
If Not Me.IsPostBack then
Me.DataBind()
Label1.Text = "一共 " &amp;
DataSet11.Products.Count &amp;
"条记录"
End If
End Sub
Private Sub Page_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.DataBinding
SqlDataAdapter1.Fill(DataSet11)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim inc As Integer = Me.ViewState.Item("inc")
If inc > 1 then
inc = inc - 1
End If
Me.ViewState.Item("inc") = inc
Me.SqlSelectCommand1.CommandText = Me.SqlSelectCommand1.CommandText &amp;
_
" where ProductID = " &amp;
inc
Label2.Text = "当前记录的ID是 " &amp;
inc
DataBind()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim inc As Integer = Me.ViewState.Item("inc")
inc = inc + 1
Me.ViewState.Item("inc") = inc
Me.SqlSelectCommand1.CommandText = Me.SqlSelectCommand1.CommandText &amp;
_
" where ProductID = " &amp;
inc
Label2.Text = "当前记录的ID是 " &amp;
inc
DataBind()
End Sub
End Class
 
很多代码是用SqlDataAdapter产生的,使用的数据库为Northwind,SQL Server中的一个
示例数据库
增加完SqlDataAdapter并使用向导产生SQL后,再使用SqlDataAdapter的"生成数据集"
生成Me.DataSet11 = New VBNET.DataSet1
Text='<%# DataBinder.Eval(DataSet11, "Tables[Products].DefaultView.[0].ProductName") %>'>
上面这段代码是使用TextBox1的DataBindings属性,再在向导中选择DataSet11,一直打开树
直到选择ProductName字段
 
vs.net自动产生的代码太死板,用起来不方便。
今天看到了用DataRead和Dataset都可以,不过很麻烦,我想知道有没有简单的??
 
前几天在用Web Matrix.
看过asp.net的教程,那里面有你需要的东西.
 
后退
顶部