1. How to create a Directory, see the following code:
string subPath ="ImagesPath"; // your code goes here
bool IsExists = System.IO.Directory.Exists(Server.MapPath(subPath));
if(!IsExists)
System.IO.Directory.CreateDirectory(Server.MapPath(subPath));
2. How to access SQL database, see the following code:
CREATE TABLE [dbo].[News](
[id] [bigint] IDENTITY(1,1) NOT NULL,
[title] [varchar](50) NULL,
[news] [varchar](50) NULL,
[imageurl] [varchar](20) NULL,
[detail] [varchar](5000) NULL,
CONSTRAINT [PK_News] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
[id] [bigint] IDENTITY(1,1) NOT NULL,
[title] [varchar](50) NULL,
[news] [varchar](50) NULL,
[imageurl] [varchar](20) NULL,
[detail] [varchar](5000) NULL,
CONSTRAINT [PK_News] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
Admin_News.aspx
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
public static string SqlConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["TakeawayConnectionString"].ToString();
public static void ExecuteNonQuery(string queryString)
{
using (SqlConnection connection = new SqlConnection(SqlConnectionString))
{
SqlCommand command = new SqlCommand(queryString, connection);
command.Connection.Open();
command.ExecuteNonQuery();
command.Connection.Close();
}
}
protected void btnAdd_Click(object sender, EventArgs e)
{
StringBuilder sqlInsert = new StringBuilder();
sqlInsert.Append("INSERT INTO [News]([title],[news],[imageurl],[detail])VALUES('");
sqlInsert.Append(txtTitle.Text);
sqlInsert.Append("','");
sqlInsert.Append(txtNews.Text);
sqlInsert.Append("','");
sqlInsert.Append(txtImageURL.Text);
sqlInsert.Append("','");
sqlInsert.Append(txtDetail.Text);
sqlInsert.Append("')");
ExecuteNonQuery(sqlInsert.ToString());
GridView1.DataBind();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Admin_News</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="id"
DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True"
ShowSelectButton="True" />
<asp:BoundField DataField="id" HeaderText="id" InsertVisible="False"
ReadOnly="True" SortExpression="id" />
<asp:BoundField DataField="title" HeaderText="title" SortExpression="title" />
<asp:BoundField DataField="news" HeaderText="news" SortExpression="news" />
<asp:BoundField DataField="imageurl" HeaderText="imageurl"
SortExpression="imageurl" />
<asp:BoundField DataField="detail" HeaderText="detail"
SortExpression="detail" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:TakeawayConnectionString %>"
DeleteCommand="DELETE FROM [News] WHERE [id] = @id"
InsertCommand="INSERT INTO [News] ([title], [news], [imageurl], [detail]) VALUES (@title, @news, @imageurl, @detail)"
SelectCommand="SELECT * FROM [News]"
UpdateCommand="UPDATE [News] SET [title] = @title, [news] = @news, [imageurl] = @imageurl, [detail] = @detail WHERE [id] = @id">
<DeleteParameters>
<asp:Parameter Name="id" Type="Int64" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="title" Type="String" />
<asp:Parameter Name="news" Type="String" />
<asp:Parameter Name="imageurl" Type="String" />
<asp:Parameter Name="detail" Type="String" />
<asp:Parameter Name="id" Type="Int64" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="title" Type="String" />
<asp:Parameter Name="news" Type="String" />
<asp:Parameter Name="imageurl" Type="String" />
<asp:Parameter Name="detail" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
<br />
<asp:TextBox ID="txtTitle" runat="server"></asp:TextBox>
<asp:TextBox ID="txtNews" runat="server"></asp:TextBox>
<br />
<asp:TextBox ID="txtImageURL" runat="server"></asp:TextBox>
<asp:TextBox ID="txtDetail" runat="server"></asp:TextBox>
<br />
<asp:Button ID="btnAdd" runat="server" onclick="btnAdd_Click" Text="Add"
Width="37px" />
<br />
<br />
</div>
</form>
</body>
</html>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
public static string SqlConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["TakeawayConnectionString"].ToString();
public static void ExecuteNonQuery(string queryString)
{
using (SqlConnection connection = new SqlConnection(SqlConnectionString))
{
SqlCommand command = new SqlCommand(queryString, connection);
command.Connection.Open();
command.ExecuteNonQuery();
command.Connection.Close();
}
}
protected void btnAdd_Click(object sender, EventArgs e)
{
StringBuilder sqlInsert = new StringBuilder();
sqlInsert.Append("INSERT INTO [News]([title],[news],[imageurl],[detail])VALUES('");
sqlInsert.Append(txtTitle.Text);
sqlInsert.Append("','");
sqlInsert.Append(txtNews.Text);
sqlInsert.Append("','");
sqlInsert.Append(txtImageURL.Text);
sqlInsert.Append("','");
sqlInsert.Append(txtDetail.Text);
sqlInsert.Append("')");
ExecuteNonQuery(sqlInsert.ToString());
GridView1.DataBind();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Admin_News</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="id"
DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True"
ShowSelectButton="True" />
<asp:BoundField DataField="id" HeaderText="id" InsertVisible="False"
ReadOnly="True" SortExpression="id" />
<asp:BoundField DataField="title" HeaderText="title" SortExpression="title" />
<asp:BoundField DataField="news" HeaderText="news" SortExpression="news" />
<asp:BoundField DataField="imageurl" HeaderText="imageurl"
SortExpression="imageurl" />
<asp:BoundField DataField="detail" HeaderText="detail"
SortExpression="detail" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:TakeawayConnectionString %>"
DeleteCommand="DELETE FROM [News] WHERE [id] = @id"
InsertCommand="INSERT INTO [News] ([title], [news], [imageurl], [detail]) VALUES (@title, @news, @imageurl, @detail)"
SelectCommand="SELECT * FROM [News]"
UpdateCommand="UPDATE [News] SET [title] = @title, [news] = @news, [imageurl] = @imageurl, [detail] = @detail WHERE [id] = @id">
<DeleteParameters>
<asp:Parameter Name="id" Type="Int64" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="title" Type="String" />
<asp:Parameter Name="news" Type="String" />
<asp:Parameter Name="imageurl" Type="String" />
<asp:Parameter Name="detail" Type="String" />
<asp:Parameter Name="id" Type="Int64" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="title" Type="String" />
<asp:Parameter Name="news" Type="String" />
<asp:Parameter Name="imageurl" Type="String" />
<asp:Parameter Name="detail" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
<br />
<asp:TextBox ID="txtTitle" runat="server"></asp:TextBox>
<asp:TextBox ID="txtNews" runat="server"></asp:TextBox>
<br />
<asp:TextBox ID="txtImageURL" runat="server"></asp:TextBox>
<asp:TextBox ID="txtDetail" runat="server"></asp:TextBox>
<br />
<asp:Button ID="btnAdd" runat="server" onclick="btnAdd_Click" Text="Add"
Width="37px" />
<br />
<br />
</div>
</form>
</body>
</html>
News.aspx User Page
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>News</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
<HeaderTemplate>
<table border="1">
<tr>
<td>
<b>title</b>
</td>
<td>
<b>news</b>
</td>
<td>
<b>imges</b>
</td>
<td>
<b>detail</b>
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%# DataBinder.Eval(Container.DataItem, "title")%>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "news")%>
</td>
<td>
<img alt="" src='<%# DataBinder.Eval(Container.DataItem, "imageurl")%>' />
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "detail")%>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TakeawayConnectionString %>"
SelectCommand="SELECT [title], [news], [imageurl], [detail] FROM [News]"></asp:SqlDataSource>
</div>
</form>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>News</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
<HeaderTemplate>
<table border="1">
<tr>
<td>
<b>title</b>
</td>
<td>
<b>news</b>
</td>
<td>
<b>imges</b>
</td>
<td>
<b>detail</b>
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%# DataBinder.Eval(Container.DataItem, "title")%>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "news")%>
</td>
<td>
<img alt="" src='<%# DataBinder.Eval(Container.DataItem, "imageurl")%>' />
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "detail")%>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TakeawayConnectionString %>"
SelectCommand="SELECT [title], [news], [imageurl], [detail] FROM [News]"></asp:SqlDataSource>
</div>
</form>
</body>
</html>
Web.Config:
<connectionStrings>
<add name="TakeawayConnectionString" connectionString="Data Source=ocean-chen;Initial Catalog=Takeaway;Integrated Security=True"
providerName="System.Data.SqlClient" /> <!-- please change the connection string -->
</connectionStrings>
<add name="TakeawayConnectionString" connectionString="Data Source=ocean-chen;Initial Catalog=Takeaway;Integrated Security=True"
providerName="System.Data.SqlClient" /> <!-- please change the connection string -->
</connectionStrings>
No comments:
Post a Comment