Re: exception error when accessing SQL database
From: William \(Bill\) Vaughn (NoSpamAtAllBillva_at_nwlink.com)
Date: 09/23/04
- Next message: William \(Bill\) Vaughn: "Re: MSDataShape - insert and update?"
- Previous message: William \(Bill\) Vaughn: "Re: I need a good book or two on ADO"
- In reply to: whitetulips: "Re: exception error when accessing SQL database"
- Next in thread: Val Mazur: "Re: exception error when accessing SQL database"
- Messages sorted by: [ date ] [ thread ]
Date: Wed, 22 Sep 2004 19:36:14 -0700
I _always_ include exception handlers as there are so many things that can
go wrong when you try to connect or reconnect.
When connecting to SQL Server, you need to address the server and the
instance name. When you install SQL Server you get to choose a unique
instance name or let the server use the "default" instance. There can only
be one default instance (or none). The default instance is used when no
instance name is referenced with the server name. "." or "(local)" addresses
the default instance on the current (local) system (server). The case is not
significant. However, if you use SSPI security ADO expects you to have an
account on the server that corresponds with the credentials supplied. In
Windows Forms applications, that would be your Windows domain credentials.
In an ASP app that would be the ASPNET account. This account also needs to
have permission to access the default database assigned to the Login
account.
This is discussed in more detail in my book...
hth
--
____________________________________
Bill Vaughn
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.
__________________________________
"whitetulips" <whitetulips@discussions.microsoft.com> wrote in message
news:CF66A6AD-62ED-4F51-BDFE-8E7D3CEA624E@microsoft.com...
> hihi
>
> It gave an unhandled exception. However, this operation should not give
> an exception. That is why I did not write an a try n catch for an
> exception.
> I wonder if it's because I failed to connect to the database.
> However, I did try to use the option "Tools"->"Connect to Database" to
> test the connection between my visual studio .net and the database. It
> says
> "Test Connection Succeeded."
> Also, I tried to change the (local) to the actual server name and and
> it's still the same. ie. "SqlConnection cn = new
> SqlConnection(@"server=baby\NetSDK; DataBase=pubs;Integrated
> Security=SSPI");" , given that baby(for eg.) is the server name. I have
> also
> tried (baby), but to not avail. I even tried "BABY" which is the SQL
> server's
> name, though I know that's dumb and indeed it's of no help. -_-" cause I
> am
> really desparate.. =(
>
> By the way, what's the difference between the 2 statements in the
> following? What's the significance of the "@", the brackets in "(local)"
> and
> "\NetSDK"? And what's the difference in the syntax "local" and
> "localhost"?
> 1) SqlConnection cn = new SqlConnection("Initial Catalog=pubs;Data
> Source=localhost;Integrated Security=SSPI;");
> 2) SqlConnection cn = new SqlConnection(@"server=(local)\NetSDK;
> DataBase=pubs;Integrated Security=SSPI");
>
> Please help.
>
> thks.
> lynn
>
>
> "Val Mazur" wrote:
>
>> Hi,
>>
>> What kind of exception do you get? try to replace (local) with the actual
>> server name
>>
>> --
>> Val Mazur
>> Microsoft MVP
>>
>>
>> "whitetulips" <whitetulips@discussions.microsoft.com> wrote in message
>> news:2A6247C7-0C32-4D20-B9ED-21A7583E689E@microsoft.com...
>> > Dear all,
>> > I am using visual studio .net and c# to develope mobile applcations
>> > and
>> > I am new to both. I used the following code under the option "visual c#
>> > project", "Smart device application". It was successfully built, but
>> > the
>> > execution got stuck with exception error when it hit the line /*dr =
>> > cmd.ExecuteReader(CommandBehavior.CloseConnection);*/.
>> >
>> > private void UseDataReader_Click(object sender, System.EventArgs e)
>> > {
>> > listBox1.Items.Clear();
>> > SqlConnection cn = new SqlConnection(@"server=(local)\NetSDK;
>> > DataBase=pubs;Integrated
>> > Security=SSPI");
>> >
>> > SqlDataReader dr;
>> > SqlCommand cmd = new SqlCommand();
>> > cmd.CommandText = "Select au_lname, au_fname from Authors";
>> > cmd.Connection = cn;
>> > cn.Open();
>> > dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
>> > .......................
>> > }
>> >
>> > I had the similar problem before when I was working with the option
>> > "visual
>> > c# project", "Windows Application". But the execution was fine after i
>> > changed the database connection statement to /*SqlConnection cn = new
>> > SqlConnection("Initial Catalog=pubs;Data Source=localhost;Integrated
>> > Security=SSPI;");*/. However, this does not seem to solve the problem
>> > here.
>> >
>> > Can anyone help?
>> >
>> > thks
>> > lynn
>>
>>
>>
- Next message: William \(Bill\) Vaughn: "Re: MSDataShape - insert and update?"
- Previous message: William \(Bill\) Vaughn: "Re: I need a good book or two on ADO"
- In reply to: whitetulips: "Re: exception error when accessing SQL database"
- Next in thread: Val Mazur: "Re: exception error when accessing SQL database"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|