vb.net コードを使用して SQL Server 列に PDF ファイルをメモリ ストリームで保存する方法は?
33856 次
2 に答える
5
ありがとうございました!!!今の私の解決策は:
public void SaveFile()
{
//Try
OpenFileDialog fd = new OpenFileDialog();
fd.Filter = "pdf file|*.pdf";
if (fd.ShowDialog == System.Windows.Forms.DialogResult.OK) {
//PdfDocument1.FilePath = fd.FileName
byte[] filebyte = null;
SqlConnection con = new SqlConnection("Data Source=LOCALHOS-A4AE79\\LOCALHOST1;Initial Catalog=library_alborz;Integrated Security=True");
SqlCommand cmd = default(SqlCommand);
filebyte = System.IO.File.ReadAllBytes(fd.FileName);
cmd = new SqlCommand("Insert into pdftbl ( pdffld ) Values(@pdf)", con);
//cmd.Parameters.Add("@filepath", SqlType.VarChar).Value = txtfilepath.Text
cmd.Parameters.Add("@pdf", SqlDbType.Binary).Value = filebyte;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Interaction.MsgBox("File saved into database", MsgBoxStyle.Information);
//Catch ex As Exception
// MsgBox(Err.Description, MsgBoxStyle.Exclamation)
//End Try
}
}
------------
// load pdf file
private void Button2_Click(System.Object sender, System.EventArgs e)
{
string strsql = null;
SqlConnection con = new SqlConnection("Data Source=LOCALHOS-A4AE79\\LOCALHOST1;Initial Catalog=library_alborz;Integrated Security=True");
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
try {
strsql = "select pdffld from pdftbl ";
da = new SqlDataAdapter(strsql, con);
da.Fill(ds, "tbl");
//Get image data from gridview column.
byte[] pdfData = Convert.ToByte(ds.Tables["tbl"].Rows[0][0]);
//Initialize pdf variable
//Read pdf data into a memory stream
using (MemoryStream ms = new MemoryStream(pdfData, 0, pdfData.Length)) {
ms.Write(pdfData, 0, pdfData.Length);
//Set pdf variable value using memory stream.
PdfDocument1.Load(ms);
PdfPageView1.Document = PdfDocument1;
PdfPageView1.Refresh();
}
} catch (Exception ex) {
MessageBox.Show(ex.ToString());
}
}
于 2012-04-25T06:06:17.443 に答える
3
まず、データベースのテーブルに varbinary(MAX) 型の列が必要です。バイト配列を保存できます。
次に、このようなコード行を使用して、PDF ファイルの内容をバイト配列として取得できます。
IO.File.ReadAllBytes("C:\my.pdf")
于 2012-04-21T19:16:17.000 に答える