| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 680 人关注过本帖
标题:保存 pdf 到数据库时,为什么不能会出现“将截断字符串或二进制数据”。
只看楼主 加入收藏
落叶飘香
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2014-7-3
结帖率:50%
收藏
 问题点数:0 回复次数:2 
保存 pdf 到数据库时,为什么不能会出现“将截断字符串或二进制数据”。
程序代码:
public partial class Savewendang : Form
    {
        private SqlConnection con = null;//数据库连接
        private SqlCommand cmd = null;//数据库操作
        private BinaryReader read = null;//二进制读取
        private long fileSize = 0;//文件大小
        private string fileName = null;//文件名字
        private string fileType = null;//文件类型
        private byte[] files;//文件

        SqlBaseClass G_SqlExecute = new SqlBaseClass();
[local]1[/local]
        GoodsIn f = new GoodsIn();
        public Savewendang(GoodsIn f)
        {
            this.f = f;
            InitializeComponent();
        }

        private void Savewendang_Load(object sender, EventArgs e)
        {

        }

        private void selectFileButton_Click(object sender, EventArgs e)//选择文件
        {
            try
            {
                //打开对话框
                OpenFileDialog dialog = new OpenFileDialog();
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    fileAddress.Text = dialog.FileName;
                    FileInfo info = new FileInfo(@fileAddress.Text);
                    //获得文件大小
                    fileSize = info.Length;
                    //提取文件名,三步走
                    int index = info.FullName.LastIndexOf(".");
                    fileName = info.FullName.Remove(index);
                    fileName = fileName.Substring(fileName.LastIndexOf(@"\") + 1);
                    //获得文件扩展名
                    fileType = info.Extension.Replace(".", "");
                    //把文件转换成二进制流
                    files = new byte[Convert.ToInt32(fileSize)];
                    FileStream file = new FileStream(fileAddress.Text, FileMode.Open, FileAccess.Read);
                    read = new BinaryReader(file);
                    read.Read(files, 0, Convert.ToInt32(fileSize));
                }


            }
            catch (Exception ex)
            {
                MessageBox.Show("选择文件时候发生了  " + ex.Message);
            }
        }

        private void saveFileButton_Click(object sender, EventArgs e)//保存
        {

          try
            {
                con = G_SqlExecute.GetCon();
                cmd = con.CreateCommand();
                 = "insert into tb_wendang (ps_no,fname,wenjian) values (@ps_no,@fname,@files)";
            }
            catch (Exception ex)
            {
                MessageBox.Show("连接数据库错误" + ex.Message);
            }
            if (fileAddress.Text.Length <= 0)
            {
                MessageBox.Show(this, "请选择一个文件", "文件地址不合法", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.RtlReading);
                return;
            }
            try
            {
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@fname", fileName);
                cmd.Parameters.AddWithValue("@files", files);
                cmd.Parameters.AddWithValue("@ps_no", PropertyClass.GetGoodsID);
               if (fileSize > 0)
                {
                   string[] type = { "pdf" };
                    bool exists = ((IList)type).Contains(fileType.ToLower());

                    if (!exists)
                    {
                        MessageBox.Show("文档格式不对!只能为pdf格式。", "提示对话框", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                }
                string strsql = "select * from tb_wendang where ps_no='" + PropertyClass.GetGoodsID + "' and fname='" + fileName + "'";
                SqlDataReader u = G_SqlExecute.GetReader(strsql);
                u.Read();
                if (u.HasRows)
                {
                    MessageBox.Show(" 已存在名为" + fileName + "的文档");
                    return;
                }
                if (cmd.ExecuteNonQuery() > 0)
                {
                    MessageBox.Show(fileName + " 保存成功!");
                }
                else
                {
                    MessageBox.Show("发生错误!!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("保存 " + fileName + " 时候发生了  " + ex.Message);
            }
        }
    }
}
图片附件: 游客没有浏览图片的权限,请 登录注册


[ 本帖最后由 落叶飘香 于 2014-8-2 08:54 编辑 ]
搜索更多相关主题的帖子: 数据库 二进制 字符串 
2014-08-01 17:42
h_xuxu
Rank: 2
等 级:论坛游民
威 望:1
帖 子:4
专家分:25
注 册:2013-5-9
收藏
得分:0 
wenjian 字段设成Varchar(max)
2014-08-19 13:06
何事惊慌
Rank: 6Rank: 6
等 级:侠之大者
威 望:4
帖 子:220
专家分:499
注 册:2008-7-2
收藏
得分:0 
应该是数据库的字段放不下你的文件了啊

QQ:860234001
编程交流群:236949758
2014-08-22 16:16
快速回复:保存 pdf 到数据库时,为什么不能会出现“将截断字符串或二进制数据” ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.024550 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved