使用C#将文件上载到数据库 [英] Uploading files to the database using C#

查看:57
本文介绍了使用C#将文件上载到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好b $ b

我正在尝试将文件上传到Oracle数据库并检索它。但是,我没有发现它是一个全面的例子。有些代码我不明白。

。请看看我的代码并帮我做。

谢谢

示例I遵循: http:// csharp- guide.blogspot.co.uk/search/label/INSERT%20BLOB [ ^ ]>

我的代码:





Hi
I'm trying to upload files to Oracle database and retrieve it. However, I didn't find it a comprehensive example to follow. There are some codes I didn't understand .
.Please have a look at my codes and help me to do it.
Thank you
Example I followed : http://csharp-guide.blogspot.co.uk/search/label/INSERT%20BLOB[^]>
My code :


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OracleClient;
using System.Configuration;
using System.Data;
using System.IO;

public partial class Lecturer_Resources_to_upload : System.Web.UI.Page

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void cmdUpload_Click(object sender, EventArgs e)
    {
        try
        {
            if (fileUploadDocument.PostedFile.ContentLength > 0)
            {
                // Get the File name and Extension
                string FileName = Path.GetFileName(fileUploadDocument.PostedFile.FileName);
                string FileExtension = Path.GetExtension(fileUploadDocument.PostedFile.FileName);
                //
                // Extract the content of the Document into a Byte array
                int intlength = fileUploadDocument.PostedFile.ContentLength;
                Byte[] byteData = new Byte[intlength];
                fileUploadDocument.PostedFile.InputStream.Read(byteData, 0, intlength);
                //
                // Save the file to the DB
                string strConn = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
                objConn = new OracleConnection(strConn);
                //
                  StringBuilder strQueryBuilder = new StringBuilder("INSERT INTO RESOURCES(RESOURCES_ID, FILE_NAME, TYPE, RESOURCE_FILE) VALUES (");
                strQueryBuilder.Append("'1', ");
                strQueryBuilder.Append("'" + FileName + "', ");
                strQueryBuilder.Append("'" + FileExtension + "', ");
                strQueryBuilder.Append(" :RESOURCE_FILE)");

                String strQuery = strQueryBuilder.ToString();
                //
                OracleParameter blobParameter = new OracleParameter();
                blobParameter.ParameterName = "resources_file";
                blobParameter.OracleType = OracleType.Blob;
                blobParameter.Direction = ParameterDirection.Input;
                blobParameter.Value = byteData;

                objCmd = new OracleCommand(strQuery, objConn);
                objCmd.Parameters.Add(blobParameter);
                //
                objConn.Open();
                objCmd.ExecuteNonQuery();
                objConn.Close();
                //
                lblMsg.Text = "Document Uploaded Succesfully";
            }
        }
        catch (Exception ex)
        {
            lblMsg.Text = " Error uploading Document: " + ex.Message.ToString();
        }
    }
}





我得到错误的代码:



The codes where Im getting the errors :

StringBuilder strQueryBuilder = new StringBuilder("INSERT INTO RESOURCES(RESOURCES_ID, FILE_NAME, TYPE, RESOURCE_FILE) VALUES (");
               strQueryBuilder.Append("'1', ");
               strQueryBuilder.Append("'" + FileName + "', ");
               strQueryBuilder.Append("'" + FileExtension + "', ");
               strQueryBuilder.Append(" :RESOURCE_FILE)");

               String strQuery = strQueryBuilder.ToString();
               //
               OracleParameter blobParameter = new OracleParameter();
               blobParameter.ParameterName = "Resources_FILE";
               blobParameter.OracleType = OracleType.Blob;
               blobParameter.Direction = ParameterDirection.Input;
               blobParameter.Value = byteData;

               objCmd = new OracleCommand(strQuery, objConn);
               objCmd.Parameters.Add(blobParameter);
               //
               objConn.Open();
               objCmd.ExecuteNonQuery();
               objConn.Close();





我所遇到的问题是上面这些代码都是带下划线的红色。它给出了一个错误,它在当前上下文中不存在



The issue I have are these codes above are all underlined Red. It gives me an error that they don't exist in the current context

推荐答案

c#中的字符串是不可变的 - 无法更改 - 所以
string in c# is immutable - cant be changed - so the
string += 'blah' 

无法正常工作



我建议你谷歌'stringbuilder',然后使用它重写你的查询sql - 顺便说一句,你的字符串是查询,但你做了INSERT?也许你可以选择一个更好的名字

isn't going to work

I suggest you google 'stringbuilder', and rewrite your query sql using that - btw, your string is 'Query' but your doing an INSERT ? maybe you could pick a better name


// Get the File name and Extension
                string FileName = Path.GetFileName(fileUploadDocument.PostedFile.FileName);
                string FileExtension = Path.GetExtension(fileUploadDocument.PostedFile.FileName);
                //
                // Extract the content of the Document into a Byte array
                int intlength = fileUploadDocument.PostedFile.ContentLength;
                Byte[] byteData = new Byte[intlength];
                fileUploadDocument.PostedFile.InputStream.Read(byteData, 0, intlength);
                //
                // Save the file to the DB
                string strConn = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
                objConn = new OracleConnection(strConn);
                //
                 strQuery = "INSERT INTO RESOURCES(RESOURCES_ID, FILE_NAME, TYPE, RESOURCE_FILE) VALUES (";
                strQuery += "'1', ";
                strQuery += "'" + strFileName + "', ";
                strQuery += "'" + strFileExtension + "', ";
                strQuery += " :RESOURCE_FILE)";





看看



string FileName

string FileExtension



vs后来



+ strFileName

+ strFileExtension



这些不会使我对使用Stringbuilder的评论无效btw



look at

string FileName
string FileExtension

vs later on

+ strFileName
+ strFileExtension

these don't invalidate my comment about using Stringbuilder btw


用这个替换你的strQuery构建序列



replace your strQuery build sequence with this

StringBuilder strQueryBuilder = new StringBuilder("INSERT INTO RESOURCES(RESOURCES_ID, FILE_NAME, TYPE, RESOURCE_FILE) VALUES (");
strQueryBuilder.Append("'1', ");
strQueryBuilder.Append("'" + FileName + "', ");
strQueryBuilder.Append("'" + FileExtension + "', ");
strQueryBuilder.Append(" :RESOURCE_FILE)");

String strQuery = strQueryBuilder.ToString();





我刚用FileName和FileExtension的虚拟/ null defs做了它并编译了



I just did it with dummy/null defs for FileName and FileExtension and it compiled


这篇关于使用C#将文件上载到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆