目的:将图片存放在数据库里面(不是存放图片的路径)数据库:
      create table ImageStorage

    mName varchar(30) primary key,
    mData  image ,  
    mClass varchar(10),
    mIntro varchar(100),
    mSize int 
)
  前台:
<body>
    <form id="form1" runat="server">
    <center ><div>
        <br />
        <span style="font-size: 1.4em; color: #ff0066"><strong>asp.net 中文件上传<br />
            <br />
            选择文件:<asp:FileUpload ID="filesUpload" runat="server" /><br />
            <br />
            图片说明: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
            <asp:TextBox ID="txtIntro" runat="server" Height="104px" TextMode="MultiLine" Width="209px"></asp:TextBox><br />
            <br />
            <br />
            <asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="上传文件" Width="173px" /></strong></span></div> </center>
    </form>
</body>处理程序段:protected void btnUpload_Click(object sender, EventArgs e)
    {
        ///////获取上传文件的相关信息
    
        string path = this.filesUpload.PostedFile.FileName;
        string fileName = Path.GetFileName(path);
        int  fileLength = this.filesUpload.PostedFile.ContentLength;
        if (fileLength < 1)
            return;
        try
        {
            //////////////////////////////建立数据库连接,命令
            SqlConnection con = new SqlConnection("server=FIRST-FIRST\\LOVE;database=stuTest;uid=sa;password=sa");
            SqlCommand cmd = new SqlCommand("insert into ImageStorage values(@name,@image,@class,@discription,@size)", con);
            //////////////////////////////读取图片
            Stream objectStream = this.filesUpload.PostedFile.InputStream;
            byte[] byteContent = new byte[fileLength ];
            objectStream.Read(byteContent, 0, fileLength );
            //////////////////////////////处理参数
            SqlParameter par = new SqlParameter("@name", SqlDbType.VarChar, 30);
            par.Value = fileName;
            cmd.Parameters.Add(par);
            par = new SqlParameter("@image", SqlDbType.VarBinary , fileLength);
            par.Value = byteContent;
            cmd.Parameters.Add(par);
            par = new SqlParameter("@class", SqlDbType.VarChar,             this.filesUpload.PostedFile.ContentType.Length);
            par.Value = this.filesUpload.PostedFile.ContentType;
            cmd.Parameters.Add(par);
            par = new SqlParameter("@discription", SqlDbType.VarChar, 100);
            par.Value = this.txtIntro.Text.ToString();
            cmd.Parameters.Add(par);
            par = new SqlParameter("@size", SqlDbType.Int);
            par.Value = this.filesUpload.PostedFile.ContentLength;
            cmd.Parameters.Add(par);            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
        catch (Exception er)
        {
            Response.Write(er.Message .ToString ());
        }
        
    }
运行后:
     出现 :"将截断字符串或二进制数据。语句已终止"请问:1、出现这种问题的原因是什么呀?
     2、数据库里设定的数据大小和SqlParameter中对应数据的大小有什么关系?设置SqlParameter中数据大小(长度)的依据是什么呢?
 
    谢谢····
 

解决方案 »

  1.   

    将截断字符串或二进制数据。语句已终止比如 
    create table tb(v varchar(2))
    insert tb select 'asdfas'
    时就报这个错了.
    v定义为2字节长,想存放asdfas6个字节数据表某个字段长度不够.
      

  2.   

    表(或存储过程)中设定的字段大小跟parameter的保持一致
      

  3.   

    怎么会没关系统呢?
    int64与bigint
    int32与int
    byte与tinyint
    ....
      

  4.   

    呵呵。我的意思是这两个东西之间并没有必然的联系。
    当然可以数据库都设为bigint,而类里面都写成int32。我的意思是它们不是必须一致。
      

  5.   

    数据字段长度不够,不是mData ,而是其他的字段,
      

  6.   

    数据库中使用类型和SqlCommand中的参数类型都设为image