文件怎样上传到数据库当中啊!我用的是这个控件!和一个按钮!
<input id="txtDoc"tabIndex="18" type="file" name="txtDoc" runat="server">然后怎么从数据库当中读取啊!请代码示例!C#语言!

解决方案 »

  1.   

    向数据库中传文件,需要将文件转换为流,(img格式)然后保存,读取的时候也是以流的形式取下,在根据保存的格式,恢复名称(包括扩展称)
    建议不要上传到数据库中(我都是建立一个文件夹,然后上传到这个文件夹中,读取的时候只要给出路径就可以了,可以下载,也可以打开)
    给一点简单的上传文件的代码给你:
    放一个HtmlInputFile
    在用他的PostedFile.SaveAs属性保存文件就可以了!
      

  2.   

    File1 是服务器段运行的,表(imgtitle,imgtype,imgdata [image类型])Stream imgdatastream = File1.PostedFile.InputStream;
    int imgdatalen = File1.PostedFile.ContentLength;
    string imgtype = File1.PostedFile.ContentType;
             string imgtitle = File1.PostedFile.FileName.Substring          (File1.PostedFile.FileName.LastIndexOf('\\'));//取文件名
    byte[] imgdata = new byte[imgdatalen];//读成字节
    string connstr="user id = sa;password = tianshuang;initial catalog = jsj2004;data source =localhost;Connect timeout = 20"; SqlConnection connection = new SqlConnection(connstr); SqlCommand command = new SqlCommand("INSERT INTO ImageStore(imgtitle,imgtype,imgdata)VALUES ( @imgtitle, @imgtype,@imgdata )", connection ); SqlParameter paramTitle = new SqlParameter("@imgtitle", SqlDbType.VarChar,50 ); paramTitle.Value = imgtitle;
    command.Parameters.Add( paramTitle); SqlParameter paramData = new SqlParameter( "@imgdata", SqlDbType.Image );
    paramData.Value = imgdata;
    command.Parameters.Add( paramData ); SqlParameter paramType = new SqlParameter( "@imgtype", SqlDbType.VarChar,50 );
    paramType.Value = imgtype;
    command.Parameters.Add( paramType ); connection.Open();
    int numRowsAffected = command.ExecuteNonQuery();
    connection.Close();
      

  3.   

    string fileName=this.File1.PostedFile.FileName;
    string name=Path.GetFileName(fileName);
    string exName=Path.GetExtension(fileName);
    if(exName.ToUpper().Trim()!=".XLS")
    {
    Response.Write("<script>window.alert('文件类型不对,只能是Excel文件,请重新选择')</script>");
    return;
    }

    int fileLength=this.File1.PostedFile.ContentLength/1024;

    if(fileLength>4096)
    {
    Response.Write("<script>window.alert('文件太大了,不能超过四兆')</scrpt>");
    return;
    } string tempFileName="k:\\data\\getup\\"+name;
    this.File1.PostedFile.SaveAs(tempFileName);
    int i=0;
    while(true)
    {
    i++;
    if(File.Exists(tempFileName))
    break;
    if(i>10000)
    {
    Response.Write("<script>window.alert('超时,请重新操作')</script>");
    return;
    }
    } string strConnection="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+tempFileName+";Extended Properties=Excel 8.0";
    OleDbConnection eConnection=new OleDbConnection(strConnection);
    eConnection.Open();
    string strSelect="SELECT pc_brnn FROM [Sheet1$]";
    OleDbCommand eCommand=new OleDbCommand();
    eCommand.Connection=eConnection;
    eCommand.CommandText=strSelect;
    OleDbDataReader odr;
    try
    {
    odr=eCommand.ExecuteReader();
    }
    catch(Exception xx)
    {
    Response.Write(xx.ToString());
    Response.Write("<script>alert('打开Excel文件出错')</script>");
    eConnection.Close();
    return;
    }
      

  4.   

    用httpPostedFile这个类来得到所上传得文件。然后用他的SaveAs方法就可以上传图片了