Re: exception error when accessing SQL database

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: William \(Bill\) Vaughn (NoSpamAtAllBillva_at_nwlink.com)
Date: 09/23/04


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
>>
>>
>> 


Relevant Pages

  • Re: accessing data mining model via web/PDA
    ... Is your OPENROWSET clause connecting to a SQL Server 2000 database? ... Is your SQL Server instance running on the same machine as your Analysis ... i need some> help for the connection string, let say to access the> mining model in the AS. ...
    (microsoft.public.sqlserver.datamining)
  • Re: VB Express SQL Express Remote Connection
    ... Yes I can connect using the SQL Server Management Studio Express Tool. ... I changed my connection to exactly what you specified and it connected. ... I can connect and create databases on the Remote Server ...
    (microsoft.public.dotnet.languages.vb)
  • Re: How to Direct connect with ActiveSync
    ... That won't work because you have a connection string that can't be correct. ... You're saying that the database is, but it's not; ... Server 2000, Visual Studio 2008. ... Again sorry if this should be in another form but you say that SQL Server ...
    (microsoft.public.sqlserver.ce)
  • .Net Scalability problem
    ... I finished a Great project using .Net and SQL Server and .Net Mobile ... Million concurrent users ... So I think that the MTC generate concurrent connection and per ...
    (microsoft.public.sqlserver.connect)
  • Re: Communication Link Failure Error
    ... On disconnection from the network, in my code I changed the connection ... string to use SQLOLEDB and sent a SQL query to SQL server which failed as ... unplug the network cable from the client machine and put the cable ...
    (microsoft.public.data.ado)