0

I have the following c# code that loads high scores from access database, how can I adjust this code to use external sql database (System.Data.SqlClient). Can I enter the connectionString to a simple config.aspx file instead of starting to use a new web.config file ?

<%@ Page Language="C#" %>
<!--#include file=config.aspx"-->
<asp:AccessDataSource id="database" Runat="Server" />
<script runat="server">

void Page_Load( object sender, EventArgs e ){
    String id = Request.QueryString["id"];
    String query;
    System.Data.DataView dataView;

    if(scoreRange == "pastDay") {
        query = "SELECT TOP " + noOfScores + " [name], [score] FROM [" + databaseTableName + "] WHERE [gameID] = " + id + " AND [date] >= DateAdd( \"d\", -1, Now() ) ORDER BY [score] DESC, [date] DESC";
    }  else {
        query = "SELECT TOP " + noOfScores + " [name], [score] FROM [" + databaseTableName + "] WHERE [gameID] = " + id + " ORDER BY [score] DESC, [date] DESC";
    }

    database.DataFile = databaseFile;
    database.SelectCommand = query;

    dataView = (System.Data.DataView) database.Select(DataSourceSelectArguments.Empty);

    Response.Write("<HIGHSCORES scoreRange=\"" + scoreRange + "\">");

    for(int i=0;i<dataView.Table.Rows.Count;i++) {
        Response.Write("<RECORD name=\"" + HttpUtility.HtmlEncode(dataView.Table.Rows[i][0].ToString()) + "\" score=\"" + dataView.Table.Rows[i][1] + "\" />");
    }

    Response.Write("</HIGHSCORES>");
}

</script>
2
  • instead of starting to use a new web.config file.. I don't understand what you mean by this, can you elaborate? Commented Feb 19, 2015 at 14:36
  • this file does not have web.config file assosiated and I prefer not to set up one Commented Feb 19, 2015 at 15:20

1 Answer 1

1

I'm assuming you mean that you want to specify the connection string in the aspx file. If so then you want something like:

<asp:SqlDataSource
    id="SqlDataSource1"
    runat="server"
    ProviderName="<%$ Your provider here%>"
    ConnectionString="<%$ You connection string here%>"
    SelectCommand="You select statement here">
</asp:SqlDataSource>

Further reading here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.