62,071
社区成员
发帖
与我相关
我的任务
分享
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UpFile.aspx.cs" Inherits="UpFile" %>
<!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>未命名頁面</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div id="Query">
<asp:DropDownList ID="dplSelectrKeyWord" runat="server">
<asp:ListItem Value="">请选择</asp:ListItem>
<asp:ListItem Value="ID">编号</asp:ListItem>
<asp:ListItem Value="name">名字</asp:ListItem>
<asp:ListItem Value="age">年龄</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="txtKeyWord" runat="server"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" Text="查询" OnClick="btnSearch_Click" />
</div>
<div id="content">
<asp:DataGrid ID="DataGrid1" runat="server" AutoGenerateColumns="False" OnItemCommand="DataGrid1_ItemCommand" >
<Columns>
<asp:TemplateColumn HeaderText="编号">
<ItemTemplate>
<asp:TextBox ID="txtID" runat="server" Width="50" Text='<%# DataBinder.Eval(Container,"DataItem.ID") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="姓名">
<ItemTemplate>
<asp:TextBox ID="txtName" runat="server" Width="50" Text='<%# DataBinder.Eval(Container,"DataItem.Name") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="年龄">
<ItemTemplate>
<asp:TextBox ID="txtAge" runat="server" Width="50" Text='<%# DataBinder.Eval(Container,"DataItem.Age") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="上传图片">
<ItemTemplate>
<asp:FileUpload ID="upFile" runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="操作">
<ItemTemplate>
<asp:LinkButton ID="linkAdd" runat="server" CommandName="Up">上传</asp:LinkButton>
<asp:LinkButton ID="linkDelete" runat="server" CommandName="Delete">删除</asp:LinkButton>
<asp:LinkButton ID="linkEdit" runat="server" CommandName="Edit">修改</asp:LinkButton>
<asp:LinkButton ID="linkView" runat="server" CommandName="View">查看图片</asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</div>
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class UpFile : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
if (!Page.IsPostBack) {
BindPage("");
}
}
private SqlConnection CretaeSqlConnection() {
string SqlConnectionString = "server=192.168.0.1;database=erp20091030;uid=sa;pwd=000000";
return new SqlConnection(SqlConnectionString);
}
public DataSet ExecuteDataSet(CommandType commandType, string SqlCommandText) {
using (SqlConnection conn = CretaeSqlConnection()) {
DataSet ds = new DataSet();
SqlCommand cmd = conn.CreateCommand();
cmd.Connection = conn;
cmd.CommandType = commandType;
cmd.CommandText = SqlCommandText;
SqlDataAdapter dapter = new SqlDataAdapter(cmd);
dapter.Fill(ds);
return ds;
}
}
public void ExecuteNonQuery(CommandType commandType, string SqlCommandText, SqlCommand cmd) {
using (SqlConnection conn = CretaeSqlConnection()) {
conn.Open();
SqlTransaction transaction = conn.BeginTransaction();
cmd.Connection = conn;
cmd.CommandType = commandType;
cmd.CommandText = SqlCommandText;
cmd.Transaction = transaction;
try {
cmd.ExecuteNonQuery();
transaction.Commit();
}
catch {
transaction.Rollback();
}
}
}
private DataView GetBindPageData(string SqlWhere) {
string SqlCommandText = string.Format("select * from biao {0} order by id asc", SqlWhere);
DataSet ds = ExecuteDataSet(CommandType.Text, SqlCommandText);
DataRow row = ds.Tables[0].NewRow();
if (ds.Tables[0].Rows.Count > 0) {
row["ID"] = Convert.ToInt32(ds.Tables[0].Select("1=1", "id desc")[0]["ID"]) + 1;
}
else {
row["ID"] = 1;
}
ds.Tables[0].Rows.Add(row);
DataView dv = ds.Tables[0].DefaultView;
dv.Sort = "ID desc ";
return dv;
}
private void BindPage(string SqlWhere) {
this.DataGrid1.DataSource = GetBindPageData(SqlWhere);
this.DataGrid1.DataBind();
}
protected void btnSearch_Click(object sender, EventArgs e) {
if (this.dplSelectrKeyWord.SelectedValue == "") {
ShowMsg("请选择查询条件");
return;
}
string SqlWhere = string.Format(" where {0} like '{1}%'", this.dplSelectrKeyWord.SelectedValue, this.txtKeyWord.Text.Trim());
BindPage(SqlWhere);
}
private void ViewPictue(string ID) {
string Sql = "select photo from biao where id='" + ID + "'";
DataSet ds = ExecuteDataSet(CommandType.Text, Sql);
byte[] photo = (byte[])ds.Tables[0].Rows[0]["photo"];
Response.BinaryWrite(photo);
}
private void Delete(string ID) {
string Sql = "delete from biao where id=@ID";
SqlCommand cmd = new SqlCommand();
cmd.Parameters.AddWithValue("@ID", ID).SqlDbType = SqlDbType.Int;
ExecuteNonQuery(CommandType.Text, Sql, cmd);
}
protected void DataGrid1_ItemCommand(object source, DataGridCommandEventArgs e) {
if (e.CommandName.Equals("View")) {
ViewPictue(((TextBox)e.Item.FindControl("txtID")).Text.Trim());
}
else {
string id = ((TextBox)e.Item.FindControl("txtID")).Text.Trim();
bool isDelete = e.CommandName.Equals("Delete");
if (isDelete) {
Delete(id);
BindPage("");
ShowMsg("删除成功");
}
else {
FileUpload upFile = e.Item.FindControl("upFile") as FileUpload;
if (upFile.PostedFile.ContentLength == 0) {
ShowMsg("请选择上传文件");
return;
}
string name = ((TextBox)e.Item.FindControl("txtName")).Text.Trim();
string age = ((TextBox)e.Item.FindControl("txtAge")).Text.Trim();
if (id == string.Empty) {
ShowMsg("请输入ID");
return;
}
string error = Save(id, name, age, upFile.FileBytes, e.CommandName.Equals("Delete"));
if (error == string.Empty) {
BindPage("");
ShowMsg("保存成功");
}
else {
ShowMsg(error);
}
}
}
}
private void ShowMsg(string msg) {
Page.RegisterStartupScript("", "<script language='javascript'>window.alert('" + msg + "')</script>");
}
private string Save(string ID, string name, string age, byte[] photo, bool isDelete) {
string SqlCommandText = @"if(exists(select * from biao where id = @id))
begin
update biao set name=@name,age=@age,photo=@photo where id = @id
end
else
begin
insert into biao(ID,name,age,photo) Values(@ID,@name,@age,@photo)
end
";
SqlCommand cmd = new SqlCommand();
if (isDelete) {
SqlCommandText = @"delete from biao where id = @id";
}
else {
cmd.Parameters.AddWithValue("@name", name).SqlDbType = SqlDbType.NVarChar;
cmd.Parameters.AddWithValue("@age", age).SqlDbType = SqlDbType.NVarChar;
cmd.Parameters.AddWithValue("@photo", photo).SqlDbType = SqlDbType.Image;
}
cmd.Parameters.AddWithValue("@ID", ID).SqlDbType = SqlDbType.Int;
try {
ExecuteNonQuery(CommandType.Text, SqlCommandText, cmd);
}
catch (Exception ex) {
return ex.Message;
}
return string.Empty;
}
}