输入字符串的格式不正确

时间:2016-10-12 11:11:43

标签: c# asp.net

  public int getcid(string UserName)
  {
            SqlConnection con = new SqlConnection(strConnString);
            con.Open();

            int js;

            string query = "select Username from register_tab where Email='" + UserName + "' ";
            sqlda = new SqlDataAdapter(query, con);
            DataSet ds = new DataSet();
            sqlda.Fill(ds);

            js = Convert.ToInt32(ds.Tables[0].Rows[0]["Username"].ToString());

            return (js);

enter image description here

1 个答案:

答案 0 :(得分:1)

将您的方法更改为此并检查出来:

SqlConnection con = new SqlConnection(strConnString);
            con.Open();
string js;
string query= "select Username from register_tab where Email= @username";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.Add("@username",SqlDbType.VarChar, 50).Value =  
                                           UserName;

using(SqlDataReader reader= cmd.ExecuteReader())
{
    while (reader.Read())
    {
        js=  reader["Username"].ToString();
    }
}

con.Close();
return js;

另外,为什么在查询中将UserName设置为电子邮件列? 如果你只想返回int,为什么要使用DataSet?

更新:无需转换为int。