-4

我开发了一个 Windows 应用程序来扫描图像。扫描图像后,我想将其直接保存到数据库而不是本地机器中......我使用的代码如下

try
{
    String str = string.Empty;
    WIA.CommonDialogClass scanner;
    ImageFile imageObject;

    scanner = new CommonDialogClass();
    imageObject = scanner.ShowAcquireImage
        (WiaDeviceType.ScannerDeviceType, 
        WiaImageIntent.ColorIntent,
        WiaImageBias.MinimizeSize,
        ImageFormat.Jpeg.Guid.ToString("B"), 
        false, 
        true,
        true);

    str = DateTime.Now.ToString();
    str = str.Replace("/", "");
    str = str.Replace(":", "");
    Directory.CreateDirectory("D:\\scanned1");
    //            MessageBox.Show(string.Format("File Extension = {0}\n
    //\nFormat = {1}", imageObject.FileExtension, imageObject.FormatID));
    imageObject.SaveFile(@"D:\scanned1\lel" + str + ".jpg");
    MessageBox.Show("Scanning Done");
}
catch (Exception ex)
{
    MessageBox.Show("Please check if the scanner is connected properly.");
}

我不想将其保存到 D 驱动器,而是将其保存到数据库中.....我该怎么做?请回复...

4

1 回答 1

1

我不知道“ImageFile”到底是什么,但应该有一种方法可以将其转换为字节数组(byte[])。之后,您需要将该数组插入 sql 中的 varbinary 字段。像这样的东西:

using(SqlCommand cmd = new SqlCommand("INSERT INTO MyTable (myvarbinarycolumn) VALUES (@myvarbinaryvalue)", conn))
{
    cmd.Parameters.Add("@myvarbinaryvalue", SqlDbType.VarBinary, myarray.length).Value = myarray;
    cmd.ExecuteNonQuery();
}

ofc sqlcommand 应该打开并且有效。myarray 的类型为 byte[]

于 2013-10-08T09:31:31.880 回答