如何从 SQL Server 插入和检索图像?

发布于 2024-12-08 07:01:29 字数 1073 浏览 0 评论 0原文

我已经编写了在 SQL Server 中插入图像的代码,但它引发了异常:

字符串或二进制数据将被截断。该声明已终止。

这是我插入图像的代码:

FileStream imageStream = new FileStream(CvVariables.IMAGE_PATH, FileMode.Open, FileAccess.Read);
int fileLangth = (int)imageStream.Length;
byte[] imageInBytes = new byte[fileLangth];
imageStream.Read(imageInBytes, 0, (int)fileLangth);
Cv_Customer_Information addNewCustomer = new Cv_Customer_Information
{
      UserID = this.NewCustomerTextUserName.Text,
      UserImage =new System.Data.Linq.Binary(imageInBytes),
      Date = this.NewCustomerDate.SelectedDate.ToString(),
      Name = this.NewCustomerTextBoxName.Text,
      Phone = this.NewCustomerTextBoxPhone.Text,
      Email = this.NewCustomerTextBoxEmail.Text,
      NationalID = this.NewCustomerTextBoxNationalID.Text,
      Address = this.NewCustomerTextBoxAddress.Text
};
singupDataContext.Cv_Customer_Informations.InsertOnSubmit(addNewCustomer);
singupDataContext.SubmitChanges();

我也不明白如何从 SQL Server 检索图像?

更新:我在 UserImage 字段中使用图像数据类型,并且正在使用 WPF

I have written code to insert an Image in SQL server but it throws an exception:

String or binary data would be truncated. The statement has been terminated.

Here is my Code for insert image:

FileStream imageStream = new FileStream(CvVariables.IMAGE_PATH, FileMode.Open, FileAccess.Read);
int fileLangth = (int)imageStream.Length;
byte[] imageInBytes = new byte[fileLangth];
imageStream.Read(imageInBytes, 0, (int)fileLangth);
Cv_Customer_Information addNewCustomer = new Cv_Customer_Information
{
      UserID = this.NewCustomerTextUserName.Text,
      UserImage =new System.Data.Linq.Binary(imageInBytes),
      Date = this.NewCustomerDate.SelectedDate.ToString(),
      Name = this.NewCustomerTextBoxName.Text,
      Phone = this.NewCustomerTextBoxPhone.Text,
      Email = this.NewCustomerTextBoxEmail.Text,
      NationalID = this.NewCustomerTextBoxNationalID.Text,
      Address = this.NewCustomerTextBoxAddress.Text
};
singupDataContext.Cv_Customer_Informations.InsertOnSubmit(addNewCustomer);
singupDataContext.SubmitChanges();

I also don`t understand how to retrieve images from SQL Server?

update: I use image Data Type in UserImage field and I am working with WPF

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

手心的海 2024-12-15 07:01:30

好吧,这个错误意味着您的字段之一(字符串或二进制)的数据比将存储它的列的数据库定义长。

例如,这可以是您的用户名。如果数据库是 varchar(8) 并且您尝试分配 10 个字符的名称,您将收到此错误。

从错误消息中您无法推断出哪个字段导致了错误。它可以是任何字符串或您的二进制数据。将输入与数据库定义交叉检查以查找导致错误的字段/列。

解决方案:提供较小的数据或增加数据库中的长度。

Well, this error means that the data of one of your fields (string or binary) is longer than the database definition of the column in which it will be stored.

This can be for example your username. If the database is a varchar(8) and you try to assign a name of 10 characters, you will get this error.

From the errormessage you cannot deduct what field is causing the error. It can be any string or your Binary data. Crosschek the input with the database definition to find the field/columns that is causing the error.

Solution: provide smaller data or increase the length in the database.

装迷糊 2024-12-15 07:01:30

这里的问题是保存图像的列不够大,无法容纳您要插入的图像。这意味着您必须增加其长度才能插入您想要的对象。

The problem here is that the column that is holding your image is not big enough to hold the image you are inserting. This means that you will have to increase its length in order to be able to insert the object you want to.

秋意浓 2024-12-15 07:01:30

我没有检查过代码,但我记得我使用过类似的东西。

Image image;
using (MemoryStream stream = new MemoryStream(imageByteArray,0,imageByteArray.Length)) 
{ 
    stream.Write(imageByteArray,0,imageByteArray.Length); 
    image = Image.FromStream(stream,true); 
}

I haven't checked the code but I remember that I used something similar.

Image image;
using (MemoryStream stream = new MemoryStream(imageByteArray,0,imageByteArray.Length)) 
{ 
    stream.Write(imageByteArray,0,imageByteArray.Length); 
    image = Image.FromStream(stream,true); 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文