开发者

help on converting to C# [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. 开发者_如何学C Closed 11 years ago.

Can some one help me convert this to c#?

   //' Import the ODBC namespace for MySQL Connection  
   Imports System.Data.Odbc  
   Partial Class login  
       Inherits System.Web.UI.Page  

       Protected Sub Login1_Authenticate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs) Handles Login1.Authenticate  
           Dim cn As New OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=mydb; User=root;Password=;")  
           cn.Open()  
           Dim cmd As New OdbcCommand("Select * from login where username=? and password=?", cn)  

           //'Add parameters to get the username and password  

           cmd.Parameters.Add("@username", OdbcType.VarChar)  
           cmd.Parameters("@username").Value = Me.Login1.UserName  

           cmd.Parameters.Add("@password", OdbcType.VarChar)  
           cmd.Parameters("@password").Value = Me.Login1.Password  

           Dim dr As OdbcDataReader  
           //' Initialise a reader to read the rows from the login table.  
           //' If row exists, the login is successful  

           dr = cmd.ExecuteReader  

           If dr.HasRows Then  
               e.Authenticated = True  
               //' Event Authenticate is true  
           End If  

       End Sub  
   End Class  
    }
} 


// Import the ODBC namespace for MySQL Connection  
using System.Data.Odbc;
partial class login : System.Web.UI.Page
{



    protected void  
Login1_Authenticate(object sender, System.Web.UI.WebControls.AuthenticateEventArgs e)
    {
        OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=mydb; User=root;Password=;");
        cn.Open();
        OdbcCommand cmd = new OdbcCommand("Select * from login where username=? and password=?", cn);

        //Add parameters to get the username and password  

        cmd.Parameters.Add("@username", OdbcType.VarChar);
        cmd.Parameters["@username"].Value = this.Login1.UserName;

        cmd.Parameters.Add("@password", OdbcType.VarChar);
        cmd.Parameters["@password"].Value = this.Login1.Password;

        OdbcDataReader dr = default(OdbcDataReader);
        // Initialise a reader to read the rows from the login table.  
        // If row exists, the login is successful  

        dr = cmd.ExecuteReader();

        if (dr.HasRows) {
            e.Authenticated = true;
            // Event Authenticate is true  
        }

    }
}

you can use this converter for future conversions.

EDIT:

you'll have to wire up the event in c# like this

protected void Page_Load(object sender, EventArgs e)
{
       Login1.Authenticate += Login1_Authenticate; 
}


Use this online tool, http://www.developerfusion.com/tools/convert/vb-to-csharp/


Quick conversion using this site: http://converter.telerik.com

It looks like an event handler, so you'll have to wire that up in your code as well.

using System.Data.Odbc;
partial class login : System.Web.UI.Page
{

    protected void  // ERROR: Handles clauses are not supported in C#
Login1_Authenticate(object sender, System.Web.UI.WebControls.AuthenticateEventArgs e)
    {
        OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=mydb; User=root;Password=;");
        cn.Open();
        OdbcCommand cmd = new OdbcCommand("Select * from login where username=? and password=?", cn);

        //Add parameters to get the username and password  

        cmd.Parameters.Add("@username", OdbcType.VarChar);
        cmd.Parameters("@username").Value = this.Login1.UserName;

        cmd.Parameters.Add("@password", OdbcType.VarChar);
        cmd.Parameters("@password").Value = this.Login1.Password;

        OdbcDataReader dr = default(OdbcDataReader);
        // Initialise a reader to read the rows from the login table.  
        // If row exists, the login is successful  

        dr = cmd.ExecuteReader;

        if (dr.HasRows) {
            e.Authenticated = true;
            // Event Authenticate is true  
        }

    }
}


The other conversions look good, but the original code was a little weak in terms of closing / disposing of the database objects. Here is a slightly refactored version that addresses those weaknesses:

using System.Data.Odbc;
partial class login : System.Web.UI.Page


{

    protected void  // ERROR: Handles clauses are not supported in C#
    Login1_Authenticate(object sender, System.Web.UI.WebControls.AuthenticateEventArgs e)
    {
        using(OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=mydb; User=root;Password=;"));
        using(OdbcCommand cmd = new OdbcCommand("Select * from login where username=? and password=?", cn))
        {
           cn.Open();

           //Add parameters to get the username and password  

           cmd.Parameters.Add("@username", OdbcType.VarChar);
           cmd.Parameters("@username").Value = this.Login1.UserName;

           cmd.Parameters.Add("@password", OdbcType.VarChar);
           cmd.Parameters("@password").Value = this.Login1.Password;

           // Initialise a reader to read the rows from the login table.  
           // If row exists, the login is successful  

           using(OdbcDataReader dr = cmd.ExecuteReader)
           {
             if (dr.HasRows) {
                 e.Authenticated = true;
               // Event Authenticate is true  
             }
          }

    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜