Go4Expert

Go4Expert (http://www.go4expert.com/)
-   ASP.NET (http://www.go4expert.com/articles/asp-net-tutorials/)
-   -   Connecting to MSSQL Server in ASP.NET (http://www.go4expert.com/articles/connecting-mssql-server-aspnet-t2559/)

pradeep 15Jan2007 17:55

Connecting to MSSQL Server in ASP.NET
 
Everything you need comes with the .NET Framework. When you say you are trying to connect to SQL Server, you need to know a bit more than how to create a connection, but also how to return data and display it. Firstly, there are actually many ways to do this in .NET - some in code, some through the IDE and wizards.

To give your page access to the classes you will need to perform SQL data access, you must import the System.Data and System.Data.SqlClient namespaces into your page or codebehind.

Code:

  <%@ Import Namespace="System.Data" %>
  <%@ Import Namespace="System.Data.SqlClient" %>

Connect to the MS SQL server, using the following syntax.

Code:

  mySqlConnection = new SqlConnection("server=mssql.win-servers.com;user=dbuser;password=dbpwd;database=db")
  mySqlCommand = new SqlCommand("SELECT * FROM test", mySqlConnection)
  mySqlConnection.Open()

Full Example below.

Code:

<%@ Page Language="VB" Debug="true" %>
  <%@ Import Namespace="System.Data" %>
  <%@ Import Namespace="System.Data.SqlClient" %>
  <html>
 <title>Test</title></head>
 
  <body>
  <%
  Dim myDataReader as SqlDataReader
  Dim mySqlConnection as SqlConnection
  Dim mySqlCommand as SqlCommand
 
 
  mySqlConnection = new SqlConnection("server=mssql.win-servers.com;user=dbuser;password=dbpwd;database=db")
  mySqlCommand = new SqlCommand("SELECT * FROM test", mySqlConnection)
  mySqlConnection.Open()
  myDataReader = mySqlCommand.ExecuteReader(CommandBehavior.CloseConnection)
     
 
  Do While (myDataReader.Read())
      Response.Write(myDataReader.getDecimal(0))
      Response.Write(myDataReader.getString(1) )
      Response.Write("<br/>")
  Loop
     
  ' Always call Close when done reading.
  myDataReader.Close()
 
  ' Close the connection when done with it.
  mySqlConnection.Close() 
  %>


Mr Download 9Sep2008 03:43

Re: Connecting to MSSQL Server in ASP.NET
 
thanks alot


All times are GMT +5.5. The time now is 12:43.