VB 中的 Function ...End function 在 C#中要怎么写

发布网友

我来回答

1个回答

热心网友

Function ......End function是vb中的函数声明,跟c#的方法是一个东西,你上面这个只是按钮点击事件,并不是一般的方法,上面功能已经完整了,不需要再 多做处理,如果你是想把检查用户名封装成方法,可以这样

public static string connstring = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\XM.mdf;Integrated Security=True;User Instance=True";
public boolean CheckUser(string name)
{
if(name==null || name.Trim()=="") return true;
  using(SqlConnection sqlconn = new SqlConnection(connstring))
  { 
         sqlconn.Open();
           string sql = "select * from Users where name ='" +name+"'" ;//注意这里你原本的语句写错了
           SqlCommand command = new SqlCommand(sql, sqlconn);
           using(SqlDataReader reader = command.ExecuteReader())
           {
           if (reader.Read())  return true;
           else return false;
           }
  }
return true;
}
protected void Button2_Click(object sender, EventArgs e)
   {
     string username=TextBox1.Text.Trim();
        if (username == "")
       {
           Label2.Text = "请输入用户名";
       }
       else
       {
          if(CheckUser(username))
           {
               Label2.Text = "用户名已存在";
               TextBox1.Text = "";
               TextBox1.Focus();
           }
           else
           {
               Label2.Text = "√ 用户名可用";
           }
       }
}

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com