Hallo zusammen,
Ich habe hier mal den Quellcode für eine simple ASP.Net Webseite welche eine SQL Abfrage in einer Tabelle darstellt.
In der web.config ein ConnectionStrings Abschnitt hinzufügen
<system.web>
...
</system.web>
<appSettings>
...
</appSettings>
<connectionStrings>
<add name="mConnstring" connectionString="Data Source=ICESRV02;Database=db_home_icewolf;Uid=user;Password=secret;"/>
</connectionStrings>
So könnte dann die test.aspx aussehen:
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.Common" %>
<%@ Import Namespace="System.Data.sqlclient" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
'###########################################################################
'# Suche
'###########################################################################
Protected Sub cmdSuche_Click(ByVal sender As Object, ByVal e As System.EventArgs)
SqlDataSource1.SelectParameters.Clear()
SqlDataSource1.SelectCommand = "SELECT * FROM tBooks WHERE fTitle like @Suche"
Dim myP As New Parameter("Suche", TypeCode.String)
myP.DefaultValue = "%" & txtSuche.Text & "%"
SqlDataSource1.SelectParameters.Add(myP)
GridView1.DataBind()
End Sub
'###########################################################################
'# Clear Search
'###########################################################################
Protected Sub cmdClear_Click(ByVal sender As Object, ByVal e As System.EventArgs)
txtSuche.Text = ""
SqlDataSource1.SelectParameters.Clear()
SqlDataSource1.SelectCommand = "SELECT * FROM [tBooks] ORDER BY fDATUM DESC"
GridView1.DataBind()
End Sub
'##############################################################################
'# Count
'##############################################################################
Protected Sub SqlDataSource1_Selected(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs)
lblAnzahl.Text = e.AffectedRows.ToString
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<h2>Stichwortsuche</h2>
<asp:TextBox ID="txtSuche" runat="server"></asp:TextBox>
<asp:Button ID="cmdSuche" runat="server" Text="Suche" OnClick="cmdSuche_Click" />
<asp:Button ID="cmdClear" runat="server" Text="Clear" OnClick="cmdClear_Click" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowPaging="True"
AllowSorting="True" PageSize="20" GridLines="None" EmptyDataText="Sorry! Keine entsprechenden Datensätze gefunden"
Width="760px" CssClass="Grid" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="fDatum" HeaderText="Datum" DataFormatString="{0:d}" HtmlEncode="False" SortExpression="fDate" />
<asp:BoundField DataField="fTitle" HeaderText="Title" SortExpression="fTitle" />
<asp:BoundField DataField="fAuthor" HeaderText="Author" SortExpression="fAuthor" />
</Columns>
</asp:GridView>
<p>Anzahl Datensätze:
<asp:Label ID="lblAnzahl" runat="server" Text="lblAnzahl"></asp:Label>
</p>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
SelectCommand="SELECT [fID], [fTitle], [fAuthor], [fISBN],[fBewertung], [fDatum] FROM [tBooks] ORDER BY fDatum DESC"
ConnectionString="<%$ ConnectionStrings:mConnstring %>" OnSelected="SqlDataSource1_Selected" />
</form>
</body>
</html>