在sql server 2008中的表中存储pdf,txt doc文件? [英] Storing pdf,txt doc files in Tables in sql server 2008?

查看:126
本文介绍了在sql server 2008中的表中存储pdf,txt doc文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我要上传数据库表中的数据.不是那个文件名,整个文件.
我想在数据库表中插入数据(PDF,txt).

我尝试但收到此错误:用户代码未处理SqlException(字符串或二进制数据将被截断)


我的数据库表:(TestTable)

编号
文件名
FileSize 



代码:

 使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.UI;
使用使用System.Web.UI.WebControls;
使用 System.Data.SqlClient;

公共 部分  _Default:System.Web .UI.页面
{
    受保护的 无效 Page_Load(对象发​​件人,EventArgs e)
    {
        SqlCommand cmd;
    }
   
受保护的 无效 btnSubmit_Click(对象发​​件人,EventArgs e)
{
     字节 [] FSize =  字节 [ FileUpload1.PostedFile.ContentLength];

        HttpPostedFile uploadFile = FileUpload1.PostedFile;

        uploadFile.InputStream.Read(FSize, 0 ,( int )FileUpload1.PostedFile.ContentLength);

        SqlConnection cn =  SqlConnection( @" );
        cn.Open();
        字符串 strQuery = " ;
        SqlCommand cmd =  SqlCommand(strQuery,cn);

        cmd.Parameters.Add( SqlParameter(" ,(解决方案

您使用的数据类型为:-

FileName  Image 



现在,您跟随更改您的代码

byte[] FSize = new byte[FileUpload1.PostedFile.ContentLength];
-------------------
cmd.Parameters.Add(new SqlParameter("@FileName", (object)FSize));



byte[] FSize = new byte[FileUpload1.PostedFile.ContentLength+1];
---------------------
cmd.Parameters.Add(new SqlParameter("@FileName", FSize));


我希望能解决您的问题.


muralikrishna53写道:

我尝试但出现此错误:用户代码未处理SqlException(字符串或二进制数据将被截断)

发生此错误是因为数据的大小大于字段的大小.因此,增加该字段的值(FileSize).


hi
I want to upload the data in database tables.! not that file name , entire file.
I want to insert data (PDF, txt)in database tables.

I tried but getting this error: SqlException was unhandled by user code(String or binary data would be truncated)


My database tables:(TestTable)

Id
FileName
FileSize



Code:

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;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlCommand cmd;
    }
   
protected void  btnSubmit_Click(object sender, EventArgs e)
{
     byte[] FSize = new byte[FileUpload1.PostedFile.ContentLength];

        HttpPostedFile uploadedFile = FileUpload1.PostedFile;

        uploadedFile.InputStream.Read(FSize, 0, (int)FileUpload1.PostedFile.ContentLength);

        SqlConnection cn = new SqlConnection(@"Data Source=TEKPC26\TEKSP26;Initial Catalog=sample;User ID=sa;Password=tekskills_26");
        cn.Open();
        string strQuery = "insert into TestTable(FileName) values(@FileName)";
        SqlCommand cmd = new SqlCommand(strQuery, cn);

        cmd.Parameters.Add(new SqlParameter("@FileName", (object)FSize));

        cmd.ExecuteNonQuery();

        cn.Close();
       
}

解决方案

You use data type:-

FileName  Image 



Now you following change your code

byte[] FSize = new byte[FileUpload1.PostedFile.ContentLength];
-------------------
cmd.Parameters.Add(new SqlParameter("@FileName", (object)FSize));


is

byte[] FSize = new byte[FileUpload1.PostedFile.ContentLength+1];
---------------------
cmd.Parameters.Add(new SqlParameter("@FileName", FSize));


I hope solve your problem.


muralikrishna53 wrote:

I tried but getting this error: SqlException was unhandled by user code(String or binary data would be truncated)

This error occurs because the size of data is bigger than the size of the field. So Increase the value for that field(FileSize).


这篇关于在sql server 2008中的表中存储pdf,txt doc文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆