Problems encountered when storing pictures in C # and taking pictures out of the database

table of Contents

1 Introduction

2. Operand type conflict: nvarchar is incompatible with image

Problem Description

The cause of the problem

solution

3. The parameterized query in C # '(@ PERSONAL_PHOTO image, @ SURNAME nvarchar (4000), @ MIN_NAME nvarcha' requires the parameter '@PASSPORT', but the parameter is not provided.

Problem Description

Cause of the problem

solution

4. When obtaining image data from the database, I encountered the problem of how to determine whether the obtained image data is null

Problem Description

reference

Cheer yourself up every day, come on!


1 Introduction

Recently, I am working on a C # project based on the MVC framework. I encountered that the web terminal needs to transfer user information to the database saved in the background and obtain it from the data and then echo it back to the web interface. Some questions, record here

2. Operand type conflict: nvarchar is incompatible with image

Problem Description

When submitting the front-end information, there is an image that was not uploaded. This problem occurs when writing to the database. The image type in the database is image.

The cause of the problem

If the database is of the Image type, when the insert statement is executed, if the inserted value is DBNull.Value, it prompts: Operand type conflict: nvarchar is incompatible with image; the reason for this problem is that the reason for not specifying DbType. In most cases, when using SqlParameter, you do not need to specify the data type of the parameter (DbType or SqlDbType). ADO.Net will automatically determine the data type according to the type of value. Even if ADO.Net cannot determine it, the SQLServer database server Make most of the judgment (of course, some performance will be lost).
But for the Image type in this program, I am not so lucky. If DbType is not specified and the data is Null / DbNull, ado.net recognizes the data type as nvarchar, and the above error occurs. So just specify DbType or SqlDbType.

solution

new SqlParameter("@Photo", SqlDbType.Image) {Value=recruitment.PASSPORT}

3. The parameterized query in C # '(@ PERSONAL_PHOTO image, @ SURNAME nvarchar (4000), @ MIN_NAME nvarcha' requires the parameter '@PASSPORT', but the parameter is not provided.

Problem Description

After solving the previous problem, a new problem appeared, the picture was still not uploaded, so recruitment.PASSPORT = null

Cause of the problem

Directly think of null in C # as null in sql, so it is considered that the parameter is not provided

solution

NUll in C # is not the same as null in SQL. The null in SQL is expressed in C # as DBNull.Value

new SqlParameter("@Photo", SqlDbType.Image) {Value= TableSqlCon.TODBNull(recruitment.PASSPORT)}

 public static object TODBNull(byte[] image)
        {
            if (image != null)
            {
                return image;
            }
            else
            {
                return DBNull.Value;
            }
        }

4. When obtaining image data from the database, I encountered the problem of how to determine whether the obtained image data is null

Problem Description

The picture data obtained from the data will be forced to be converted to byte [] type, such as recruitment.PASSPORT = (byte []) dt.Rows [0] [21]; but when the picture is empty, an error is reported: System cannot be used. DBNull is converted to byte [], and then I try to judge whether it is empty, if (dt.Rows [0] [21]! = Null) and other attempts, all fail

solution

 Correct writing: if (dt.Rows [0] [21] is System.DBNull)

 if (dt.Rows[0][21] is System.DBNull)
                    {
                        recruitment.PASSPORT = null;
                    }
                    else
                    {
                        recruitment.PASSPORT =(byte[]) dt.Rows[0][21];
                    }

reference

https://blog.csdn.net/weixin_30675967/article/details/99175174?depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1&utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1

https://blog.csdn.net/kaixin_5200_kaixin/article/details/23115953

https://blog.csdn.net/weixin_30270889/article/details/97839671

Cheer yourself up every day, come on!

Published 30 original articles · Like1 · Visits1158

Guess you like

Origin blog.csdn.net/chunchunlaila/article/details/105353856