Processing
Knowledge Base 
Search the Knowledge Base for

Here you can search and view our site Knowledge Base.
To find a specific article, use KB-article number format (ex. KB-1)

Article number: Article number: kb-31
Q. How can I access to the MSSQL database from my website?

A. It depends on the programming techniques which can be used to access to the databases. Below you can find some example with different technique which can be used to access to databse
  • ASP (using ODBS DSN)

    <%System_DSN = "dsn=DBDSN;uid=DBUserName;pwd=DBUserPassword"

    set Conn = server.createobject("adodb.Connection")
    Conn.Open System_DSN
    set RS = server.createobject("adodb.recordset")
    SQL = "select * from table"
    RS.Open SQL, Conn,1,2

    while not RS.eof
    ......
    rs.moevnext
    wend
    RS.close
    set RS=nothing
    Conn.close
    set COnn=nothing

    %>


  • ASP.Net 1.1

    In <appSettings> section of web.config
    <add key="DBConnStr" value="server=localhost;
    User ID=DBUserName;Password=DBUserPassword;database=yourDB;"/>

  • Code in the web page
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;

    bool ret=false;
    SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["DBConnStr"].Trim().ToString());
    try
    { myConnection.Open();
    SqlCommand myCommand = new SqlCommand();
    myCommand.Connection = myConnection;
    myCommand.CommandText = "UpdateShopCartQty";
    myCommand.CommandType = CommandType.StoredProcedure;
    .....

    //myCommand.ExecuteNonQuery();
    ret = true;
    }
    catch (Exception er)
    {
    this._lasterror = er.Message;
    ret = false;
    }
    finally
    {
    if (myConnection.State == ConnectionState.Open)
    myConnection.Close();
    myConnection.Dispose();
    }

  • ASP.Net 2.0

    In <configuration> section of Web.config
    <connectionStrings>
    <add name="ConnectionString" connectionString="server=localhost;uid=DBUserName;pwd=DBPassword;database=yourDB;"/>
    </connectionStrings>

    Code in the web page
    using System.Data;
    using System.Data.SqlClient;
    bool ret=false;
    SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
    try
    { myConnection.Open();
    SqlCommand myCommand = new SqlCommand();
    myCommand.Connection = myConnection;
    myCommand.CommandText = "UpdateShopCartQty";
    myCommand.CommandType = CommandType.StoredProcedure;
    .....

    //myCommand.ExecuteNonQuery();
    ret = true;
    }
    catch (Exception er)
    {
    this._lasterror = er.Message;
    ret = false;
    }
    finally
    {
    if (myConnection.State == ConnectionState.Open)
    myConnection.Close();
    myConnection.Dispose();
    }

This page has been viewed 1229 times


Return to Search