I am developing an application in Classic ASP
My database is SQLSErver 2008

I am getting a problem in connecting to my database.

Here is the code which i used to connect.

connstring ="server=mymsever;database=mydb;Trusted_Connection=Yes;"
 set conntemp = server.createobject("ADODB.Connection")
 conntemp.ConnectionString=connstring
 conntemp.Open()
 Response.Write(conntemp.State)

The last statement is for the confirmation that is my connection status open or not. But each time i get 0 as printed which means its closed.

What to do in this case. How to connect it.

Member Avatar for wilsites
'##Declara as variaveis a serem utilizadas no script
Dim conexao, stringConexao, host, usuario, senha, banco

'##Informe os dados de conexão junto ao banco Microsoft SQL Server
host   ="ENDERECO_SQL_SERVER"
usuario="USUARIO" 
senha  ="SENHA"
banco  ="BANCO"

'##Monta a string de conexão utilizando os dados informados anteriormente
stringConexao = "Provider=SQLOLEDB.1;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

Try this and see

connstring ="provider=sqloledb;data source=[B]YOURSERVER[/B];initial catalog=[B]DATABASENAME[/B];user id=[B]USERNAME[/B];password=[B]PASSWORD[/B];"
 set conntemp = server.createobject("ADODB.Connection")
 conntemp.ConnectionString=connstring
 conntemp.Open()
 Response.Write(conntemp.State)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.