locked
OleDbConnection - Beginner RRS feed

  • Question

  • User-263791825 posted

      Hi All,

     

    Having real problems trying to configure a OleDbConnection, this is the first time I've done so. My database is in the same directory as the .aspx page but still no luck, what am I missing? [*-)]

     

    		OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("database.mdb"));
                    OleDbCommand cmd = new OleDbCommand( SQL, conn );
                    conn.Open( );
    		OleDbDataReader rdr = cmd.ExecuteReader( ); 
     
    Thursday, October 23, 2008 2:30 PM

Answers

  • User-821857111 posted

    You need to give the Data Source property in your connection string the full file system path to the database.  At the moment, it is assuming that the database is in the same directory as the ASP.NET runtime, or somewhere equally as silly - I can't remember where, but your error message will tell you.  Try moving the database to App_Data within the site structure, and reference it using the |DataDirectory| substitute string:

    Provider=Microsoft.Jet.OleDb.4.0;Data Source=|DataDirectory|database.mdb

    That way, you won't have to change the path every time you copy the site to another environment.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 27, 2008 11:51 AM
  • User-1199946673 posted

    Try 

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

    instead of

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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 27, 2008 3:39 PM

All replies

  • User-1199946673 posted

    check this out:

    http://www.mikesdotnetting.com/Article.aspx?ArticleID=78

     

     

    Thursday, October 23, 2008 7:17 PM
  • User-821857111 posted

    Could you perhaps explain the nature of your problem?  Do you get an error?  Unexpected behaviour?  What?

     

    Friday, October 24, 2008 2:21 AM
  • User-263791825 posted

    Could you perhaps explain the nature of your problem?  Do you get an error?  Unexpected behaviour?  What?

     

     

     

    I'm trying to create an RSS feed but get a compilation error from the OleDbConnection which doesn't find my database, heres the full code for the feed in C#, the database is located in the same directory as the script.

     

    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Xml" %>
    <%@ Import Namespace="System.Text" %>
    <%@ Import Namespace="System.IO" %>
    <script runat="server" language="C#">
    private void Page_Load(object sender, System.EventArgs e)
    {
        // Set the content-type
        Response.ContentType = "text/xml";
        Response.ContentEncoding = Encoding.UTF8;

        // check to see if a cached version exists
        if (Cache["RssFeed"] == null)
        {
           try {
            // build up the cache dynamically

                    // you can change the 10 to any number you want...number of items to be retrieved
                    String SQL = "SELECT TOP 10 id, title, content, date FROM news ORDER BY date DESC";

                    OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=database.mdb");
                    OleDbCommand cmd = new OleDbCommand( SQL, conn );
                    conn.Open( );
            OleDbDataReader rdr = cmd.ExecuteReader( );

            // Use an XmlTextWriter to write the XML data to a string...
            StringWriter sw = new StringWriter();
            XmlTextWriter writer = new XmlTextWriter(sw);

            // write out <rss version="2.0">
            writer.WriteStartElement("rss");
            writer.WriteAttributeString("version", "2.0");

            // write out <channel>
            writer.WriteStartElement("channel");

            // write out <channel>-level elements
            // **** CHANGE TO MATCH YOUR NEEDS ****
            writer.WriteElementString("title", "Spectors Feed");
            writer.WriteElementString("link", "http://myWebSite.com/");
            writer.WriteElementString("description", "Description");
            writer.WriteElementString("ttl", "60");

            // write out an <item> element for each of record found by the SQL query:
                    while ( rdr.read( ) )
            {
                // write out <item>
                writer.WriteStartElement("item");

                // write out <item>-level information
                writer.WriteElementString("title", rdr[1].ToString());

                // use DateTimeFormatInfo "r" to use RFC 1123 date formatting (same as RFC 822)
                writer.WriteElementString("pubDate", ((DateTime) rdr[2].ToString("r")));

                writer.WriteElementString("description", rdr[3].ToString());

                // write out </item>
                writer.WriteEndElement();
            }

            // write out </channel>
            writer.WriteEndElement();

            // write out </rss>
            writer.WriteEndElement();

            // save the string in the cache (cache for 1.5 hours)
            Cache.Insert("RssFeed", sw.ToString(), null, DateTime.Now.AddHours(1.5), TimeSpan.Zero);
            writer.Close();

                } catch( Exception ex ) {
                    Response.Write("Exception caught: " + ex.toString());
                } finally {
                    try { rdr.Close(); } catch { /* ignore errors */ }
                    try { conn.Close(); } catch { /* ignore errors */ }
                }
        }

        // write out the cached value
        Response.Write(Cache["RssFeed"].ToString());

    }
    </script>

    Monday, October 27, 2008 9:45 AM
  • User-821857111 posted

    You need to give the Data Source property in your connection string the full file system path to the database.  At the moment, it is assuming that the database is in the same directory as the ASP.NET runtime, or somewhere equally as silly - I can't remember where, but your error message will tell you.  Try moving the database to App_Data within the site structure, and reference it using the |DataDirectory| substitute string:

    Provider=Microsoft.Jet.OleDb.4.0;Data Source=|DataDirectory|database.mdb

    That way, you won't have to change the path every time you copy the site to another environment.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 27, 2008 11:51 AM
  • User-263791825 posted

    You need to give the Data Source property in your connection string the full file system path to the database.  At the moment, it is assuming that the database is in the same directory as the ASP.NET runtime, or somewhere equally as silly - I can't remember where, but your error message will tell you.  Try moving the database to App_Data within the site structure, and reference it using the |DataDirectory| substitute string:

    Provider=Microsoft.Jet.OleDb.4.0;Data Source=|DataDirectory|database.mdb

    That way, you won't have to change the path every time you copy the site to another environment.

     

    Ok i've done as is stated, but im still getting a compilation error; "Compiler Error Message: CS0246: The type or namespace name 'OleDbConnection' could not be found (are you missing a using directive or an assembly reference?)"

     

     

               OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source=|DataDirectory|database.mdb");
                    OleDbCommand cmd = new OleDbCommand( SQL, conn );
                    conn.Open( );
            OleDbDataReader rdr = cmd.ExecuteReader( );

    Monday, October 27, 2008 1:52 PM
  • User-1199946673 posted

    Try 

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

    instead of

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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 27, 2008 3:39 PM