How to connect an HTML page, using ASP, to SQL Server database?

-1

How do I connect a sql database to an html site using asp?

Note: I already have the whole bank developed and the whole page too, I just need to know how I bridge the two

    
asked by anonymous 30.06.2016 / 15:11

2 answers

1

Gui Sartori, it's good to put some details that make the difference type:

  • It's asp classics or asp.net
  • SQL what is the 2008, 2012 release

If asp classico follows

'<% 

    Dim conexao, stringConexao, host, usuario, senha, banco 
    host   ="SERVIDOR"
    usuario="BASE" 
    senha  ="SENHA"
    banco  ="BANCO"

    Monta a string de conexão utilizando os dados informados anteriormente
    stringConexao ="Provider=SQLNCLI10;
    SERVER="&host&";DATABASE="&banco&";UID="&usuario&";PWD="&senha&";"

  ##Instancia o objeto de conexão com o banco
  SET conexao = Server.CreateObject("ADODB.Connection")

  On Error Resume Next

 ##Abre a conexão junto ao banco
 conexao.Open stringConexao

##Tratamento de erro. Caso ocorra problemas na conexão, exibe esta informação e apresenta detalhes.
 If Err.Number <> 0 Then
    response.write "<b><font color='red'>Conexão com o banco'"&banco&"'Microsoft SQL Server falhou !</font></b>"
   response.write "<BR><BR>"
   response.write "<b>Erro.Description:</b> " & Err.Description & "<br>"
   response.write "<b>Erro.Number:</b> " & Err.Number & "<br>"
   response.write "<b>Erro.Source:</b> " & Err.Source & "<br>"
Else
   ##Caso a conexão seja bem sucedida, mostra mensagem de confirmação.
     response.write "<b><font color='blue'> Conexão com o banco '" & banco & "' Microsoft SQL Server estabelecida com sucesso !</font>"
End If

 ##Fecha a conexão com o banco
 conexao.close

 ##Remove as referência do objeto da memória
 SET conexao = Nothing
 %>

I do not know asp classico I saw this code in the wiki of the locaweb go here

    
30.06.2016 / 15:55
2

There are several ways to solve your problem. I would use the entity framework to map my database objects to classes (database-first) and insert my conectionstring into the config. After that, in your controller (if it is MVC), refer to the database connection and execute the necessary query by saving the results in a < > list. Send the controller list to view via viewmodel ..

Ex. controller: private ProductsContext bancodedados = new ProductsContext ();

List products = new List products = banked.Products.ToList ();

* You need to create a Products class () with its properties, example: ProductID, Name, Description, Price {get; set;} etc ..

link

p>

I recommend you read these two articles.

    
30.06.2016 / 15:28