Results 1 to 11 of 11

Thread: MySQL Connection String Question[RESOLVED]

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Posts
    104

    Resolved MySQL Connection String Question[RESOLVED]

    Is there any reason at all that this code shouldnt work for a connection string? I cannot seem to get my web app to connect to a mySQL server.

    VB Code:
    1. Option Explicit On
    2. Public Class WebForm1
    3.     Inherits System.Web.UI.Page
    4.     Protected WithEvents Label1 As System.Web.UI.WebControls.Label
    5.     Protected WithEvents Label2 As System.Web.UI.WebControls.Label
    6.     Protected WithEvents txtUsername As System.Web.UI.WebControls.TextBox
    7.     Protected WithEvents txtPassword As System.Web.UI.WebControls.TextBox
    8.     Protected WithEvents Button1 As System.Web.UI.WebControls.Button
    9.     Protected WithEvents Button2 As System.Web.UI.WebControls.Button
    10.     Dim mySQL As ADODB.Connection
    11.     Public Username As String
    12.     Public perms As String
    13.  
    14. #Region " Web Form Designer Generated Code "
    15.  
    16.     'This call is required by the Web Form Designer.
    17.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    18.  
    19.     End Sub
    20.  
    21.     Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
    22.         'CODEGEN: This method call is required by the Web Form Designer
    23.         'Do not modify it using the code editor.
    24.         InitializeComponent()
    25.     End Sub
    26.  
    27. #End Region
    28.  
    29.     Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    30.  
    31.         mySQL = New ADODB.Connection()
    32.         mySQL.ConnectionString = ("Provider=MSDASQL;Driver={MySQL ODBC 3.51 Driver};Database=workorder;Server=IP;UID=user;PWD=password;OPTION=147458")
    33.         mySQL.Open()
    34.     End Sub
    35.  
    36.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    37.         Response.Redirect("index.htm")
    38.     End Sub
    39.     Public Function UserCheck(ByVal strUserName As String, ByVal strPassword As String) As Boolean
    40.  
    41.         Dim rs As New ADODB.Recordset()
    42.  
    43.         rs.Open("SELECT username, password FROM users WHERE username = '" & strUserName & "'", mySQL, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockReadOnly)
    44.  
    45.         If Not rs.EOF Then
    46.             If rs.Fields("password") Is strPassword Then
    47.                 rs.Close()
    48.                 rs = Nothing
    49.                 UserCheck = True
    50.                 Exit Function
    51.             Else
    52.                 rs.Close()
    53.                 rs = Nothing
    54.                 UserCheck = False
    55.                 Exit Function
    56.  
    57.             End If
    58.         Else
    59.             rs.Close()
    60.             rs = Nothing
    61.             UserCheck = False
    62.             Exit Function
    63.         End If
    64.  
    65.     End Function
    66.  
    67.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    68.         Dim mrs As ADODB.Recordset
    69.         Dim permQuery As String
    70.         Dim writeQuery As String
    71.  
    72.         Username = txtUsername.Text
    73.  
    74.         permQuery = "SELECT permissions FROM users WHERE username = '" & Username & "'"
    75.  
    76.         mrs = New ADODB.Recordset()
    77.         mrs.Open(permQuery, mySQL, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockReadOnly)
    78.         If Not mrs.EOF Then
    79.             perms = mrs.Fields("permissions").Value
    80.             mrs.Close()
    81.             mrs = Nothing
    82.         End If
    83.         writeQuery = "INSERT INTO verify(username, permissions) VALUES ('" & Username & "', '" & perms & "')"
    84.         If UserCheck(txtUsername.Text, txtPassword.Text) = True Then
    85.             mySQL.Execute(writeQuery)
    86.             mySQL.Close()
    87.             mySQL = Nothing
    88.             Response.Redirect("menu.aspx")
    89.  
    90.         Else
    91.             Response.Redirect("Login.aspx")
    92.  
    93.  
    94.         End If
    95.     End Sub
    96.  
    97.  
    98. End Class
    New to ASP.NET by the way. Came over from VB6.0 and trying to migrate an application to the web. Any help is appreciated.
    Last edited by Polariss; Jul 6th, 2005 at 02:07 PM. Reason: [RESOLVED]

  2. #2

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Posts
    104

    Re: MySQL Connection String Question

    Anyone got anything at all? Ive done searches but no help. This code wont check the database it just keeps on redirecting me to login.aspx. Any help is appreciated. I tried following the link here

    http://dev.mysql.com/tech-resources/...tnet/#ODBC.NET

    When I try to do the Imports statement it isnt recognizing the System.data.Odbc item.
    Last edited by Polariss; Jul 5th, 2005 at 07:59 PM.

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: MySQL Connection String Question

    In short Server=IP isn't a valid entry.... What the fork is IP?

    Check http://www.connectionstrings.com/ for help on building connectionstrings of all kinds...

    Tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Posts
    104

    Re: MySQL Connection String Question

    Im going to say thanks for the reply however, I know what an IP is and im not going to give anyone server name nor ip address. I know what all of that information means. I know that part of the connection string and I know what to fill that out with. Basically i guess what Im asking is this: Is the syntax for the correction string correct with ASP.NET?

    And I did look at connectionstrings.com.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Posts
    104

    Re: MySQL Connection String Question

    Here is a question though. I am using Visual Studio .NET 2002. I just checked my application in IIS. My app is currently using the wrong .NET Framework. How do I repair this problem?

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: MySQL Connection String Question

    You want to use System.Data.Odbc, but you're still creating an ADODB.Connection object. Your approach is incorrect.

    Scroll down, about halfway down that page and you'll find an example on how to connect to MYSQL.

    I'll copy paste it here:

    Demo Example - Establishing a Connection

    In the demo example, we will look at how to connect to MySQL server through MyODBC using ODBC.NET.

    1. Import the System.Data.Odbc namespace (ODBC.NET) to your application using the following statement:

    using System.Data.Odbc;


    In case of VB, it should be:

    Imports System.Data.Odbc;


    2. Once the namespace is imported in your application, you can create a simple class and establish a connection to MySQL server through MyODBC using an OdbcConnection object.

    string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" +
    "SERVER=localhost;" +
    "DATABASE=test;" +
    "UID=venu;" +
    "PASSWORD=venu;" +
    "OPTION=3";

    OdbcConnection MyConnection = new OdbcConnection(MyConString);
    MyConnection.Open();


    The above one uses DSN-less connection, if you have a MyODBC DSN defined already then you can just use "DSN=dsn_name" as the connection string i.e.

    OdbcConnection MyConnection = new OdbcConnection("DSN=myodbc3-test");
    MyConnection.Open();


    In case of VB, it should be

    Dim MyConnection As New OdbcConnection(MyConString)
    MyConnection.Open()


    3. Once connected, you can execute the SQL statements using the interfaces provided by ODBC.NET.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Posts
    104

    Re: MySQL Connection String Question

    For some reason I cannot Import ODBC.

    The only one that shows is OleDb. I tried to switch the version of the .NET Framework im using. I installed the Microsoft .NET ODBC driver. How do you change Visual Studio to use .NET Framework 1.1 instead of 1.0?
    Last edited by Polariss; Jul 6th, 2005 at 07:20 AM.

  8. #8
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: MySQL Connection String Question

    Did you get MDAC 2.6/2.8?

    Have you added a reference to the ODBC dll that you downloaded?

    (I did not know you were on 1.0)

  9. #9

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Posts
    104

    Re: MySQL Connection String Question

    Yes I added a reference to odbc.dll however it is still not working.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Posts
    104

    Re: MySQL Connection String Question

    Ok sucessfully got the page to connect to the database. I had to use

    Imports Microsoft.Data.Odbc

    No idea. Anyhow the page is connecting to the database but now recordsets arent working. I need to translate my above recordsets into I guess OdbcCommands. I have no idea. Im trying something out in hopes of this actually working. Will keep ya posted.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Posts
    104

    Resolved Re: MySQL Connection String Question

    Ok this thing is solved. I do need to open another thread though. I had to rewrite the code. Thanks guys for pointing me in the right direction.

    VB Code:
    1. Imports Microsoft.Data.Odbc 'couldnt use System.Data.Odbc for some reason but this works.
    2.  
    3. Dim myConn As String
    4.         myConn = "Driver={MySQL ODBC 3.51 Driver};SERVER=SERVER;DATABASE=workorder;UID=Username;PASSWORD=Password;OPTION=147458;"
    5. Dim mySQL As New OdbcConnection(myConn)
    6.         mySQL.Open()

    Well on to other things!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width