ASP.NET/SQL Server 2005数据库交互:本地主机

ASP.NET和SQL Server 2005数据库的交互包括:

语言: ASP.NET 连接: SQL Server 2005 前言:这种数据库连接不是天才,它是一个简单的连接,带有SQL语句和VB代码。 它将需要VS Web Dev 2005 Express平台的一些知识才能使本文更有意义。 提示:访问/下载免费软件和视频以熟悉这些VS Web Dev 2005版本,​​一个朋友向我介绍了此内容:-)

如果您安装了Visual Studio 2005:

(1)启动一个新的ASP.NET网站

(2)给出选择的文件夹名称

(3)接受所有默认设置,包括添加母版页

(4)好的

(5)添加一个表单,将其命名为DataCentral

(6)添加6个文本框和一个按钮

(7)添加一个SQL数据库,将其命名为SQLDB,将其添加到App_Data文件夹中

(8)仅允许在此数据库中返回的IP地址为Null

(9)在表中一起添加6个字段(阅读代码以弄清楚如何命名字段)

(7)通过右键单击数据库资源管理器中的表来添加表

(8)保存并退出数据库设计屏幕

 
Protected Sub Add2SQLDB_Click(ByVal sender As Object, ByVal e As System.EventArgs) 
        Dim SQLDBDataSource  As New SqlDataSource
        SQLDBDataSource .ConnectionString = ConfigurationManager.ConnectionStrings("SQLDBConnectionString1").ToString
        SQLDBDataSource .InsertCommandType = SqlDataSourceCommandType.Text
        SQLDBDataSource .InsertCommand = "INSERT INTO LIBRARY(EmailAddress, IPAddress, DateTimeStamp, itemPrice, itemName, typeApartment, crimeRate1, crimeRate2) VALUES(@EmailAddress,@IPAddress, @DateTimeStamp, @itemPrice, @itemName, @typeApartment, @crimeRate1, @crimeRate2)"
        SQLDBDataSource .InsertParameters.Add("EmailAddress", emailAddressTextBox.Text)
        SQLDBDataSource .InsertParameters.Add("IPAddress", Request.UserHostAddress.ToString())
        SQLDBDataSource .InsertParameters.Add("DateTimeStamp", DateTime.Now())
        SQLDBDataSource .InsertParameters.Add("itemPrice", itemPriceTextbox.Text)
        SQLDBDataSource .InsertParameters.Add("itemName", itemNameTextbox.Text)
        SQLDBDataSource .InsertParameters.Add("typeApartment", typeApartmentTextbox.Text)
        SQLDBDataSource .InsertParameters.Add("crimeRate1", crimeRate1Textbox.Text)
        SQLDBDataSource .InsertParameters.Add("crimeRate2", crimeRate2Textbox.Text)         
        Dim rowsAffected As Integer = 0
        Try
            rowsAffected = SQLDBDataSource.Insert()
        Catch ex As Exception
            Server.Transfer("Error_Submitting.aspx") 
        Finally
            SQLDBDataSource = Nothing
        End Try 
        If rowsAffected <> 1 Then           
            Server.Transfer("Error_Submitting.aspx")
        Else
            Server.Transfer("OK_Submitting.aspx")
        End If   
    End Sub
</script> 
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Admin Apartment Log<br />
    <br />
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp;
    Email Address:<asp:TextBox ID="emailAddressTextBox" runat="server"></asp:TextBox>
    <asp:RegularExpressionValidator ID="regExValidator" runat="server" ControlToValidate="emailAddressTextBox"
        ErrorMessage="RegularExpressionValidator" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">*</asp:RegularExpressionValidator><br />
    <br />
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Price:
    <asp:TextBox ID="itemPriceTextbox" runat="server"></asp:TextBox><br />
    <br />
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp; &nbsp; &nbsp; ItemName:
    <asp:TextBox ID="itemNameTextbox" runat="server"></asp:TextBox><br />
    <br />
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Type:
    <asp:TextBox ID="typeApartmentTextbox" runat="server"></asp:TextBox><br />
    <br />
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    Crime Rate # 1:
    <asp:TextBox ID="crimeRate1Textbox" runat="server"></asp:TextBox><br />
    <br />
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    Crime Rate # 2:
    <asp:TextBox ID="crimeRate2Textbox" runat="server"></asp:TextBox><br />
    <br />
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp; &nbsp; &nbsp;
    <asp:Button ID="Button1" runat="server" Height="26px" Text="Submit" Width="101px" OnClick="Button1_Click" /><br />
    <br />
    <asp:ValidationSummary ID="ValidationSummary1" runat="server" DisplayMode="SingleParagraph" Width="224px" />
    <br />
</asp:Content>   
请为电子邮件字段设置验证器,以防伪造电子邮件。
参见下一页 母版页

(1)主页包含其他网站页面的外观

(2)母版页可用于更改页面的颜色或背景


<%@ Master Language="VB" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<script runat="server"> 
</script> 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Master Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
        </asp:contentplaceholder>
    </div>
    </form>
</body>
</html> 
查看数据页下一页... 查看数据页

(1)右键单击项目路径

(2)新增项目

(3)选择Web表格

(4)称呼它

OK_提交

(5)包括对母版页说“是”

(6)再次右键单击项目路径

(7)点击添加新项

(8)选择数据集

(9)随便命名

(a)弹出窗口时说是

(b)您的数据库现在应该在下拉列表中列出

(c)选中所有默认值,下一步,下一步,下一步

(d)当要求键入查询时,请使用“查询生成器”代替

(e)添加此项目中正在使用的数据库表

(f)在“查询”构建器视图中选择所有列

(g)说确定,然后完成

(h)回到

OK_提交页面

(i)选择设计视图

(j)在数据下方,从左侧拖动一个Gridview(默认为Gridview1)

(k)使用选择数据源绑定到数据源

(l)在“数据源配置”向导中选择“对象”

(m)从下拉列表绑定到YourTableNameTableAdapter选项

(n)点击下一步,如果尚未选择,请在下拉菜单中仅选择选项

(o)点击下一步,然后点击完成

现在您可以在下面添加代码...

<%@ Page Language="VB" MasterPageFile="~/DashMaster.master" Title="Thank You Page" %> 
<script runat="server"> 
Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) 
End Sub 
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        If Button1.Enabled Then
            Server.Transfer("SubmitPage.aspx")
            'Else
            'Server.Transfer("OK_Submitting.aspx")
        End If
    End Sub
</script> 
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    Why, Thank you!<br />
    <br />
    Please take a look at your submissions<br />
    <br />
    &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
        AutoGenerateColumns="False" DataSourceID="ObjectDataSource1" Width="231px" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
        <Columns>
            <asp:CommandField ShowSelectButton="True" />
            <asp:BoundField DataField="EmailAddress" HeaderText="EmailAddress" SortExpression="EmailAddress" />
        </Columns>
    </asp:GridView>
    &nbsp;<br />
    &nbsp;&nbsp;<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Go Back to Add more Data"
        Width="205px" /><br />
    &nbsp; &nbsp; &nbsp;<br />
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" OldValuesParameterFormatString="original_{0}"
        SelectMethod="GetData" TypeName="DataSet1TableAdapters.EmailTableAdapter">
    </asp:ObjectDataSource>
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br />
    &nbsp;<br />
    <br />
    &nbsp;
    <br />
</asp:Content>  
下一个问题提交页面...下一个 问题提交页面

(1)右键单击项目路径

(2)新增项目

(3)选择Web表格

(4)称呼它

错误提交

(5)包括对母版页说“是”

(6)再次右键单击项目路径

(7)在Error_Submitting表单上拖动一个按钮

(8)默认情况下称为Button1

(9在下面添加代码

代码允许用户返回并修复问题...

<%@ Page Language="VB" MasterPageFile="~/DashMaster.master" Title="Problem Page" %> 
<script runat="server"> 
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        If Button1.Enabled Then
            Server.Transfer("SubmitPage.aspx")
        End If
    End Sub
</script> 
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    There was a problem...<br />
    <br />
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Go back and Fix" />
</asp:Content>  
有关此和其他ASP.NET示例的更多信息,请通过“入门”访问Microsoft的ASP.NET主页。官方Microsoft ASP_NET网站 http://www.asp.net/get-started/

ASP.NET,结束...

From: https://bytes.com/topic/asp-net/insights/762010-asp-net-sql-server-2005-database-interaction-localhost

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值