将图像插入数据库 [英] Inserting images to a database

查看:65
本文介绍了将图像插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据库中插入图片时遇到一些问题,它无法正常工作,请问我做错了什么?



  //   1。使用默认数据库打开数据库连接 
尝试
{
string connectionString = TestUI.Properties.Settings.Default.UserStoreConnectionString;
使用(SqlConnection con = new SqlConnection(connectionString))
{
bool userExists = false ;
con.Open();
if (userExists)
{
string commandText = UPDATE [UserStore]。[dbo]。[user] SET Name = @Name,Surname = @Surname,@ Usrimage WHERE UserID = @UserID;;
SqlCommand command = new SqlCommand(commandText,con);
command.Parameters.AddWithValue( @ UserId 0 );
MemoryStream _stream = new MemoryStream();
pcbMember.Image.Save(_ stream,System.Drawing.Imaging.ImageFormat.Jpeg);
byte [] _pic = _stream.ToArray();
command.Parameters.AddWithValue( @ UserImage,_ pic);
if (txtName.Text!= null ){command.Parameters.AddWithValue( @ Name,txtName.Text); }
if (txtSurname.Text!= null ){command.Parameters.AddWithValue(< span class =code-string>
@ Surname,txtSurname.Text); }
command.ExecuteNonQuery();
Debug.WriteLine( 现有用户已保存。);
}
else if (!userExists)
{
int _count = 0 ;
string commandText = INSERT INTO [UserStore ]。[dbo]。[user] VALUES(Name = @ Name,Surname = @Surname,UserPicture = @ UserPicture,UserID = @UserID);;
SqlCommand command = new SqlCommand(commandText,con);
command.Parameters.AddWithValue( @ UserID,_ count ++);
command.Parameters.AddWithValue( @ Name,txtName.Text);
command.Parameters.AddWithValue( @ Surname,txtSurname.Text);
command.Parameters.AddWithValue( @ UserPicture,pcbMember.Image);
command.ExecuteNonQuery();
Debug.WriteLine( 新用户保存。);
con.Close();
}
}
}
catch (例外情况)
{
Debug.WriteLine (ex.Message);
Debug.WriteLine(ex.Source);
Debug.WriteLine(ex.StackTrace);
}
最后
{

}

解决方案

没有映射存在  from  object  type  System.Drawing.Bitmap  一个已知的托管提供者本机
System.Data





我花了几个小时调试这个,我的书签可用其他。 [ ^ ]


I'm having a little problem inserting images within a database, it just won't work, what am I doing wrong please?

//1. Open database connection using default database
         try
         {
             string connectionString = TestUI.Properties.Settings.Default.UserStoreConnectionString;
             using (SqlConnection con = new SqlConnection(connectionString))
             {
                 bool userExists = false;
                 con.Open();
                 if (userExists)
                 {
                     string commandText = "UPDATE [UserStore].[dbo].[user] SET Name = @Name, Surname = @Surname, @Userimage WHERE UserID = @UserID;";
                     SqlCommand command = new SqlCommand(commandText, con);
                     command.Parameters.AddWithValue("@UserId", 0);
                     MemoryStream _stream = new MemoryStream();
                     pcbMember.Image.Save(_stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                     byte[] _pic = _stream.ToArray();
                     command.Parameters.AddWithValue("@UserImage", _pic);
                     if (txtName.Text != null) { command.Parameters.AddWithValue("@Name", txtName.Text); }
                     if (txtSurname.Text != null) { command.Parameters.AddWithValue("@Surname", txtSurname.Text); }
                     command.ExecuteNonQuery();
                     Debug.WriteLine("Existing user saved.");
                 }
                 else if (!userExists)
                 {
                     int _count = 0;
                     string commandText = "INSERT INTO [UserStore].[dbo].[user] VALUES (Name = @Name, Surname = @Surname, UserPicture = @UserPicture, UserID = @UserID);";
                     SqlCommand command = new SqlCommand(commandText, con);
                     command.Parameters.AddWithValue("@UserID", _count++);
                     command.Parameters.AddWithValue("@Name", txtName.Text);
                     command.Parameters.AddWithValue("@Surname", txtSurname.Text);
                     command.Parameters.AddWithValue("@UserPicture", pcbMember.Image);
                     command.ExecuteNonQuery();
                     Debug.WriteLine("New user saved.");
                     con.Close();
                 }
             }
         }
         catch (Exception ex)
         {
             Debug.WriteLine(ex.Message);
             Debug.WriteLine(ex.Source);
             Debug.WriteLine(ex.StackTrace);
         }
         finally
         {

         }

解决方案

No mapping exists from object type System.Drawing.Bitmap to a known managed provider native type.
System.Data



I spent a few hours debugging this, my bookmarks are available here.[^]


这篇关于将图像插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆