string sqlstr = "select top 1 tu from tuzhi where id='b1409'"; SqlCommand cm = new SqlCommand(sqlstr, con); SqlDataReader dr = cm.ExecuteReader(); byte[] imageBody = null; try { //byte[] imageBody = (byte[])cm.ExecuteScalar();
while (dr.Read()) { imageBody = (byte[])dr.GetValue(0); }
邮件我收到了,谢谢你,你用的是把TEXTBOX里的文字写到数据库里,而不是把PDF文件存到数据库,有两处代码我有点不明白 SqlCommand cmd = new SqlCommand("", con); cmd.CommandText = "Select Files From test3 Where ID='1'";commandText 为什么会写在 sqlcommand 后面呢,这样会起什么作用的呢,还有SqlCommand cmd = new SqlCommand("", con); 前面这个参数是空又怎么插入到数据库里面的呢?我家里这台电脑上没有数据库也没法看到效果,但我总觉得有点问题
数据库的 pdf 格式是二进制的 你读数据库的时候返回的只读器 传入一个文件流内然后把它保存到硬盘 再打开读 就可以了;
textbox中写的是你准备写入数据库里的pdf文件在硬盘里的存放地址。
SqlCommand cmd = new SqlCommand("", con); cmd.CommandText = "Select Files From test3 Where ID='1'"; 第一行只是定义一个SqlCommand型的变量cmd,并且将其连接设为con,其CommandText赋空值,第二行给cmd的CommandText属性重新赋值。
利用Command取出后,转换为二进制格式的数据流或文件。
把数据流或文件利用PDF格式文件的读取控件读出来就好了。
PDF格式是Adobe公司的产品有专门的ACTIVEX控件。
写入:
FileStream fs = File.OpenRead(@“D:\aa.pdf”
byte[] bytes = new byte[fs.Length];
fs.Read(bytes, 0, bytes.Length);
SqlConnection con = new SqlConnection(ConStr);
SqlCommand cmd = new SqlCommand("", con);
cmd.CommandText = "Insert into test2 values('10','','D:\aa.pdf',@files)";
SqlParameter paraFile = new SqlParameter("@files", SqlDbType.Image);
paraFile.Value = bytes;
cmd.Parameters.Add(paraFile);
this.Cursor = Cursors.WaitCursor;
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();
this.Cursor = Cursors.Default;
读出:
byte[] mypdffile = null;
SqlConnection con = new SqlConnection(ConStr);
SqlCommand cmd = new SqlCommand("", con);
cmd.CommandText = "Select Files From test2 Where id='10'";
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while(dr.Read())
{
mypdffile = (byte[])dr.GetValue(0);
}
con.Close();
this.Cursor = Cursors.WaitCursor;
try
{
FileStream fs = new FileStream("D:\\aaa.pdf", FileMode.Create);
fs.Write(mypdffile, 0, mypdffile.Length);
fs.Flush();
fs.Close();
}
catch { }
this.Cursor = Cursors.Default;
显示(采用webBrowser控件):
this.webBrowser1.Navigate(“D:\\aaa.pdf”);
楼主分太少了。
保存到temp,完后再删除不就和没有保存到文件一样的效果了啊
private void ShowPDF_Load(object sender, EventArgs e)
{
this.TopLevel = false;
SqlConnection con= DB.createcon();
con.Open();
string sqlstr = "select top 1 tu from tuzhi where id='b1409'";
SqlCommand cm = new SqlCommand(sqlstr, con);
SqlDataReader dr = cm.ExecuteReader();
byte[] imageBody = null;
try
{
//byte[] imageBody = (byte[])cm.ExecuteScalar();
while (dr.Read())
{
imageBody = (byte[])dr.GetValue(0);
}
try
{
FileStream fs = new FileStream("D:\\aaa.pdf", FileMode.Create);
fs.Write(imageBody, 0, imageBody.Length);
fs.Flush();
fs.Close();
}
catch { }
this.Cursor = Cursors.Default; //PdfReader pdfread = new PdfReader(imageBody );
axAcroPDF1.LoadFile("D:\\aaa.pdf");
}
catch (Exception er)
{
MessageBox.Show(er.Message);
}
cm.Dispose();
}
{
SqlConnection con = new SqlConnection("server=.;database=drawing;uid=sa;pwd=7612098");
con.Open();
FileStream myStream = new FileStream(Filename, FileMode.Open, FileAccess.Read);
Byte[] imageBody = new byte[myStream.Length];
myStream.Read(imageBody, 0, (int)myStream.Length);
myStream.Close();
string sqlstr = "Insert into tuzhi values('"+textBox1.Text+"','"+textBox2.Text +"','" + imageBody + "','2009-11-4' )";
try
{
SqlCommand cm = new SqlCommand(sqlstr, con);
cm.ExecuteNonQuery();
cm.Dispose();
MessageBox.Show("上传成功!");
}
catch (Exception er)
{
MessageBox.Show(er.Message);
}
}这个是写入 是不 是写入的时候就不对呀
/// <summary>
/// 实现PDF复制,并控制打印权限
/// </summary>
/// <param name="filePath">源PDF文件</param>
/// <param name="toPath">目标PDF文件</param>private void ConvertPDFToPDF(string filePath, string toPath, bool print)
{
try
{
//读取源pdf中的内容
PdfReader reader = new PdfReader(filePath);
Document document = new Document(reader.GetPageSizeWithRotation(1));
//取得源pdf的页数
int n = reader.NumberOfPages;
FileStream baos = new FileStream(toPath, FileMode.Create, FileAccess.Write);
PdfCopy copy = new PdfCopy(document, baos);
//往pdf中写入内容
document.Open();
for (int i = 1; i <= n; i++)
{
PdfImportedPage page = copy.GetImportedPage(reader, i);
copy.AddPage(page);
}
document.Close();
reader.Close();
}
catch
{
Response.Write("<script language='javascript'>window.alert('该文件在目录中不存在');window.close();</script>");
Dtsc.Common.Utility.PageHelper.Close(this.Page);
} }我这个方法是在线阅读并要控制打印权限和菜单栏是否的方法(已去掉控制打印权限的代码),希望对你有所帮助。
其次,要用byte[]型的参数将数据流写入该字段。
红色部分没有传参数。
SqlCommand cmd = new SqlCommand("", con);
cmd.CommandText = "Select Files From test3 Where ID='1'";commandText 为什么会写在 sqlcommand 后面呢,这样会起什么作用的呢,还有SqlCommand cmd = new SqlCommand("", con); 前面这个参数是空又怎么插入到数据库里面的呢?我家里这台电脑上没有数据库也没法看到效果,但我总觉得有点问题
cmd.CommandText = "Select Files From test3 Where ID='1'";
第一行只是定义一个SqlCommand型的变量cmd,并且将其连接设为con,其CommandText赋空值,第二行给cmd的CommandText属性重新赋值。