以数据流的形式上传上传数据存入数据库

protected void Unnamed1_Click(object sender, EventArgs e)
    {
        string Sql = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
        SqlConnection con = new SqlConnection(Sql);
        con.Open();
       
        //FileStream fs = File.Open(Server.MapPath( "img/"+ this.FileUpload1.PostedFile.FileName), FileMode.Open);
        //byte[] by = new byte[fs.Length];
        //fs.Read(by, 0, (int)fs.Length);
        string SqlCmd = "insert into ImageStore(ImageData,ImageContentType,ImageDescription,ImageSize) values(@Image,@fileType,@fileDec,@fileSize)";
        SqlCommand comobj = new SqlCommand(SqlCmd, con);
        comobj.Parameters.Add("@Image", SqlDbType.Image).Value = this.FileUpload1.FileBytes;
        comobj.Parameters.Add("@fileType", SqlDbType.VarChar, 20).Value = this.FileUpload1.PostedFile.ContentType;
        comobj.Parameters.Add("@fileDec", SqlDbType.VarChar, 200).Value = this.TextBox1.Text;
        comobj.Parameters.Add("@fileSize", SqlDbType.BigInt).Value = this.FileUpload1.PostedFile.ContentLength;
       
        comobj.ExecuteNonQuery();
        con.Close();
    }

注释掉的是比较麻烦的一种方法;

这里涉及到前台的一个上传控件和textbox

你可能感兴趣的:(以数据流的形式上传上传数据存入数据库)