帮助转换为C#

时间:2011-03-14 19:46:36

标签: c# asp.net .net-3.5 c#-4.0

有人可以帮我转换为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  
    }
} 

4 个答案:

答案 0 :(得分:5)

// 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  
        }

    }
}

您可以使用this converter进行日后转换。

编辑:

你必须像这样

在c#中连接事件
protected void Page_Load(object sender, EventArgs e)
{
       Login1.Authenticate += Login1_Authenticate; 
}

答案 1 :(得分:2)

答案 2 :(得分:1)

使用此网站进行快速转换:http://converter.telerik.com

它看起来像一个事件处理程序,所以你必须在你的代码中连接它。

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  
        }

    }
}

答案 3 :(得分:1)

其他转换看起来不错,但是在关闭/处理数据库对象方面,原始代码有点弱。这是一个稍微重构的版本,解决了这些弱点:

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  
             }
          }

    }
}