Connet to SQL Server?

G

Guest

I am a newbie to both .Net and Sql Server and I am trying to access the
database by using the ADO.Net data reader. This is actually one of the
execises in a book that I am reading. However, when I run this program, there
is an error saying that "Login failed for user 'sa'. Reason: Not associated
with a trusted SQL Server connection." Are there some settings I have to
change in the Sql Server? Please help me!

//--------------------------------------------------------------
using System;
using System.Data;
using System.Data.SqlClient;

class MyApp
{
static void Main()
{
try{
SqlConnection conn = new
SqlConnection("server=localhost;uid=sa;pwd=;database=pubs");
conn.Open();

SqlCommand comm = new SqlCommand("select * from titles", conn);
SqlDataReader reader = comm.ExecuteReader();

while(reader.Read())
Console.WriteLine(reader.GetString(1));

conn.Close();
}

catch(Exception e){
Console.WriteLine(e.Message);
}
}
}

//--------------------------------------------------------------
 
G

Guest

I just figured out the problem... and the way to solve it is to go to
Enterprise Manager, select server properties and on the Security tab change
Authentication mode to 'SQL Server and Windows'.
 
G

Guest

Mixed mode security works wonders. Good catch!


---

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 
W

William \(Bill\) Vaughn

I don't agree with the book you're reading. It's not a good idea to use SQL
Server security and it's a terrible idea to develop using the SA account.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
G

Guest

No no no, it is just an exercise to demonstrate the classes in the
System.Data namespace. Of cause, I agree with you that it is a terrible idea
to develop using the SA account. :)
 
G

Guest

Just wondering, what exactly are the two authentication modes and how are
they different? Thanks!
 
W

William \(Bill\) Vaughn

I don't demonstrate, illustrate or show examples of bad practices. It's like
showing someone how to swim wearing body armor and a field pack. ;)
Too many people use examples in unintended ways. I try my darndest to always
use best practices... it's tough but I sleep better.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
G

Guest

You are probably right. But in my humble opinion, I guess the author's
intention was to stay focus on the point of the section. After all, it is
pretty hard for a newbie like me to swallow too many new concepts all at
once...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top