且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将图像插入数据库

更新时间:2023-02-24 08:53:53

没有映射存在  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.[^]