Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ASP NET Core Microsoft.Data.SqlClient.SqlException (0x80131904)

I’m trying to make a selection from the database

Db db = new Db();
var con = await db.OpenConnection();
SqlCommand command = new SqlCommand("SELECT * FROM users", con);
SqlDataReader reader = await command.ExecuteReaderAsync();
while (await reader.ReadAsync())
{
...
}
await db.CloseConnection(con);

But I get an exception

Microsoft.Data.SqlClient.SqlException (0x80131904): The object name
‘users’ is not valid.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I have no idea what’s going on, because the database exists and the table exists too

enter image description here

Class code

using System.Data;
using Microsoft.Data.SqlClient;

namespace CarWashServer
{
    internal class Db
    {

        public async Task<SqlConnection> OpenConnection()
        {
            var con = new SqlConnection();
            con.ConnectionString = "Data Source=localhost;User ID=*****;Password=********;Connect Timeout=30;Encrypt=False;Trust Server Certificate=True;Application Intent=ReadWrite;Multi Subnet Failover=False";
            if (con.State != ConnectionState.Open)
                await con.OpenAsync();
            return con;
        }

        public async Task<int> CloseConnection(SqlConnection con)
        {
            await con.CloseAsync();
            return 0;
        }


    }
}

>Solution :

Ensure the table name in your database is actually "users". SQL Server table names are case-insensitive by default, but the name must match exactly, including any spaces or special characters.

If the table is in a schema other than dbo, include the schema name:

SELECT * FROM schema_name.users

Use Square Brackets for Reserved Keywords:

SELECT * FROM [users]

Good luck!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading