C#C
C#3y ago
Alex

✅ SQLConnection and queries to the database

Hey there!
I've been having this problem lately with something i'm writing, which essentially boils down to the following code:
String conn = $@"Server={server};Database={database};Trusted_Connection=True;";
this.Connection = new SqlConnection(connectionString);
this.Connection.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM Alunos", this.Connection);
SqlDataReader reader = cmd.ExecuteReader();

When I run this, it seems to just not find my Alunos table, and I end up having to write [{database}].[dbo].[Alunos] even though I had specified that I wanted to start out with a database in my connection string. What am I doing wrong here? I don't want to have to write my queries with dbo.(...); I'd rather just switch to the database I want at runtime and then query a table directly 😅
Was this page helpful?