question

ErdemErdemir-5953 avatar image
0 Votes"
ErdemErdemir-5953 asked SeeyaXi-msft commented

i am executing a query but it doesnt work like i want to

hi guys i am executing;
select name from sys.Databases
it only shows me system databases like master ect.
but i have lots of databases in my instance

sql-server-general
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Maybe the databases are offline or you do not have permissions?


0 Votes 0 ·

i am connecting with sa

0 Votes 0 ·
Viorel-1 avatar image Viorel-1 ErdemErdemir-5953 ·

Are you using "sa" in your C# tests too?


0 Votes 0 ·

Hi @ErdemErdemir-5953 ,

Welcome to Microsoft Q&A!
Here is an article about C# Code: https://hevodata.com/learn/c-sql-server/

Best regards,
Seeya

0 Votes 0 ·
JingyangLi avatar image
0 Votes"
JingyangLi answered ErdemErdemir-5953 commented

You may have multiple instances on the same machine.
Double check that you are running this query in your instance correctly.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

when i made this program i made users selecting the instance
When i execute this query on ssms there is no problem it shows every database but when i use c# to do it it only shows system databases

0 Votes 0 ·
ErlandSommarskog avatar image
0 Votes"
ErlandSommarskog answered ErlandSommarskog commented

when i made this program i made users selecting the instance

When i execute this query on ssms there is no problem it shows every database but when i use c# to do it it only shows system databases

Debugging time, I guess!

First add this query to your C# program:

```
SELECT @@servername
```to verify that you are connecting to the server you think you are connecting to.

Would that be the case, then add this query:

```
SELECT SYSTEM_USER, * FROM sys.fn_my_permissions(NULL, NULL)
```Do you see VIEW ANY DATABASE in the list? Is the user the expected one?
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

yes it has the perm but when i executed in the c# it only says DESKTOP-DESKTOPNAME\CETRUSFORT
this is the code i run


SqlConnection con = new SqlConnection("Server= " + sss.sv + "; Database=; Integrated Security=True;");
con.Open();
SqlCommand cmd = new SqlCommand("SELECT SYSTEM_USER, * FROM sys.fn_my_permissions(NULL, NULL)", con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
listBox1.Items.Add(dr[0].ToString());
}
con.Close();

0 Votes 0 ·

yes it has the perm but when i executed in the c# it only says DESKTOP-DESKTOPNAME\CETRUSFORT


And what about @@servername? Is that the same as you see in SSMS?

0 Votes 0 ·

it says DESKTOP-DESKTOPNAME

0 Votes 0 ·
Show more comments