1and1 Help Centre Categories

print article

Connect to a MSSQL Database Using ASP.NET

For Windows

To make a connection to MSSQL you need the following information from the 1&1 Control Panel:

  • MSSQL Host Name
  • User Name
  • Password
  • Database Name
In this guide, a MSSQL database named db123456789 has already been created. Within this MSSQL database there is one table named tbl_Sales. This table holds the data for recent sales by employees. Look below at the data in the database.

Employee Product Price SaleNumber
John Shoes £39.99 28637283
Sue Dress shirt £41.99 33894673
John Pants £34.99 34783263
John Socks £4.99 4.99
Sue Gloves £7.99 7.99
Bob Jacket £74.99 74.99

In this example, an ASP.NET script in VB will be created to connect to the MSSQL database, print the products and price for each sale on the page.

Please note:
Look for the // below for commented lines that will instruct you when you need to replace the sample data with your information such as database name, password, etc. using the MSSQL Database Connection Credentials.
<%@ Page Language="VB" %>
<script runat="server">

Sub Page_Data
Dim oDR as System.Data.SQLClient.SQLDataReader
Dim oCom As System.Data.SQLClient.SqlCommand
Dim oConn as System.Data.SQLClient.SQLConnection

Try
// Enter the database server address, initial catalog(database name),
// uid(username) and pwd(password) in the line below
oConn = New System.Data.SQLClient.SQLConnection ("server=winsqlus01.1and1.com; initial catalog=db123456789;uid=dbo123456789;pwd=yourPassword")
oConn.Open()
oCom = New System.Data.SQLClient.SqlCommand()
oCom.Connection = oConn
// Enter your SQL query within the quotes in the line below
oCom.CommandText = "SELECT * FROM tbl_Sales"
oDR = oCom.ExecuteReader()
While oDR.Read
// Enter a column name within the quotes below for each item
Response.Write(oDR.Item("Product") & ", " & oDR.Item("Price"))
Response.Write("<BR/>")
End While
Catch
Response.Write("Error:" & err.Description)
Finally
oDR = Nothing
oCom = Nothing
oConn.Close()
oConn = Nothing
End Try
End Sub
</script>

<html>
<title>Queries from the MS-SQL database with ASP.NET</title>
<body bgcolor="FFFFFF">
<h2>Query from table <b>products</b> with ASP.NET</h2>
<%Page_Data()%>
</body>
</html>
Please note:
You can download the machine.config, default web.config and custom trust level files used for 1&1 Shared Hosting Windows.
For additional information, you may want to reference: