.


:




:

































 

 

 

 


1. ³ DropDownLst SqlDataSourc.




ᒺ , , runat, ( tovar, MSSQLServer) :

 

<asp:SqlDataSource ID="Source1" runat="server"

ConnectionString="Data Source=KATYAN\SQLEXPRESS;Initial Catalog= tovar; Integrated Security=True"

ProviderName="System.Data.SqlClient" SelectCommand="SELECT [kod_tov], [nazv], [gost] FROM [tovar]">

</asp:SqlDataSource>

 

, tovar , DropDownLst. , . ᒺ DropDownLst ᒺ SqlDataSourc :

 

<asp:DropDownList ID="drop1" runat="server" DataSourceID="Source1" DataTextField="nazv">

</asp:DropDownList>

 

(.3.1):

. 3.1. DropDownLst

 

2. ³ SqlDataSourc. 1 SqlDataSourc Source1. BulletedList. , . ᒺ BulletedList ᒺ SqlDataSourc :

<asp:BulletedList ID="BulletedList1" runat="server" BulletStyle="UpperRoman" DataSourceID="Source1" DataTextField="gost" DataValueField="gost">

</asp:BulletedList>

 

(. 3.2).

 

. 3.2. BulletedList

3. ᒺ Repeater. , ᒺ Repeater tovar SqlDataSourc, 1 Source1. , ᒺ Repeater. , temTemplate. HeaderTemplate Repeater, FooterTemplate , SeparatorTemplate , AlternatngtemTemplate . , , , HTML. , < table > HeaderTemplate, FooterTemplate.

䳿 DataBndng Repeater Contaner, tem, . tem Datatem. Contaner.Datatem. ᒺ Repeater :

<asp:Repeater ID="rep" runat="server" DataSourceID="Source1">

<HeaderTemplate><asp:Label runat="server" ID="head" BackColor= "DarkGreen" ForeColor="ActiveCaptionText" Font-Bold="true" Font-Underline="true" Font-Size="Large"> </br> </asp:Label>

<table>

</HeaderTemplate>

<ItemTemplate>

<tr>

<td> :

</br> <%#DataBinder.Eval(Container.DataItem,"nazv") %> </br>

:

</br> <%#DataBinder.Eval(Container.DataItem,"gost") %> </br><hr />

</td>

</tr>

</ItemTemplate>

<FooterTemplate>

</table>

</FooterTemplate>

</asp:Repeater>

(. 3.3):

. 3.3. Repeater

 

4. ᒺ DataList. DataLst , Repeater, . . , , , . SelectedtemTemplate EdttemTemplate. ᒺ DataList ᒺ SqlDataSourc, 1 Source1 .

 

<asp:DataList id="DataList1" runat="server" BorderColor="black" BorderWidth= "1" GridLines="Both" CellPadding="3" Font-Names="Verdana" Font-Size="8pt" Width="250px" HeaderStyle-BackColor="#aaaadd" AlternatingItemStyle-BackColor ="Gainsboro" SelectedItemStyle-BackColor="yellow" DataSourceID="Source1">

<HeaderTemplate> : </HeaderTemplate>

<ItemTemplate>

<%#DataBinder.Eval(Container.DataItem,"nazv") %>

<%#DataBinder.Eval(Container.DataItem,"gost") %>

</ItemTemplate>

</asp:DataList>

(. 3.4):

. 3.4. DataLst

5. ᒺ GridView. GrdVew , DataGrd. GrdVew ' , , , , , , ' - . ³ , , .

ᒺ GrdVew Columns, BoundField. DataField , GrdVew. , ᒺ GrdVew tovar SqlDataSourc, 1 Source1. :

 

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"

DataKeyNames="kod_tov" DataSourceID="Source1" EmptyDataText="There are no data records to display." Font-Bold="True" Font-Size="Medium">

<Columns>

<asp:BoundField DataField="nazv" HeaderText=" " SortExpression="nazv" />

<asp:BoundField DataField="gost" HeaderText="" SortExpression="gost" />

</Columns>

</asp:GridView>

 

(. 3.5). AllowSortng , . DataGrd, SortCommand. . 3.6 AllowSortng.

 

. 3.5. DataLst

 

 

. 3.6

 

ϳ . - , , . . , ² .

䳿 Sortng, 䳿 Sortng :

 

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)

{ string oldExpression = GridView1.SortExpression;

string newExpression = e.SortExpression;

if (oldExpression.IndexOf(newExpression) < 0)

{

if (oldExpression.Length > 0)

e.SortExpression = oldExpression + ", " + newExpression;

else

e.SortExpression = newExpression;

}

else

{ e.SortExpression = oldExpression;

}

}

. 3.7.

 

. 3.7. -

 

, .

< Columns > <asp:CommandFeld/>. ' " Delete ", " Edt ". :

 

<asp:CommandField ShowEditButton="True" ShowDeleteButton="True" />

 

ᒺ GrdVew (. 3.8):

 

. 3.8. GrdVew ShowEditButton ShowDeleteButton

 

ButtonType mage, . , . , , (. 3.9), (. 3.10) (. 3.11), .

 

. 3.9

 

. 3.10 . 3.11

 

ᒺ GrdVew ᒺ SqlDataSourc. ᒺ SqlDataSourc. , :

 

InsertCommand="INSERT INTO [tovar]([nazv], [gost]) VALUES (@nazv, @gost)"

 

. . ' . . Parameter: CookeParameter cooke, FormParameter , QuerystrngParameter , ProfleParameter SessonParameter . , ControlParameter, , , :

 

<InsertParameters>

<asp:ControlParameter ControlID ="TextBox1" Name="nazv" Type="String" />

<asp:ControlParameter ControlID ="TextBox2" Name="gost" Type="String" />

</InsertParameters>

 

, 2 TextBox1 TextBox2 . , , , . , :

 

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>

<asp:Button ID="Button1" runat="server" Text="Button" onclick= "Button1_Click" />

 

(. 3.12).

 

. 3.12. -

 

䳿 , , InsertCommand:

 

Source1.Insert();

 

. 3.13.

 

. 3.13

 

:

DeleteCommand="DELETE FROM [tovar] where [kod_tov]=@kod_tov"

UpdateCommand="UPDATE [tovar]SET [nazv]=@nazv, [gost]=@gost WHERE [kod_tov]=@kod_tov">

<UpdateParameters>

<asp:Parameter Name="nazv" Type="String" />

<asp:Parameter Name="gost" Type="String" />

<asp:Parameter Name="kod_tov" Type="Int32" />

</UpdateParameters>

<DeleteParameters>

<asp:Parameter Name="kod_tov" Type="Int32" />

</DeleteParameters>

 

, :

 

Source1.Update();

Source1.Delete();

 

6. ᒺ DetailsView. ³ , , - . GrdVew, DetalsVew , ' . nserttemTemplate, GrdVew. . , ᒺ DetailsVew tovar SqlDataSourc, 1 Source1. :

 

<asp:DetailsView ID="DetailsView1" runat="server" DataSourceID="Source1" Height="50px" Width="561px" CellPadding="4" GridLines="None" ForeColor= "#333333" HeaderText="" AutoGenerateRows="False" DataKeyNames= "kod_tov" AutoGenerateInsertButton="True">

<Fields>

<asp:BoundField DataField="nazv" HeaderText="" SortExpression= "nazv" />

<asp:BoundField DataField="gost" HeaderText="" SortExpression="gost" />

</Fields>

<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

<EditRowStyle BackColor="#2461BF" />

<RowStyle BackColor="#EFF3FB" />

<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />

<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

<AlternatingRowStyle BackColor="White" />

<CommandRowStyle BackColor="#D1DDF1" Font-Bold="True" />

<FieldHeaderStyle BackColor="#DEE8F5" Font-Bold="True" />

</asp:DetailsView>

 

(. 3.14). ϳ (. 3.15), , . , ᒺ SqlDataSourc .

 

. 3.14. -

 

. 3.15. -

7. Web . , . ' Connecton System.Data.SqlClent SQL Server System.Data.OleDb , Access. MSSQLServer :

 

string connectionString;

connectionString = "Data Source=KATYAN\\SQLEXPRESS;Initial Catalog= tovar; Integrated Security=True";

 

MSSQLServer, SqlConnection, , :

 

SqlConnection con = new SqlConnection(connectionString);

 

' Open ' Connecton. , ' '. ' , 䳿 :

 

protected void Page_Load(object sender, EventArgs e)

{

string connectionString;

connectionString = "Data Source=KATYAN\\SQLEXPRESS; Initial Catalog=tovar; Integrated Security=True";

SqlConnection con = new SqlConnection(connectionString);

con.Open();

Label1.Text = "<b>:</b>" + con.ServerVersion;

Label1.Text += "</br> <b>:</b>" + con.ToString();

}

, , ' . ' , ', web.confg, :

<connectionStrings>

<add name="connectionString" connectionString="Data Source= KATYAN\SQLEXPRESS; Initial Catalog=tovar;Integrated Security=True" providerName="System.Data.SqlClient" />

</connectionStrings>

, 璺 web.confg WebConfigurationManager, System.Web.Configuration. 䳿 :

 

protected void Page_Load(object sender, EventArgs e)

{

string connectionString;

string connectionString = WebConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;

SqlConnection con = new SqlConnection(connectionString);

con.Open();

Label1.Text = "<b>:</b>" + con.ServerVersion;

Label1.Text += "</br> <b>:</b>" + con.ToString();

}

, ' , try catch, . .

try

{

con.Open();

Label1.Text = "<b>:</b>" + con.ServerVersion;

Label1.Text += "</br> <b>:</b>" + con.ToString();

}

catch (Exception ex)

{ Label1.Text = " ";

Label1.Text += ex.Message;

}

finally

{

con.Close();

Label1.Text += "</br><b>:</b>";

Label1.Text += con.State.ToString();

}

' ', Web . ' , ' , , , 䳿 ' . , , ' - ' . , , fnally - , , ' . (. 3.16).

 

. 3.16.

 

8. ³ . ' . , , . . ADO.NET, - SQL Command. , Command , SQL ' ' . 3 Command (. 3.1).

 

. 3.1. Command

CommandType.Text   SQL, CommandText. .
CommandType. StoredProcedure , ' CommandText
CommandType. TableDrect 񳺿 , ' CommandText. OLE DB SQL Server.

 

' Command SQL .

 

SqlCommand cmd_SQL = new SqlCommand("Select * From tovar ",sqlCon);

cmd_SQL.CommandType = CommandType.Text;

SqlCommand cmd_Proc=new SqlCommand("GetGoods",sqlCon); cmd_Proc.CommandType = CommandType.StoredProcedure;

 

' DataReader . .

SqlDataReader rdr_SQL = cmd_SQL.ExecuteReader();

StringBuilder strResult = new StringBuilder("");

while (rdr_SQL.Read())

{

strResult.Append("<li>");

strResult.Append(" <b>");

strResult.Append(rdr_SQL["kod_tov"]);

strResult.Append("</b>, <b>");

strResult.Append(rdr_SQL.GetString(1));

strResult.Append("</b>");

strResult.Append(", <b>");

strResult.Append(rdr_SQL.GetString(2));

strResult.Append("</b></li>");

}

rdr_SQL.Close(); Label2.Text = strResult.ToString();

 

, . , , , . ϳ ' DataReader .

StrngBulder, (using System.Text). ҳ . . 3.17.

 

. 3.17.

 





:


: 2017-02-28; !; : 244 |


:

:

- , 20 40 . - .
==> ...

1735 - | 1679 -


© 2015-2024 lektsii.org - -

: 0.142 .