当我尝试将图像插入到我的数据库中时,它始终保存这个:
0xFFD8FFE145AB4578696600004D4D002A00000008000A011200030000000100000000021300030000000100010000011A0005000000010000018E011B0005000000010000019601280003000000010002000001320002000000140000019E010F000200000064000001B201100002000000640000021687690004000000010000
即使图像为空。
请帮忙。
C#代码:
if (FileUpload1.PostedFile.FileName!="")
{
Byte[] image;
Stream s = FileUpload1.PostedFile.InputStream;
BinaryReader br = new BinaryReader(s);
image = br.ReadBytes((Int32)s.Length);
SqlCommand NewUser = new SqlCommand("INSERT INTO [User] Values (@username,@password,@name,@lastname,@location,@profesion,@email,@gender,@money,@pro,@xp,@lv,@m1,@m2,@m3,@m4,@m5,@d1,@d2,@d3,@d4,@d5,@im,@phone);", c);
NewUser.Connection = c;
NewUser.Parameters.AddWithValue("@username", txtuser.Text);
NewUser.Parameters.AddWithValue("@password", txtpass.Text);
NewUser.Parameters.AddWithValue("@name", txtFName.Text);
NewUser.Parameters.AddWithValue("@lastname", txtLName.Text);
NewUser.Parameters.AddWithValue("@location", ddlcountry.SelectedItem.Text);
NewUser.Parameters.AddWithValue("@profesion", txtprofession.Text);
NewUser.Parameters.AddWithValue("@email", txtemail.Text);
NewUser.Parameters.AddWithValue("@gender", rbgendere.SelectedItem.Text);
NewUser.Parameters.AddWithValue("@money", 0);
NewUser.Parameters.AddWithValue("@pro", DBNull.Value);
NewUser.Parameters.AddWithValue("@xp", 0);
NewUser.Parameters.AddWithValue("@lv", 1);
NewUser.Parameters.AddWithValue("@m1", 0);
NewUser.Parameters.AddWithValue("@m2", 0);
NewUser.Parameters.AddWithValue("@m3", 0);
NewUser.Parameters.AddWithValue("@m4", 0);
NewUser.Parameters.AddWithValue("@m5", 0);
NewUser.Parameters.AddWithValue("@d1", 0);
NewUser.Parameters.AddWithValue("@d2", 0);
NewUser.Parameters.AddWithValue("@d3", 0);
NewUser.Parameters.AddWithValue("@d4", 0);
NewUser.Parameters.AddWithValue("@d5", 0);
NewUser.Parameters.AddWithValue("@im", image);
NewUser.Parameters.AddWithValue("@phone", PhoneNumber.Text);
Session["CurentUserid"] = txtuser.Text;
c.Open();
int row = NewUser.ExecuteNonQuery();
c.Close();
if (row > 0)
{
LabelError.Text = "success";
}
Session["Conect"] = (bool)true;
Response.Redirect("Finish Had Member.aspx", true);
}
else
{
SqlCommand NewUser = new SqlCommand("INSERT INTO [User] Values (@username,@password,@name,@lastname,@location,@profesion,@email,@gender,@money,@pro,@xp,@lv,@m1,@m2,@m3,@m4,@m5,@d1,@d2,@d3,@d4,@d5,@im,@phone);", c);
NewUser.Connection = c;
NewUser.Parameters.AddWithValue("@username", txtuser.Text);
NewUser.Parameters.AddWithValue("@password", txtpass.Text);
NewUser.Parameters.AddWithValue("@name", txtFName.Text);
NewUser.Parameters.AddWithValue("@lastname", txtLName.Text);
NewUser.Parameters.AddWithValue("@location", ddlcountry.SelectedItem.Text);
NewUser.Parameters.AddWithValue("@profesion", txtprofession.Text);
NewUser.Parameters.AddWithValue("@email", txtemail.Text);
NewUser.Parameters.AddWithValue("@gender", rbgendere.SelectedItem.Text);
NewUser.Parameters.AddWithValue("@money", 0);
NewUser.Parameters.AddWithValue("@pro", DBNull.Value);
NewUser.Parameters.AddWithValue("@xp", 0);
NewUser.Parameters.AddWithValue("@lv", 1);
NewUser.Parameters.AddWithValue("@m1", 0);
NewUser.Parameters.AddWithValue("@m2", 0);
NewUser.Parameters.AddWithValue("@m3", 0);
NewUser.Parameters.AddWithValue("@m4", 0);
NewUser.Parameters.AddWithValue("@m5", 0);
NewUser.Parameters.AddWithValue("@d1", 0);
NewUser.Parameters.AddWithValue("@d2", 0);
NewUser.Parameters.AddWithValue("@d3", 0);
NewUser.Parameters.AddWithValue("@d4", 0);
NewUser.Parameters.AddWithValue("@d5", 0);
NewUser.Parameters.AddWithValue("@im", DBNull.Value);
NewUser.Parameters.AddWithValue("@phone", PhoneNumber.Text);
Session["CurentUserid"] = txtuser.Text;
c.Open();
NewUser.ExecuteNonQuery();
c.Close();
Session["Conect"] = (bool)true;
Response.Redirect("Finish Had Member.aspx", true);
}
答案 0 :(得分:1)
我正在做一个相应的改变工作示例
页面的HTML
<div>
<table>
<tr>
<td>
Image Name:
</td>
<td>
<asp:TextBox ID="txtImageName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Upload Image:
</td>
<td>
<asp:FileUpload ID="fileuploadImage" runat="server" />
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" />
</td>
</tr>
</table>
</div>
<div>
<asp:GridView ID="gvImages" CssClass="Gridview" runat="server" AutoGenerateColumns="False"
HeaderStyle-BackColor="#7779AF" HeaderStyle-ForeColor="white">
<Columns>
<asp:BoundField HeaderText="Image Name" DataField="imagename" />
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# "Handler.ashx?ImID="+ Eval("ImageID") %>'
Height="150px" Width="150px" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
背后的代码
protected void btnUpload_Click(object sender, EventArgs e)
{
if (fileuploadImage.HasFile)
{
//getting length of uploaded file
int length = fileuploadImage.PostedFile.ContentLength;
//create a byte array to store the binary image data
byte[] imgbyte = new byte[length];
//store the currently selected file in memeory
HttpPostedFile img = fileuploadImage.PostedFile;
//set the binary data
img.InputStream.Read(imgbyte, 0, length);
string imagename = txtImageName.Text;
//use the web.config to store the connection string
SqlConnection connection = new SqlConnection(strcon);
connection.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO Image (ImageName,Image) VALUES (@imagename,@imagedata)", connection);
cmd.Parameters.Add("@imagename", SqlDbType.VarChar, 50).Value = imagename;
cmd.Parameters.Add("@imagedata", SqlDbType.Image).Value = imgbyte;
int count = cmd.ExecuteNonQuery();
connection.Close();
if (count == 1)
{
BindGridData();
txtImageName.Text = string.Empty;
ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:alert('" + imagename + " image inserted successfully')", true);
}
}
}
private void BindGridData()
{
SqlConnection connection = new SqlConnection(strcon);
SqlCommand command = new SqlCommand("SELECT imagename,ImageID from [Image]", connection);
SqlDataAdapter daimages = new SqlDataAdapter(command);
DataTable dt = new DataTable();
daimages.Fill(dt);
gvImages.DataSource = dt;
gvImages.DataBind();
gvImages.Attributes.Add("bordercolor", "black");
}
右键点击您的项目,添加新的Generic Handler
名称 - &gt; Handler.ashx
用此
替换ProcessRequest
string strcon = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
public void ProcessRequest(HttpContext context)
{
string imageid = context.Request.QueryString["ImID"];
System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(strcon);
connection.Open();
System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand("select Image from Image where ImageID=" + imageid, connection);
System.Data.SqlClient.SqlDataReader dr = command.ExecuteReader();
dr.Read();
context.Response.BinaryWrite((Byte[])dr[0]);
connection.Close();
context.Response.End();
}
在网络配置中,在<configuration> -> <connectionStrings>
这里的数据库名称是testDB
<add name="ConnectionString" connectionString="data source=.\SQLEXPRESS;database=testDB;trusted_Connection=yes"/>
使用此查询在SQL中创建表
CREATE TABLE [dbo].[Image](
[ImageId] [int] IDENTITY(1,1) NOT NULL,
[ImageName] [varchar](50) NULL,
[Image] [image] NULL
)
希望这会对你有所帮助
答案 1 :(得分:1)
尝试
command.Parameters.Add("@im", SqlDbType.VarBinary);
command.Parameters["@ID"].Value = image;
必须匹配SQL列类型 根据安萨里的答案,图像是正确的类型。