如何上传图像并保存到数据库中 [英] How to Upload Image and Save into database

查看:74
本文介绍了如何上传图像并保存到数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码在这里,但数据不保存到数据库中



Item.aspx



< ;%@ Page Title =Language =C#MasterPageFile =〜/ Site.masterAutoEventWireup =trueCodeFile =Item.aspx.csInherits =Admin_Item%>



< asp:Content ID =Content1ContentPlaceHolderID =HeadContentRunat =Server>





< asp:Content ID =Content2ContentPlaceHolderID =MainContentRunat =Server>





$ / b


< asp:Label ID =lblItemIDrunat = serverText =Item_IDAssociatedControlID =txtItemID> < asp:TextBox ID =txtItemIDrunat =server>
< asp:Label ID =lblItemNamerunat =serverText =Item_NameAssociatedControlID =txtItemName> < asp:TextBox ID =txtItemNamerunat =服务器> ;
< asp:Label ID =lblBrandrunat =serverText =BrandAssociatedControlID =txtBrand> < ; asp:TextBox ID =txtBrandrunat =server>
< asp:Label ID =lblSizerunat =serverText =Size AssociatedControlID =txtSize> < asp:TextBox ID =txtSizerunat =server>
< asp:Label ID =lblPricerunat =serverText =PriceAssociatedControlID =txtPrice> < asp:TextBox ID =txtPricerunat =server>
< asp:Label ID =lblImagerunat =serverText =Image> < asp:FileUpload ID =upImagerunat =server/>
< asp:Label ID =lblColorrunat =serverText =ColorAssociatedControlID =txtColor> < asp:TextBox ID =txtColorrunat =server>
< asp:Button ID =btnSubmitrunat =serverText =Submit

onclick =btnSubmit_Click/>






< asp:SqlDataSource ID =SqlDataSource1runat =server

ConnectionString =<%$ ConnectionStrings:ConnectionString%>

SelectCommand =SELECT * FROM [Item_Master]>







Item.aspx.cs



使用System;

使用System.Collections.Generic;

使用System.Linq;

使用System.Web;

使用System.Web.UI;

使用System.Web.UI.WebControls;

使用System.Data.SqlClient;

使用System.Configuration;

使用System.Data;



public partial class Admin_Item:System.Web.UI.Page

{

SqlConnection con;

SqlCommand cmd;



protected void Page_Load(object sender,EventArgs e)

{



}

protected void btnSubmit_Click(object sender,EventArgs e)

{



if(upImage.PostedFile! = null&& upImage.PostedFile.FileName!=)

{

byte [] imageSize = new byte [upImage.PostedFile.ContentLength];

HttpPostedFile uploadedImage = upImage.PostedFile;

uploadedImage.InputStream.Read(imageSize,0,(int)upImage.PostedFile.ContentLength );



con = new SqlConnection(@Data Source = .\SQLEXPRESS; AttachDbFilename = | DataDirectory | \Online Shopping Portal.mdf; Integrated Security = True ; User Instance = True);

con.Open();

String insertsql =INSERT Item_Master(Item_ID,Item_Name,Brand,Size,Price,Image,Color) )值(''+ txtItemID.Text +'',''+ txtItemName.Text +'',''+ txtBrand.Text +'',''+ txtSize.Text +'' ,''+ txtPrice.Text +'',''+ upImage.FileBytes +'',''+ txtColor.Text +'');

cmd = new SqlCommand(insertsql ,con);

con.Close();

txtItemID.Text =;

txtItemName.Text =;

txtBrand.Text =;

txtSize.Text =;

txtPrice.Text =;

txtColor.Text =;

}

}

}



数据库



Item_ID int未选中

Item_Name varchar(50)已检查

品牌varchar(50)已检查

尺寸int已检查

价格int已检查

图像图像已检查

颜色varchar(50)已检查

未选中

解决方案

ConnectionStrings:ConnectionString%>

SelectCommand =SELECT * FROM [Item_Master]>







Item.aspx.cs



使用System;

使用System.Collections.Generic;

使用System.Linq;

使用System.Web;

使用System.Web.UI;

使用System.Web.UI.WebControls;

使用System.Data.SqlClient;

使用System.Configuration;

使用System.Data;



公共部分类Admin_Item:System.Web.UI .Page
>
{

SqlConnection con;

SqlCommand cmd;



protected void Page_Load(object sender,EventArgs e)

{



}

protected void btnSubmit_Click(object sender, EventArgs e)

{



if(upImage.PostedFile!= null&& upImage.PostedFile.FileName!=)

{

byte [] imageSize = new byte [upImage.PostedFile.ContentLength];

HttpPostedFile uploadedImage = upImage.PostedFile;

uploadedImage.InputStream.Read(imageSize,0,(int)upImage.PostedFile.ContentLength);



con = new SqlConnection(@Data Source = .\SQLEXPRESS; AttachDbFilename = | DataDirectory | \ Online Shopping Portal.mdf; Integrated Security = True; User Instance = True);

con .Open();

String insertsql =INSERT Item_Master(Item_ID,Item_Name,Brand,Size,Price,Image,Color)值(''+ txtItemID.Text +'','' + txtItemName.Text +'',''+ txtBrand.Text +'',''+ txtSize.Text +'',''+ txtPrice.Text +'',''+ upImage .FileBytes +'',''+ txtColor.Text +''');

cmd = new SqlCommand(insertsql,con);

con。关闭();

txtItemID.Text =;

txtItemName.Text =;

txtBrand.Text =;

txtSize.Text =;

txtPrice.Text =;

txtColor.Text =;

}

}

}



数据库



Item_ID int未选中

Item_Name varchar(50)已检查

品牌varchar(50)已检查

尺寸int已检查

价格int已检查

图像图像已检查

颜色varchar(50)已检查

未选中


< blockquote>嗨请阅读这篇文章它对你有帮助。



http://www.dotnetcurry.com/ShowArticle.aspx?ID=129 [ ^ ] [ ]

My Code is here but data are not save into database

Item.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Item.aspx.cs" Inherits="Admin_Item" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">


<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">





<asp:Label ID="lblItemID" runat="server" Text="Item_ID" AssociatedControlID="txtItemID"> <asp:TextBox ID="txtItemID" runat="server">
<asp:Label ID="lblItemName" runat="server" Text="Item_Name" AssociatedControlID="txtItemName"> <asp:TextBox ID="txtItemName" runat="server">
<asp:Label ID="lblBrand" runat="server" Text="Brand" AssociatedControlID="txtBrand"> <asp:TextBox ID="txtBrand" runat="server">
<asp:Label ID="lblSize" runat="server" Text="Size" AssociatedControlID="txtSize"> <asp:TextBox ID="txtSize" runat="server">
<asp:Label ID="lblPrice" runat="server" Text="Price" AssociatedControlID="txtPrice"> <asp:TextBox ID="txtPrice" runat="server">
<asp:Label ID="lblImage" runat="server" Text="Image"> <asp:FileUpload ID="upImage" runat="server" />
<asp:Label ID="lblColor" runat="server" Text="Color" AssociatedControlID="txtColor"> <asp:TextBox ID="txtColor" runat="server">
<asp:Button ID="btnSubmit" runat="server" Text="Submit"
onclick="btnSubmit_Click" />



<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [Item_Master]">



Item.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

public partial class Admin_Item : System.Web.UI.Page
{
SqlConnection con;
SqlCommand cmd;

protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnSubmit_Click(object sender, EventArgs e)
{

if (upImage.PostedFile != null && upImage.PostedFile.FileName != "")
{
byte[] imageSize = new byte[upImage.PostedFile.ContentLength];
HttpPostedFile uploadedImage = upImage.PostedFile;
uploadedImage.InputStream.Read(imageSize, 0, (int)upImage.PostedFile.ContentLength);

con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Online Shopping Portal.mdf;Integrated Security=True;User Instance=True");
con.Open();
String insertsql="INSERT Item_Master(Item_ID,Item_Name,Brand,Size,Price,Image,Color) Values (''" + txtItemID.Text + " '' ,''" + txtItemName.Text + "'' , ''"+txtBrand.Text +"'',''"+txtSize.Text +"'',''"+txtPrice.Text+"'',''"+upImage.FileBytes+"'',''"+txtColor.Text+"'')";
cmd = new SqlCommand(insertsql, con);
con.Close();
txtItemID.Text = "";
txtItemName.Text = "";
txtBrand.Text = "";
txtSize.Text = "";
txtPrice.Text="";
txtColor.Text = "";
}
}
}

Database

Item_ID int Unchecked
Item_Name varchar(50) Checked
Brand varchar(50) Checked
Size int Checked
Price int Checked
Image image Checked
Color varchar(50) Checked
Unchecked

解决方案

ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [Item_Master]">



Item.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

public partial class Admin_Item : System.Web.UI.Page
{
SqlConnection con;
SqlCommand cmd;

protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnSubmit_Click(object sender, EventArgs e)
{

if (upImage.PostedFile != null && upImage.PostedFile.FileName != "")
{
byte[] imageSize = new byte[upImage.PostedFile.ContentLength];
HttpPostedFile uploadedImage = upImage.PostedFile;
uploadedImage.InputStream.Read(imageSize, 0, (int)upImage.PostedFile.ContentLength);

con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Online Shopping Portal.mdf;Integrated Security=True;User Instance=True");
con.Open();
String insertsql="INSERT Item_Master(Item_ID,Item_Name,Brand,Size,Price,Image,Color) Values (''" + txtItemID.Text + " '' ,''" + txtItemName.Text + "'' , ''"+txtBrand.Text +"'',''"+txtSize.Text +"'',''"+txtPrice.Text+"'',''"+upImage.FileBytes+"'',''"+txtColor.Text+"'')";
cmd = new SqlCommand(insertsql, con);
con.Close();
txtItemID.Text = "";
txtItemName.Text = "";
txtBrand.Text = "";
txtSize.Text = "";
txtPrice.Text="";
txtColor.Text = "";
}
}
}

Database

Item_ID int Unchecked
Item_Name varchar(50) Checked
Brand varchar(50) Checked
Size int Checked
Price int Checked
Image image Checked
Color varchar(50) Checked
Unchecked


Hi Please read this article it''ll helpful for you.

http://www.dotnetcurry.com/ShowArticle.aspx?ID=129[^][]


这篇关于如何上传图像并保存到数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆