2011-03-14 88 views
-1

有人可以帮我转换成C#吗?帮助转换为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 
    } 
} 
+7

StackOverflow不是代码转换服务。请花时间研究:这两种语法并不相距甚远。您可以先将关键字设置为小写,然后学习如何在C#中声明变量。然后,你甚至可以从编译错误中继续。 – 2011-03-14 19:48:33

+0

非常抱歉,我不知道代码转换,我认为堆栈溢出是一个你可以得到帮助的地方?感谢社区,我肯定不会发布我知道的可用于代码转换的事情。 – 2011-03-14 20:04:29

+1

此外,还有一些需要手动更改。 – 2011-03-14 20:13:31

回答

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; 
} 
+0

现在正在快速测试的耶稣 – 2011-03-14 19:49:02

+0

当它从相同的地方得到相同的代码。大声笑。 – Robaticus 2011-03-14 19:50:03

+1

@Garrith:那是因为@StackOverflowException无疑使用了一个像http://www.developerfusion.com/tools/convert/vb-to-csharp/这样的工具来提供这个答案。 (并不是说这有什么不妥,我的+1)。像这样的工具对于为你节省一些繁忙的工作是非常宝贵的。 – 2011-03-14 19:50:26

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 
     } 

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

    } 
} 
+0

更好的办法是将ODBC驱逐到MySQL ADO.NET驱动程序。 – 2011-03-14 20:32:59

+0

那里没有参数! – RQDQ 2011-03-14 20:40:54