Home>

There is a database.

If Not Exists (Select * from sys.databases Where Name= 'Test')
Begin.
    CREATE DATABASE [DTELEVATOR]
End.
use test;
CREATE TABLE [ORGANIZATION]
(
    [Organization_id] Bigint Primary Key Identity,
    [Organization_name] VARCHAR (50) Not NULL,
)
CREATE TABLE [USER]
(
    [USER_ID] Bigint Primary Key Identity,
    [USER_NAME] VARCHAR (20) NOT NULL,
    [USER_SURNAME] VARCHAR (30) NOT NULL,
    [USER_PHONE] VARCHAR (12) NOT NULL,
    [user_avatar] image,
    [Organization_id] Bigint Foreign Key ([Organization_ID])
    References [Organization] ([Organization_ID]) Not NULL,
)

Trying to insert the data in the User table.

Public Async Task CreateUser (User User)
        {
            Using (Var Connection= New OdbcConnection (_ConnectionString))
            {
                await connection.openasync ();
                Using (var executecommand= new odbccommand ($ INSERT INTO [USER] "+
                    $ "([user_name], [user_surname], [User_Phone], [user_avatar], [organization_id])" +
                    //$ "Values ​​(?,?,?,?,?)", Connection))
                    //$ "Values ​​(?,?,?,?,?)", Connection))
                    $ "Values", '{user.surname}', '{user.phone}' ,?, {user.organizationid}) ", Connection))
             {
                 //Executecommand.parameters.add ("@user_name, ODBCTYPE.VARCHAR, 20) .Value= user.name;
                 //Executecommand.parameters.add ("@user_surname ", odbctype.varchar, 30) .Value= user.surname;
                 //Executecommand.parameters.add ("@user_phone ", odbctype.varchar, 12) .Value= user.phone;
                 executecommand.parameters.add ("@ user_avatar", odbctype.image) .Value= user.avatar;
                 //Executecommand.parameters.add ("@organization_id ", odbctype.bigint) .Value= user.organizationID;
                 await executecommand.executenonqueryasync ();
             }
         }
     }

I get errors like:

error [07s01] [Microsoft] [ODBC Driver 17 for SQL Server] Invalid use of the standard parameter -when used?

You need to declare the scalar variable "@Name". -when using @param

How to solve this problem.

Why ODBC? What type of database?

aepot2021-09-26 04:54:47

MSSQL database. Because the old programmer did so, to change on EF is not yet possible.

Denis2021-09-26 04:54:47

docs.microsoft.com/ru-ru/dotnet/framework/data/adonet/sql -without ODBC you can do the usual ADO.NET. In essence, you are now using an excess layer between the code and the base that is not needed.

aepot2021-09-26 04:54:47

Examples in the documentation is so-so, but still. In principle, you are all right: ... add (...). Value= ...; -So it should be.

Alexander Petrov2021-09-26 04:54:47

There are no named parameters in ODBC. Binding Parameters ODBC. As a playerholeder, use a question mark. Add values ​​using the Add method in the same order as in the SQL query. Parameter names in ADD can be any.

Alexander Petrov2021-09-26 04:54:47