1

この問題に対する多くの解決策を見てきました。人々はただ使用するだけCommand.ExecuteScalar as byte[];ですが、SQL クエリは一度に 1 つの varbinary フィールドを取得しています。約 30k 行の varbinary エントリを選択しようとしていますが、それらは byte[] にあり、逆シリアル化されています。

これが私のコードです:

public void MNAdapter()
    {
        IsoStorage retVal = new IsoStorage();

        SqlConnectionStringBuilder csb = new SqlConnectionStringBuilder();
        csb.DataSource = @"LocalMachine\SQLDEV";
        csb.InitialCatalog = "Support";
        csb.IntegratedSecurity = true;
        string connString = csb.ToString();

        using (SqlConnection conn = new SqlConnection(connString))
        {
            conn.Open();

            SqlCommand command = conn.CreateCommand();
            command.CommandText = @"SELECT S.Settings
from Support.dbo.SavedLocalSettings S
inner join WebCatalog.Published.People P
on P.PKey = S.PeopleLink
inner join WebCatalog.Published.Company C
on P.Link = C.PeopleList
where S.DateSaved >= GETDATE()-34
and C.PKey != '530F4622-C30D-DD11-A23A-00304834A8C9'
and C.PKey != '7BAF7229-9249-449E-BEA5-4B366D7ECCD1'
and C.PKey != 'CCBB2140-C30D-DD11-A23A-00304834A8C9'
and S.CompanyName not like 'Tech Support%'
Group By S.PeopleLink, S.Settings";

using (SqlDataReader reader = command.ExecuteReader(CommandBehavior.SequentialAccess))
                {
                    //DataTable dt = new DataTable();
                    //dt.Load(reader);

                    byte[] blob = null;
                    BinaryFormatter bf = new BinaryFormatter();
                    bf.Binder = new CustomBinder();
                    while (reader.Read())
                    {
                        reader.GetBytes(0,0,blob,0,100000);
                        Console.WriteLine(blob.ToString());
                        retVal = bf.Deserialize(new MemoryStream(blob)) as IsoStorage;
                    }
                }
            }
        }

冗長だと思っていたにもかかわらず、最初にそれらをデータタブに入れてみましたが、整数として読み込まれます。

エラーは発生せず、データはデータ リーダーに送られreader.GetBytes(0,0,blob,0,100000);ますが、blob が null のままであるため、実行されていないようです。

4

2 に答える 2

2

使用しない理由:

blob = (byte[])reader.Items["Settings"];

または

blob = (byte[])reader["Settings"];
于 2013-10-18T19:11:34.663 に答える
1

reader.GetBytes(0,0,blob,0,100000); このメソッドがバイト配列を作成することを期待しています。そうではありません-既存の配列への参照が必要です。アレイを自分で準備する必要があります。

于 2013-10-18T19:15:10.557 に答える