我有许多非常大的矩阵(有时是双倍的,但也许是其他类型),它们的尺寸可能不同。我想将整个数组存储为varbinary(max)
我能够很好地存储记录(我认为)。但是,当我来阅读数据时,我可以创建一个具有多个维度的新双数组。我可以从SQL中读取存储的数据(我认为)。我现在无法弄清楚的是我使用的Buffer.BlockCopy()操作的反转。
SQL表如下所示:
create table test_varbinary_table(
id int not null IDENTITY PRIMARY KEY ,
name varchar(256) not null,
rows int not null,
cols int not null,
vb_data varbinary(max) null
)
写入数据的存储过程在这里:(删除因为可能只是混淆了问题)编写示例记录的C#在这里:(删除因为我认为这部分有效,我不知道它有助于解决问题)
GetData存储过程是这样的:
ALTER PROCEDURE [dbo].[GetData]
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT * from test_varbinary_table
END
我看了桌子
try
{
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
SqlDataReader rdrMatrix = null;
DataTable tblMatrix = new DataTable();
using (SqlCommand cmd = new SqlCommand("dbo.GetData", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
rdrMatrix = cmd.ExecuteReader(CommandBehavior.CloseConnection);
tblMatrix.Load(rdrMatrix);
String strResult = "";
foreach (DataRow dr in tblMatrix.Rows)
{
int r = 2, c = 3;
double[,] dary = new double[r, c];
byte[] retData = new byte[6 * sizeof(double)];
String retName;
retName = dr["name"].ToString();
strResult += retName + "\r\n";
retData = (byte[])dr["vb_data"]; // potential problem?
Buffer.BlockCopy(retData, 0, dary, 0, dary.Length); // potential problem?
for (int ri = 0; ri< 2; ri++)
{
strResult += " ";
for (int ci = 0; ci<3; ci++)
{
strResult += " " + dary[ri,ci] ;
}
strResult += "\r\n";
}
strResult += "\r\n" ;
}
textBox1.Text = strResult;
}
conn.Close();
}
//textBox1.Text = "No Failure!";
}
对于编写代码,我只是手动修改内容以写入一些不同的记录。当我在SSMS中检查表时,我可以看到它已成功添加行。虽然我无法读取字节流,但如果我更改数据它会发生变化,如果我不更改数据则它会保持不变。但是,当我运行c#代码来读取记录时,每次都得到相同的结果...零。
输出如下:
foo
0 0 0
0 0 0
bar
0 0 0
0 0 0
所以它不是在读取@data字段,或者它没有正确地将它转换为byte []或者blockcopy无法正常工作。 (我认为。)无论如何,它返回零而不是我存储的数据。
我能够使用BlockCopy将矩阵编码为可以写入varbinary的字节数组来解决问题。在读取时,我使用BitConverter从字节数组解码为double [,]。我不知道为什么我无法让BlockCopy在另一个方向上工作。我错过了什么。
无论如何,到目前为止我的解决方案使用了一些不同的代码,但这些是必不可少的元素:
我存储矩阵的表:
CREATE TABLE [dbo].[test_varbinary_table](
[id] [int] IDENTITY(1,1) NOT NULL,
[name] [varchar](8) NOT NULL,
[version] [varchar](16) NOT NULL,
[date] [datetime] NOT NULL,
[notes] [varchar](max) NULL,
[rows] [int] NOT NULL,
[cols] [int] NOT NULL,
[checksum] [float] NULL,
[vb_data] [varbinary](max) NULL
)
将double [,]编码为字节数组的方法,该数组将直接写入varbinary:
public byte[] DoubleMatrixToByteArray(double[,] doubleArray)
{
int rows = doubleArray.GetLength(0);
int cols = doubleArray.GetLength(1);
byte[] byteArray = new byte[rows * cols * sizeof(double)];
Buffer.BlockCopy(doubleArray, 0, byteArray, 0, byteArray.Length);
return byteArray;
}
从varbinary解码回2D双数组的方法:
public double[,] VarBinaryToDoubleMatrix (byte[] byteArray, int rows, int cols)
{
double[,] doubleArray = new double[rows, cols];
for (int r=0; r<rows; r++)
{
for (int c=0; c<cols; c++)
{
doubleArray[r,c] = BitConverter.ToDouble(byteArray, r * cols * sizeof(double) + c * sizeof(double));
}
}
return doubleArray;
}
校验:
// Summation of all cells in a matrix, just a quick verification check.
// The intent is to verify that when a matrix is read from the database,
// the reconstructed matrix has the same checksum as the one that was saved.
//
public double computeChecksum(double[,] dblArray)
{
int rank = dblArray.Rank; // rank is the number of dimensions of an array.
int rows = dblArray.GetLength(0);
int cols = dblArray.GetLength(1);
double sum = 0.0;
for (int r = 0; r < rows; r++)
{
for (int c = 0; c < cols; c++)
{
sum += dblArray[r, c];
}
}
return sum;
}
}
我用校验和保存数组,我和varbinary字段一起读取。然后我重新计算校验和并将其与保存的校验和值进行比较。