上传图片到数据库
如题.
下面是一些方法
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;
public partial class upload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void button2_click(object sender, EventArgs e)
{
string caption = textbox2.Text;
bool ispublic = false;
if (checkbox1.Checked)
ispublic = true;
Addalbum(caption, ispublic);
label1.Text = "album is ok! ";
}
public static void Addalbum(string caption, bool ispublic)
{
SqlConnection connection = new SqlCannection(ConfigurationManager.ConnectionStrings[ "personal "].ConnectionString);
string sql;
sql = "insert into [albums] ([caption],[ispublic]) values (@caption,@ispublic) ";
SqlCommand command = new SqlCommand(sql, connection);
command.Parameters.Add(new SqlParameter( "@caption ", caption));
command.Parameters.Add(new SqlParameter( "@ispublic ", ispublic));
connection.Openn();
command.ExecuteNonQuery();
}
protected void button1_click(object sender, EventArgs e)
{
Stream imgdatastream = fileupload1.PostedFile.InputStream;
int imgdatalen = fileupload1.PostedFile.ContentLength;
string caption = texbox1.Text;
byte[] bytesoriginal = new byte[imgdatalen];
int n = imgdatastream.Read(bytesoriginal, 0, imgdatalen);
addphoto(1, caption, bytesoriginal);