【问题标题】:Varbinary(max) insertion - duplication of varbinary(max) fields between unrelated db recordsVarbinary(max) 插入 - 在不相关的数据库记录之间重复 varbinary(max) 字段
【发布时间】:2014-12-19 15:13:10
【问题描述】:

我试图在这里粗略地提出一个概念,但我遇到了一个奇怪的问题。供您娱乐...

总结:

我想做的是在 C# 代码中生成一个图像,将其插入到 SQL 2012 db 的 varbinary(max) 字段中,然后将其显示在 SSRS 报告中。 (是的,我记得在每次测试后删除 rdl.data 缓存。) 图像只是一个带有突出显示的弧段的圆。该数据库表基于http://www.kodyaz.com/articles/display-database-image-using-sql-server-2008-reporting-services.aspx 中的表,我唯一更改的是添加主键。我创建了一个 DataSet (xsd) 文件并将表格从服务器资源管理器拖到它上面。

奇怪的是,虽然我成功地插入了记录并且 fname 字段被正确插入,但据我所知,二进制字段总是与添加的第一个字段重复。我还应该提到,当我最初通过写入本地磁盘文件来测试主要图形代码时,图像总是正确输出。

起初我认为从我拥有的 Web 服务执行此操作可能会出现问题,但我在本地测试 Web 应用程序中运行代码时得到完全相同的行为。此外,所有代码都是该方法的本地代码。没有什么远程持久的。诚然,我只涉足图形。这是从其他各种参考资料中窃取的。这是代码的核心。我在 try/catch 中有它,它没有给出任何异常。

Bitmap x;
Pen p = new Pen(System.Drawing.Color.Red, 5);
Pen p2 = new Pen(System.Drawing.Color.Pink, 5);
SolidBrush b;
RectangleF rf;
Graphics gr;       
p = new Pen(System.Drawing.Color.Red, iThick);
p2 = new Pen(System.Drawing.Color.Pink, iThick);
x = new Bitmap(500, 500);
rf = new RectangleF(0, 0, x.Width, x.Height);
gr = Graphics.FromImage(x);
b = new SolidBrush(System.Drawing.Color.White);
gr.FillRectangle(b, rf);
rf = new RectangleF(p.Width, p.Width, x.Width - p.Width * 2, x.Height - p.Width * 2);
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
gr.DrawEllipse(p2, rf);
gr.DrawArc(p, rf, fStartAngle, fSweepAngle);
byte[] bytes;
using (System.IO.MemoryStream sampleStream = new System.IO.MemoryStream())
{                
      x.Save(sampleStream, System.Drawing.Imaging.ImageFormat.Bmp);                
      bytes = sampleStream.ToArray();
      dsCountryTableAdapters.DBFiles1TableAdapter ta = new dsCountryTableAdapters.DBFiles1TableAdapter();
      ta.Insert(sImageName, bytes);
}

【问题讨论】:

    标签: sql-server graphics dataset draw varbinarymax


    【解决方案1】:

    无论最初的问题是什么,将插入方法从 TableAdapter.Insert 更改为 SQLConnection/SQLCommand 设置即可解决问题。 (数据表结构没有变化。)还是很好奇为什么 TA 不工作。

    using (SqlCommand cmd = new SqlCommand("INSERT INTO DBFiles(fname, [file]) VALUES (@fname, @file)", conn))
                {
                    using (System.IO.MemoryStream sampleStream = new System.IO.MemoryStream())
                    {
                        x.Save(sampleStream, System.Drawing.Imaging.ImageFormat.Bmp);
                        bytes = sampleStream.ToArray();
                    }                
                    cmd.Parameters.Add("@fname", SqlDbType.NVarChar, 1000).Value = sImageName;
                    cmd.Parameters.Add("@file", SqlDbType.VarBinary, -1).Value = bytes;
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();
                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多