我正在设计我的网页,图像存储在我的数据库中(该项目是Photostudio管理系统)
我的代码:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace photoshops
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlDataAdapter da = new SqlDataAdapter();
SqlConnection cnn = new SqlConnection();
DataSet ds = new DataSet();
string constr = null;
SqlCommand cmd = new SqlCommand();
if (IsValid != true )
{
constr = @"Data Source=DEVI\SQLEXPRESS;Initial Catalog =cat; Integrated
Security=SSPI";
cnn.ConnectionString = constr;
try
{
if (cnn.State != ConnectionState.Open)
cnn.Open();
}
catch (Exception ex)
{
string str1 = null;
str1 = ex.ToString();
}
cmd.Connection = cnn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "photoset";
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@BillNo", TextBox1.Text);
cmd.Parameters.AddWithValue("@CustomerName", TextBox2.Text);
cmd.Parameters.AddWithValue("@Address", TextBox3.Text);
cmd.Parameters.AddWithValue("@StartDate",Rdbsdate.SelectedDate );
cmd.Parameters.AddWithValue("@EndDate", Rdbddate.SelectedDate );
SqlParameter param0 = new SqlParameter("@Systemurl", SqlDbType.VarChar,
50);
cmd.Parameters.AddWithValue("@Numberofcopies", TextBox7.Text);
cmd.Parameters.AddWithValue("@Amount", TextBox8.Text);
cmd.Parameters.AddWithValue("@Total", TextBox9.Text);
da.SelectCommand = cmd;
try
{
da.Fill(ds);
}
catch (Exception ex)
{
string strErrMsg = ex.Message;
//throw new applicationException("!!!! An error an occured while
//inserting record."+ex.Message)
}
finally
{
da.Dispose();
cmd.Dispose();
cnn.Close();
cnn.Dispose();
}
if (ds.Tables.Count > 0)
{
if (ds.Tables[0].Rows.Count > 0)
{
Msg.Text = "Photo setting sucessfullY";
}
else
{
Msg.Text = "photosetting failled";
}
}
}
}
}
}
我的错误 记录未存储,图像未存储如何更改我的代码。
答案 0 :(得分:1)
首先,您没有保存图像,而是保存计算机的路径。 您需要保存照片的字节数组。
简而言之: 上传其上传控件,您可以在其中选择图像 在你上传照片的二进制内容的字节arrey 然后你只将它作为一个简单的参数发送cmd.Parameters.Add(“@ pic”,pic);
public void OnUpload(Object sender, EventArgs e)
{
// Create a byte[] from the input file
int len = Upload.PostedFile.ContentLength;
byte[] pic = new byte[len];
Upload.PostedFile.InputStream.Read (pic, 0, len);
// Insert the image and comment into the database
SqlConnection connection = new
SqlConnection (@"server=INDIA\INDIA;database=iSense;uid=sa;pwd=india");
try
{
connection.Open ();
SqlCommand cmd = new SqlCommand ("insert into Image "
+ "(Picture, Comment) values (@pic, @text)", connection);
cmd.Parameters.Add ("@pic", pic);
cmd.Parameters.Add ("@text", Comment.Text);
cmd.ExecuteNonQuery ();
}
finally
{
connection.Close ();
}
}
这里有一些教程,第一个链接非常简单,代码很简单 http://www.codeproject.com/KB/web-image/PicManager.aspx
另一个,以防万一: http://www.redmondpie.com/inserting-in-and-retrieving-image-from-sql-server-database-using-c/
主要资源:http://www.codeproject.com/KB/web-image/PicManager.aspx