【发布时间】:2014-01-28 12:16:10
【问题描述】:
我在将图像插入数据库时出错。 下面给出的是我试图插入图像但无法正确插入的代码。
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.Data;
public partial class welcome : System.Web.UI.Page
{
string fname;
SqlConnection con = new SqlConnection();
string emailname;
protected void Page_Load(object sender, EventArgs e)
{
if ((Session["Username"] == null) && (Session["useraddress"] == null))
{
Response.Redirect("Registration.aspx");
}
else
{
emailname = Session["useremail"].ToString();
Label2.Text = Session["Username"].ToString();
Label3.Text = Session["useraddress"].ToString();
welcomelbl.Text = Session["Username"].ToString();
addlbl.Text = Session["useraddress"].ToString();
}
}
protected void Button1_Click1(object sender, EventArgs e)
{
Session.Clear();
Response.Redirect("login.aspx");
}
protected void Button1_Click(object sender, EventArgs e)
{
imageupload();
}
我用来上传图片的功能如下。
private void imageupload()
{
int imglength = FileUpload2.PostedFile.ContentLength;
byte[] bytearray = new byte[imglength];
fname = FileUpload2.PostedFile.FileName;
TextBox1.Text = fname;
HttpPostedFile image = FileUpload2.PostedFile;
image.InputStream.Read(bytearray, 0, imglength);
SqlConnection con = Connection.conn();
con.Open();
SqlCommand cmd = new SqlCommand("insert into imgtbl (imgname,img,useraddress) values(@name ,@image '" + emailname + "')", con);
cmd.Parameters.AddWithValue("@name", SqlDbType.VarChar).Value = TextBox1.Text;
cmd.Parameters.AddWithValue("@image", SqlDbType.Image).Value = bytearray;
cmd.ExecuteNonQuery();
con.Close();
}
}
【问题讨论】:
-
请格式化您的问题,指定错误消息以及您在代码中遇到错误的确切位置
-
为什么你不使用与
imgname和img相同的方法添加useraddress -
“我的代码有错误”不是一个足够详细的错误报告。
-
我的错误是“xyz@gmail.com 附近的语法错误”。听到 xyz@gmail.com 是我们要插入到 imgtb 表中的 +emailname+
-
@user3230665 而且那个 syntax 错误没有导致你检查你的 SQL 语法?