Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

Here is my piece of code,

string con = ConfigurationSettings.AppSettings["DBConnectionString"]
SqlConnection conn = new SqlConnection(con);
string query="select name from sysdatabases(nolock)";
sqlcommand comm= new sqlcommand(query,con);
sqlDatareader dr= comm.executeReader();


dr returning result as
master
tempdb
model
msdb


Note: I am using master database in my connectionstring "Initial Catalog=Master;" for above code.



but when I am running the same query in sql server query editer (Master Database)
select name from sysdatabases(nolock)


result: (this one is correct result)
master
tempdb
model
msdb
DB_test
DB_HMS


Please Point/suggest me where I am doing wrong..??
Posted
Comments
Anurag Sinha V 16-Aug-13 5:45am    
Interesting question Adarsh..need to see it...
Adarsh chauhan 16-Aug-13 5:47am    
:) I don't know if I am missing something.. any help would be appreciated.. :)
Mehdi Gholam 16-Aug-13 5:55am    
Are you using the same username and password?
Adarsh chauhan 16-Aug-13 5:59am    
yes.. same username and password,as i have only one user account for my sql server..

I executed same code on my test application. It gives me same result in application as well in sql.

it provides me the same result. Only the thing i changed in your code is :
1. SqlCommand comm = new SqlCommand(query, conn); : instead of string variable i put the object of SqlConnection conn.
2. conn.Open()
3. conn.Close()

as mentioned below :

C#
string con = ConfigurationSettings.AppSettings["DBConnectionString"];
SqlConnection conn = new SqlConnection(con);
conn.Open();
string query = "select name from sysdatabases(nolock)";
SqlCommand comm = new SqlCommand(query, conn);
SqlDataReader dr = comm.ExecuteReader();
conn.Close();
 
Share this answer
 
v3
Comments
CodeBlack 16-Aug-13 7:13am    
still it doesnt work then there must be a permission issue i guess.
Adarsh chauhan 16-Aug-13 7:44am    
Ok, will try once I reach on my machine.
well what I think it should not be affected by conn or con.
CodeBlack 16-Aug-13 7:52am    
thats true. it doesnt make sense. But still, if our luck is not good then we may face problem because of some silly mistakes. :)
Thanks for showing concern guys..

Without making any changes in code or connection string now code is running fine, don't know why it was showing wrong output earlier.. :)
Accepting this as an answer to remove it from unanswered..
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900