Some strange problems that C# form applications may encounter

I recently took a program training class and wrote a management program, mainly using C#. On the VS2017 platform, there are naturally many strange problems in the development process, so make a record.

There are the following questions:

问题1:.Conversion failed when converting from a character string to uniqueidentifier.

Problem 2: ExecuteNonQuery requires a Connection that is already open and available. The current state of the connection is closed.

 Issue 3: CS0012 The type 'Object' is defined in an assembly that is not referenced. A reference to assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' must be added.

The main database structure currently used is as follows.

Many errors in connecting to the database are reported on the special type of ID, which is the unique identifier uniqueidentifier. The type in the program is Guid, but because identifiers are actually indispensable in a database, these problems are also Can't get around.

问题1:Conversion failed when converting from a character string to uniqueidentifier.

The problem background is that the input id format is a string, and then there will be problems when querying. For the convenience of viewing, I put the problem in the sql query, as follows:

Select * from Operator where id='3a9a2894-f9f6-4ffe-b8e6-a67e949f276c'

This problem has troubled me for a long time. It is often disconnected during the running of the program. What is the reason for querying the information... I have forgotten. The specific solution is as follows:

select * from Operator where id =CAST('3a9a2894-f9f6-4ffe-b8e6-a67e949f276c' AS UNIQUEIDENTIFIER)

Problem 2: ExecuteNonQuery requires a Connection that is already open and available. The current state of the connection is closed.

The way this question is actually said is very straightforward, but I have forgotten it several times and got stuck for a while. The specific background is that an error will be reported when connecting to the database for query. The solution is to remember to open the connection, which is Open below.

 using (SqlConnection conn = new SqlConnection(connStr))
 {
     conn.Open();
     SqlCommand commUpdate = new SqlCommand(sql, conn);
     int n = commUpdate.ExecuteNonQuery();
     if (n > 0)
     {
        flag = true;
     }
}

 Issue 3: CS0012 The type 'Object' is defined in an assembly that is not referenced. A reference to assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' must be added. 

 This error is very strange and weird. The specific manifestation is that the created class library cannot reference some libraries such as System.Configuration in the dependencies, and some very weird errors may be reported. Anyway, if you don’t understand it, the following may appear.

 I searched almost all related blogs and found no solution. Later, I discovered the nuances of the two class libraries by chance. If you look carefully, there are actually two types of class libraries, Standard and Framework. I created Standard at the beginning, so many It is not compatible, and there will be no problem if it is changed to Framework later. If you are like me and report a similar error in the form application, then you can consider whether it is caused by the wrong type of creation.

Guess you like

Origin blog.csdn.net/weixin_60360239/article/details/129286925