怎样才能把文本文件以二进制流的方式存进数据库

怎样才能把文本文件以二进制流的方式存进数据库,第1张

文件流的方式,把从文件中读出的数据转换成二进制,从数据库中读出就是反方向的:** void button1_Click(object sender, EventArgs e){byte[] bufferbuffer = File.ReadAllBytes(\"readme.doc\")//读取文件内容//创建连接SqlConnection connect = new SqlConnection(@\"Integrated Security=SSPIPersist Security Info=FalseInitial Catalog=BSPlatform2008Data Source=.\\SqlExpress\")SqlCommand cmd = connect.CreateCommand()cmd.CommandText = \"INSERT INTO Tmp (FileContent) VALUES (@FileContent)\"//FileContent字段是Image类型cmd.Parameters.Add(\"@FileContent\", SqlDbType.Image)cmd.Parameters[\"@FileContent\"].Value = buffer//接受byte[]类型的值connect.Open()cmd.ExecuteNonQuery()connect.Close()} 查看更多答案>>

private int WriteToDB(string strName, string strType, ref byte[] Buffer) { int nFileID = 0// Create connection OleDbConnection dbConn = new OleDbConnection(GetConnectionString())// Create Adapter OleDbDataAdapter dbAdapt = new OleDbDataAdapter("SELECT * FROM tblFile", dbConn)// We need this to get an ID back from the database dbAdapt.MissingSchemaAction = MissingSchemaAction.AddWithKey// Create and initialize CommandBuilder OleDbCommandBuilder dbCB = new OleDbCommandBuilder(dbAdapt)// Open Connection dbConn.Open()// New DataSet DataSet dbSet = new DataSet()// Populate DataSet with data dbAdapt.Fill(dbSet, "tblFile")// Get reference to our table DataTable dbTable = dbSet.Tables["tblFile"]// Create new row DataRow dbRow = dbTable.NewRow()// Store data in the row dbRow["FileName"] = strNamedbRow["FileSize"] = Buffer.LengthdbRow["ContentType"] = strTypedbRow["FileData"] = Buffer// Add row back to table dbTable.Rows.Add(dbRow)// Update data source dbAdapt.Update(dbSet, "tblFile")// Get newFileID if( !dbRow.IsNull("FileID") ) nFileID = (int)dbRow["FileID"]// Close connection dbConn.Close()// Return FileID return nFileID} 写入库。 private void ShowTheFile(int FileID) { // Define SQL select statement string SQL = "SELECT FileSize, FileData, ContentType FROM tblFile WHERE FileID = " + FileID.ToString()// Create Connection object OleDbConnection dbConn = new OleDbConnection(GetConnectionString())// Create Command Object OleDbCommand dbComm = new OleDbCommand(SQL, dbConn)// Open Connection dbConn.Open()// Execute command and receive DataReader OleDbDataRea

用streamreader直接读成byte[] 然后用ado什么的都好了,存到access的data结构里

至于这个读取再显示到另一word中是什么意思呢?

是调用word程序打开这个word呢?如果这样,就要把access读到的byte【】用writer写到temp.doc,然后调用shellexcute执行 temp.doc就ok了

至于shellexcute从系统dll中调用要使用下面代码

[DllImport("shell32.dll ")]

public static extern int ShellExecute(IntPtr hwnd, StringBuilder lpszOp,

StringBuilder lpszFile, StringBuilder lpszParams, StringBuilder lpszDir, int FsShowCmd)

如果不保存word的话,你还是自己设计个格式,或者把文字存文字,图片存图片,。。。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/9928628.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-03
下一篇2023-05-03

发表评论

登录后才能评论

评论列表(0条)

    保存