--商品信息表
create table product
(productID int identity (1,1) primary key ,--商品编号
productName nvarchar(200) ,--商品名称
author nvarchar(200) ,--作者;
isRecommend bit ,--是否被推荐
startPrice decimal(8,2),--原价
salePrice decimal (8,2),--销售价
img nvarchar(200) ,--商品图片文件名
description ntext,--商品的描述
storeID int ,--所属商城编号
typeID  int references typies(typeID),--所属类型编号
hits int default(0) ,--浏览的次数
addTime datetime default getdate() --商品上架时间
)
----------------------------------------------------
help类
namespace Web_项目
{   public class Help
    {
        public static string strCo= "server=NGUABLSUMXZ012J;database=eshop;uid=sa;password=123";        public static SqlConnection conn;
        //打开数据库
        public static SqlConnection sqlOpen()
        {
            conn = new SqlConnection();
            conn.Open();
            return conn;
         }
        //返回数据操作
        public static int exeSQL(string sql)
        {
            using (SqlConnection conn=new SqlConnection (strCon))
            {                conn.Open();
                using (SqlCommand cmd=new SqlCommand (sql,conn))
                {
                    try
                    {
                        int i = cmd.ExecuteNonQuery();
                        return i;
                    }
                    catch (Exception ex)
                    {     
                        throw ex;                      
                    }
                }
            }
          
             }
    }
}----------------------------------------------------
  protected void Button1_Click(object sender, EventArgs e)
        {
            string imgName = "";
            if (this.FileUpload1.HasFile)
            {   
                imgName = DateTime.Now.ToString("yyyMMddHHmmss") + FileUpload1.FileName;//记录文件;
              
                    string Path = Server.MapPath("~/p/" + imgName);//上传文件
                    this.FileUpload1.SaveAs(Path);
                 this.Image1.ImageUrl = "~/p/" + imgName;    //显示图片
                string sql = "insert into product values (" + TextBox1.Text + "," + TextBox2.Text + "," + CheckBox1.Checked + ",'" + TextBox3.Text + "','" + TextBox5.Text + "',"+imgName+ "," + TextBox6.Text + ",'" + DropDownList1.SelectedValue +"','" + DropDownList2.SelectedValue + "'," +"default"+" ,"+"default"+")";
                  
//在调试中imgName附近有错误,不知道是什么原因,是不是类型的不同,还是其他的原因,求指教            
    int i = Help.exeSQL(sql);
                if (i>0)     
                {
                    this.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('新的商品添加成功')", true);
                }
            }
            
        }SQLString

解决方案 »

  1.   

    引号可能不匹配,调试下,输出下sql看是什么。
      

  2.   

    't01ffd55e92d55fb89b 是文件名称
      

  3.   

    1、Image类型为什么要转为 string 类型,有没可能直接存图片到这个字段呢?
    2、如果只是文件名为嘛不直接用nvarchar
    3、在sql这里下个断点,将断到内容(你是SQL语句)拿到查询分析器里执行,看看是否报相同异常
      

  4.   

    图片名称里带有单引号的字符了,尽量使用sqlparamter 参数来传值,不要直接用字符串
      

  5.   

    varchar字符类型,在拼接的时候,变量两端要加单引号string sql = "insert into product values (" + TextBox1.Text + "," + TextBox2.Text + "," + CheckBox1.Checked + ",'" + TextBox3.Text + "','" + TextBox5.Text + "','"+imgName+ "'," + TextBox6.Text + ",'" + DropDownList1.SelectedValue +"','" + DropDownList2.SelectedValue + "'," +"default"+" ,"+"default"+")";