Asp.net Connect To Mssql And Execute A Query?
Right now I am testing and debugging my ASP.net and MSSQL on my Windows Vista computer, not on a web server yet. I have MSSQL 2005 installed along with Microsoft SQL Server Management Studio Express. For my ASP.net, I am using Visual Web Developer.
I am using windows authentication to connect and create tables on the database in SQL server management. I can also execute queries. In ASP.net, I used the solution explorer to connect to the database and now I am trying to write in my code a simple connection and query. Here is the code:
When I used this code, I had the following error:
Exception Details: System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 – Could not open a connection to SQL Server)
Source Error:
Line 12: Dim myCommand As New SqlCommand(myQuery)
Line 13: myCommand.Connection = myConnection
Line 14: myConnection.Open() < this is the line that had the problem
Line 15: myCommand.ExecuteNonQuery()
How do I connect to a database with ASP.net with windows authentication? The name of my database is SaintsBlog. Do I have to put SaintsBlog.dbo or what for the MyConnectionString variable? I'm confused as to how to connect to database on my computer. I inserted some test data into the table so if it executes it should return data.
This is my first time using MSSQL, I come from a MySQL background. Just the other day I figured out how to actually make tables and such but I'm picking it up. I will pick the best answer and thanks in advance for the help!
Related Posts:
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.


1 Response to “Asp.net Connect To Mssql And Execute A Query?”.
With ASP.Net you usually always want to create a connection string in your web.config file first. This link shows the code for SQL 2000 and ASP.net 2.0 but I think it still applies to SQL 2005 and ASP.net 3.5.http://msdn.microsoft.com/en-us/library/…
IMPORTANT QUOTE FROM THE LINK :
“The connection string used with Windows authentication must include either the Trusted_Connection=Yes attribute, or the equivalent attribute Integrated Security=SSPI”
Leave a comment.