簡體   English   中英

在C#中將Blob數據從一個表復制到另一個表

[英]copy blob data from one table to another in c#

基本上我有一個服務,它查看兩個表-一個駐留在遠程服務器上,另一個駐留在本地。 我正在嘗試編寫一個程序,該程序將從遠程服務器中選擇任何必需的文件並將其復制到本地。 我可以將其用於標准記錄,但是我該如何處理C#中的Blob-我只是從語言入手,所以要保持溫柔

我所擁有的片段如下

public static void BroadcastCheck(String ip_addr)
    {
        OdbcConnection local = new OdbcConnection("DSN=local");
        OdbcConnection cloud = new OdbcConnection("DSN=cloud");
        local.Open();
        cloud.Open();
        OdbcCommand update1 = new OdbcCommand("UPDATE exchange set status = '1' where `status`='0' and inp_date=chg_date and LEFT(filename,12)='" + ip_addr + "' and type='UPDATE'", cloud);
        update1.ExecuteNonQuery();
        OdbcCommand broadcastSelect = new OdbcCommand("select * from exchange where inp_date=chg_date and LEFT(filename,12)='" + ip_addr + "' and status='1' and type='UPDATE'", cloud);
        OdbcDataReader DbReader = broadcastSelect.ExecuteReader();
        int fCount = DbReader.FieldCount;
        byte[] outByte = new byte[500]; 
        while (DbReader.Read())
        {
           String type = DbReader.GetString(0);
           String filename = DbReader.GetString(1);
           String data = DbReader.GetBytes(1);
           OdbcCommand broadcastCopy = new OdbcCommand("INSERT INTO exchange(type,filename) VALUES('"+type+"','"+filename+"'"+data+")", local);
           broadcastCopy.ExecuteNonQuery();


        }
        itouchcloud.Close();
        itouchlocal.Close();
        Console.Write("Broadcast Check Completed \n");

    }

基本上查詢雲數據庫並可能返回多個結果,我想處理返回的每個記錄並將其復制到本地數據庫。 我環顧四周,似乎無法真正得到一個不錯的解決方案,我可以在Visual FoxPro 9中簡單地做到這一點,所以我想還有一個類似的解決方案。

任何幫助表示贊賞:)

答案的第一部分是,如果可以,請避免使用動態SQL。 當您應該使用“ ... VALUES(?,?,?)”時,您正在使用“ ... VALUES('“ + type +”','“ +文件名+”'“ +數據+”)“。

然后,例如使用

// sample: the name of the parameter (here @Type) can be anything, and the type and length should match your schema.
broadcastCommand.Parameters.Add("@Type", OleDbType.VarChar, 10).Value = type; 

問號將按您指定的順序替換為參數,因此您應按該順序依次添加類型,文件名和數據。

現在,您指定的值還應該與您要插入的字段類型相對應。 因此,您可能希望變量不是String,String,String,而是String,String,byte []類型。

有大約一百萬個理由不動態構建查詢,因此,我建議您研究如何在OdbcCommand上使用Parameters集合。 這里開始。

更新

通常,您只需使用索引器[]就可以獲取DataReader值,而無需遍歷GetXXX()方法。 對於字節數組,這通常更簡單,因為您無需事先知道或嘗試猜測其長度。

您可以通過以下方式將代碼轉換為使用索引器:

String type = (string)DbReader[0];
String filename = (string)DbReader[1];
byte[] data = (byte[])DbReader[2];

請注意,您的GetBytes()調用最初在其中具有1,但是我假設您沒有嘗試獲取文件名字段的字節。 因此,如果您的byte[]數據在另一個字段中,請改用該字段。 但是請注意,您也可以輕松地使用字符串字段名稱(下次需要閱讀代碼時可能會更清楚):

String type = (string)DbReader["type"]; // replace with whatever your fields are actually called
String filename = (string)DbReader["filename"];
byte[] data = (byte[])DbReader["data"];

有時,您使用相同的字段來使用filenamedata ,因為data實際上不在數據庫中,而是您想要獲取文件名並讀取該文件系統對象作為插入查詢的數據,您將需要使用不同的方法。

byte[] data = System.IO.File.ReadAllBytes(filename); // requires .NET 2.0+

無論哪種方式填充變量,都可以使用上述參數化查詢插入變量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM