如何将txt,doc等文件以二进制形式保存在数据库中
如何将txt,doc,pdf等文件以二进制形式保存在数据库中并应该怎么样将它从数据库中读出来.(希望能有代码,并给上注释).因为我的积分不多所以暂时就只能先给10分,如果...
如何将txt,doc,pdf等文件以二进制形式保存在数据库中并应该怎么样将它从数据库中读出来.(希望能有代码,并给上注释).因为我的积分不多所以暂时就只能先给10分,如果不够以后将一定补上.希望大家帮帮忙,急切需求!小弟先在此谢过各位!
展开
1个回答
2013-09-14
展开全部
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"] = strName; dbRow["FileSize"] = Buffer.Length; dbRow["ContentType"] = strType; dbRow["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
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询