1 数据库表格--数据库
图片分类
-----
typeid subtypeid typename
图片信息
picid subtypeid picname filename serverfile mimetype
编号 类别 图片名 文件名 文件路径 文件类型
说明; 两个表格使用subtypeid关联
2 程序前台
<HTML>
<HEAD>
<title>pic</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:label id="Label1" runat="server"Width="72px">subtypeid</asp:label>
<INPUT id="File1" type="file"name="File1" runat="server">
<asp:textbox id="TextBox1" runat="server"></asp:textbox>
<asp:label id="Label2" runat="server">照片名</asp:label>
<asp:button id="Button1" runat="server"Text="insert"></asp:button>
<asp:textbox id="TextBox2" runat="server"></asp:textbox>
<asp:Label id="Lbl_typename"runat="server" Width="256px"></asp:Label>
<asp:dropdownlist id="DropDownList1" runat="server" AutoPostBack="True" ></asp:dropdownlist>
<asp:Label id="Label3" runat="server">keyid</asp:Label>
</form>
</body>
</HTML>
3 程序后台:
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace house.map
{
/// <summary>
/// pic 的摘要说明。
/// </summary>
public class pic : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.HtmlControls.HtmlInputFile File1;
private string connstr=System.Configuration.ConfigurationSettings.AppSettings["mapConnstring"];
protected System.Web.UI.WebControls.Label Lbl_typename;
protected System.Web.UI.WebControls.Label Label3;
DataTable dt=new DataTable();
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!this.IsPostBack)
{
this.LoadDropDownData();
this.DropDownList1.Items.Add("");//增加空值
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.DropDownList1.SelectedIndexChanged += new System.EventHandler(this.DropDownList1_SelectedIndexChanged_1);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void LoadDropDownData()
{
/**/
DataSet ds=new DataSet();
using(SqlConnection conn=new SqlConnection(this.connstr))
{
string sqlstr="select SubTypeId,typename from 图片分类 order by SubTypeId";
conn.Open();
SqlDataAdapter sda=new SqlDataAdapter(sqlstr,conn);
sda.Fill(ds);
this.DropDownList1.Items.Clear();
this.DropDownList1.DataSource=ds;
this.DropDownList1.DataValueField="typename";
this.DropDownList1.DataTextField="SubTypeId";
this.DropDownList1.DataBind();
}
}
private void Button1_Click(object sender, System.EventArgs e)
{
string sql="insert into 图片信息(PicId,SubTypeId,KeyId,PicName,FileName,ServerFile,MimeType) values(@PicId,@SubTypeId,@KeyId,@PicName,@FileName,@ServerFile,@MimeType) ";
using(SqlConnection conn=new SqlConnection(this.connstr))
{
try
{
conn.Open();
SqlCommand cmd=new SqlCommand(sql,conn);
cmd.Parameters.Add("@PicId",this.Max());
cmd.Parameters.Add("@SubTypeId",this.DropDownList1.SelectedValue);
cmd.Parameters.Add("@KeyId",this.TextBox2.Text.Trim());
cmd.Parameters.Add("@PicName",this.TextBox1.Text.Trim());
cmd.Parameters.Add("@FileName",this.HaveFile(this.File1.PostedFile.FileName));
cmd.Parameters.Add("@ServerFile",this.File1.PostedFile.FileName);
cmd.Parameters.Add("@MimeType",this.File1.PostedFile.ContentType);
cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
Response.Write("error:"+ex.Message);
}
}
}
//
private string HaveFile(string filename)
{
int i=filename.LastIndexOf(@"/",filename.Length);
string ResultFilename=filename.Substring(i+1);
return ResultFilename;
}
//
private int Max()
{
int result=0;
using(SqlConnection conn1=new SqlConnection(this.connstr))
{
string sql="select max(picid) from 图片信息";
SqlCommand cmd=new SqlCommand(sql,conn1);
try
{
conn1.Open();
result=Convert.ToInt32(cmd.ExecuteScalar());
}
catch
{
}
return result+1;
}
}
private void DropDownList1_SelectedIndexChanged_1(object sender, System.EventArgs e)
{
this.Lbl_typename.Text=this.DropDownList1.SelectedValue;
}
}
}
4 解释
图片分类
-----
typeid subtypeid typename
图片信息
picid subtypeid picname filename serverfile mimetype
编号 类别 图片名 文件名 文件路径 文件类型
说明; 两个表格使用subtypeid关联
2 程序前台
<HTML>
<HEAD>
<title>pic</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:label id="Label1" runat="server"Width="72px">subtypeid</asp:label>
<INPUT id="File1" type="file"name="File1" runat="server">
<asp:textbox id="TextBox1" runat="server"></asp:textbox>
<asp:label id="Label2" runat="server">照片名</asp:label>
<asp:button id="Button1" runat="server"Text="insert"></asp:button>
<asp:textbox id="TextBox2" runat="server"></asp:textbox>
<asp:Label id="Lbl_typename"runat="server" Width="256px"></asp:Label>
<asp:dropdownlist id="DropDownList1" runat="server" AutoPostBack="True" ></asp:dropdownlist>
<asp:Label id="Label3" runat="server">keyid</asp:Label>
</form>
</body>
</HTML>
3 程序后台:
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace house.map
{
/// <summary>
/// pic 的摘要说明。
/// </summary>
public class pic : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.HtmlControls.HtmlInputFile File1;
private string connstr=System.Configuration.ConfigurationSettings.AppSettings["mapConnstring"];
protected System.Web.UI.WebControls.Label Lbl_typename;
protected System.Web.UI.WebControls.Label Label3;
DataTable dt=new DataTable();
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!this.IsPostBack)
{
this.LoadDropDownData();
this.DropDownList1.Items.Add("");//增加空值
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.DropDownList1.SelectedIndexChanged += new System.EventHandler(this.DropDownList1_SelectedIndexChanged_1);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void LoadDropDownData()
{
/**/
DataSet ds=new DataSet();
using(SqlConnection conn=new SqlConnection(this.connstr))
{
string sqlstr="select SubTypeId,typename from 图片分类 order by SubTypeId";
conn.Open();
SqlDataAdapter sda=new SqlDataAdapter(sqlstr,conn);
sda.Fill(ds);
this.DropDownList1.Items.Clear();
this.DropDownList1.DataSource=ds;
this.DropDownList1.DataValueField="typename";
this.DropDownList1.DataTextField="SubTypeId";
this.DropDownList1.DataBind();
}
}
private void Button1_Click(object sender, System.EventArgs e)
{
string sql="insert into 图片信息(PicId,SubTypeId,KeyId,PicName,FileName,ServerFile,MimeType) values(@PicId,@SubTypeId,@KeyId,@PicName,@FileName,@ServerFile,@MimeType) ";
using(SqlConnection conn=new SqlConnection(this.connstr))
{
try
{
conn.Open();
SqlCommand cmd=new SqlCommand(sql,conn);
cmd.Parameters.Add("@PicId",this.Max());
cmd.Parameters.Add("@SubTypeId",this.DropDownList1.SelectedValue);
cmd.Parameters.Add("@KeyId",this.TextBox2.Text.Trim());
cmd.Parameters.Add("@PicName",this.TextBox1.Text.Trim());
cmd.Parameters.Add("@FileName",this.HaveFile(this.File1.PostedFile.FileName));
cmd.Parameters.Add("@ServerFile",this.File1.PostedFile.FileName);
cmd.Parameters.Add("@MimeType",this.File1.PostedFile.ContentType);
cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
Response.Write("error:"+ex.Message);
}
}
}
//
private string HaveFile(string filename)
{
int i=filename.LastIndexOf(@"/",filename.Length);
string ResultFilename=filename.Substring(i+1);
return ResultFilename;
}
//
private int Max()
{
int result=0;
using(SqlConnection conn1=new SqlConnection(this.connstr))
{
string sql="select max(picid) from 图片信息";
SqlCommand cmd=new SqlCommand(sql,conn1);
try
{
conn1.Open();
result=Convert.ToInt32(cmd.ExecuteScalar());
}
catch
{
}
return result+1;
}
}
private void DropDownList1_SelectedIndexChanged_1(object sender, System.EventArgs e)
{
this.Lbl_typename.Text=this.DropDownList1.SelectedValue;
}
}
}
4 解释