A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
Connecting to a SQL database from ASP .NET II
This is part II of the tutorial that teaches you how to connect to a SQL database from ASP .NET using SQL Authentication. This part shows you how to connect to the database using ASP .NET code and Visual Studio .NET's features.
On Saturday, August 21st 2004 at 07:16 AM
By Andrew Pociu (View Profile)
(Rated 3.7 with 82 votes)
Connecting to SQL from ASP .NET I
In the first part of the tutorial we dealed with the SQL database. Now we're going to make the ASP .NET web application that will establish a connection to the database we created. We're going to do it the Visual Studio way - as quick as possible with very few effort.
So fire up Visual Studio .NET and create a new ASP .NET Web Application - name it 'SQLConnection'.
Do a Ctrl+Alt+S shortcut that brings the Server Explorer window. Browse through Server > YourComputerName > SQL Servers > YourInstanceName.
We are interested in the MyDB database, and if you click the [+] sign next to it you can see that it is freely browseable. It can be browsed because Visual Studio authenticates using Windows NT Integrated Security but one of the purposes of this tutorial is to connect to it using SQL Authentication (in fact, that's why we created that SQL login in the first tutorial).
We can simply connect to MyDB using different authentication: right click the database name and choose Change Login... uncheck the checkbox named 'Use Windows NT Integrated Security' and in the Login field enter the login name that we created in the first tutorial (aspnet) and the password you chose:
Everything should work just fine and you should be able to browse the database now, just as you did using Windows Integrated Authentication.
You can freely browse the database in the Server Explorer:
Server Explorer also shows us the table and the columns. What's great about this is that you can actually drag and drop the columns and even the table on the design area of the Web Application. And this is what we're going to do, as I said earlier, we're going to do this the Visual Studio way - quick and simple.
So grab the MyLinks (dbo) item and drop it on the design area. You are warned that the password is saved as clear text - ignore and choose Include password. Now a Data Adapter Configuration Error pops up that tells us it couldn't generate the UPDATE and DELETE statements. We don't mind, we only want to retrieve data from the database right now.
sqlConnection1 and sqlDataAdapter1 are created. Now we only have to code the part where we retrieve rows from the database. And thanks to Visual Studio .NET, it won't take long.
Open the code behind file (WebForm1.aspx.cs) and inside Page_Load use the following code:
Compile and see the result in Internet Explorer:
We successfully connected to a SQL database from an ASP .NET application using SQL Authentication!
Get yourself a beer or whatever you may drink and enjoy your accomplishment.
|Digg It! Del.icio.us Reddit StumbleIt Newsvine Furl BlinkList|
Rate this tutorial
There are no related tutorials.
Related Source Code
There is no related source code.
ASP.NET Job Search