Using Database Connection

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Simon Harris

    Using Database Connection

    Hi All,

    I have this in an ASCX file:
    <%@ Control Language="vb" %>
    <%@import namespace="Syst em.Data.OleDb"% >
    <Script runat="Server">
    Public Function GetConnection()
    Dim Conn as OleDbConnection
    Dim ConnStr as String
    ConnStr="Provid er=Microsoft.Je t.OLEDB.4.0;Dat a Source=C:\Docum ents and
    Settings\Simon\ My
    Documents\Adote k\Projects\SkiP assDirect\priva te\skipassdirec t_mainDB.mdb"
    'For password protected DB's add "Userid=xxxx;Pa ssword=xxxxx" to the above
    string
    Conn=new OleDbConnection (ConnStr)
    end Function
    </Script>

    I have successfully registered the class and opened the connection like
    this:
    <%@ Register TagPrefix="SPD" TagName="DBConn ection"
    src="/assets/classes/database_connec tion.ascx"%>
    <SPD:DBConnecti on id="DBConnectio n" runat="Server"/>

    Could someone tell me how I then make use of this connection? I have tried
    the following:
    Dim sqlCmd as New SQLCommand("SEL ECT CompanyName, ContactTitle, ContactName,
    Country FROM Customers", Conn) <-- Errornous Line
    DataGrid1.DataS ource = sqlCmd.ExecuteR eader();
    DataGrid1.DataB ind();

    Which causes an error:
    Compiler Error Message: BC30451: Name 'Conn' is not declared. (This refers
    to the line marked above)

    Also, do I need to close the connection explicitly? (As I did in 'classic'
    ASP)

    Thanks for your help - I am struggling to find a basic data access tutorial
    to get me started, they all seem to assume alot of prior knowledge.

    Simon.

    --
    -
    * Please reply to group for the benefit of all
    * Found the answer to your own question? Post it!
    * Get a useful reply to one of your posts?...post an answer to another one
    * Search first, post later : http://www.google.co.uk/groups
    * Want my email address? Ask me in a post...Cos2Much SpamMakesUFat!


  • avnrao

    #2
    Re: Using Database Connection

    i dont think it is a good way of using user control for creating
    connections.
    rather you can create a class library which returns connection object and
    use it all across your projects.

    Av.

    "Simon Harris" <toomuchspam@ma kesyoufat.com> wrote in message
    news:OtJnEbmMEH A.3664@TK2MSFTN GP10.phx.gbl...[color=blue]
    > Hi All,
    >
    > I have this in an ASCX file:
    > <%@ Control Language="vb" %>
    > <%@import namespace="Syst em.Data.OleDb"% >
    > <Script runat="Server">
    > Public Function GetConnection()
    > Dim Conn as OleDbConnection
    > Dim ConnStr as String
    > ConnStr="Provid er=Microsoft.Je t.OLEDB.4.0;Dat a Source=C:\Docum ents and
    > Settings\Simon\ My
    > Documents\Adote k\Projects\SkiP assDirect\priva te\skipassdirec t_mainDB.mdb"
    > 'For password protected DB's add "Userid=xxxx;Pa ssword=xxxxx" to the above
    > string
    > Conn=new OleDbConnection (ConnStr)
    > end Function
    > </Script>
    >
    > I have successfully registered the class and opened the connection like
    > this:
    > <%@ Register TagPrefix="SPD" TagName="DBConn ection"
    > src="/assets/classes/database_connec tion.ascx"%>
    > <SPD:DBConnecti on id="DBConnectio n" runat="Server"/>
    >
    > Could someone tell me how I then make use of this connection? I have tried
    > the following:
    > Dim sqlCmd as New SQLCommand("SEL ECT CompanyName, ContactTitle,
    > ContactName,
    > Country FROM Customers", Conn) <-- Errornous Line
    > DataGrid1.DataS ource = sqlCmd.ExecuteR eader();
    > DataGrid1.DataB ind();
    >
    > Which causes an error:
    > Compiler Error Message: BC30451: Name 'Conn' is not declared. (This refers
    > to the line marked above)
    >
    > Also, do I need to close the connection explicitly? (As I did in 'classic'
    > ASP)
    >
    > Thanks for your help - I am struggling to find a basic data access
    > tutorial
    > to get me started, they all seem to assume alot of prior knowledge.
    >
    > Simon.
    >
    > --
    > -
    > * Please reply to group for the benefit of all
    > * Found the answer to your own question? Post it!
    > * Get a useful reply to one of your posts?...post an answer to another one
    > * Search first, post later : http://www.google.co.uk/groups
    > * Want my email address? Ask me in a post...Cos2Much SpamMakesUFat!
    >
    >[/color]


    Comment

    Working...