调用方法的灵异事件
我自己写的一个类 灵异事件发生在:public bool userIsExist(User user) 方法和 public bool blogNameIsExist(User user) 灵异情况看调用
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
/// <summary>
/// User 的摘要说明
/// </summary>
public class User
{
public string strName;
public string strPwd;
public string strSex;
public string strEmail;
public string strBlogName;
public string strBlogType;
public SqlConnection conn = DataBase.createDBconn();
public User()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public bool isSave(User user)
{
try
{
if ((userIsExist(user)&&blogNameIsExist(user)))
{
conn.Open();
string strSql = "insert into userTable(userName,userPwd,sex,Email,blogName,blogType)";
strSql += " values('" + user.strName + "','" + user.strPwd + "','" + user.strSex + "','" + user.strEmail + "','" + user.strBlogName + "','" + user.strBlogType + "')";
SqlCommand sqlCommand = new SqlCommand(strSql, conn);
sqlCommand.ExecuteNonQuery();
conn.Close();
return true;
}
else
{
return false;
}
}
catch (Exception)
{
conn.Close();
return false;
}
}
public bool userIsExist(User user)
{
try
{
conn.Open();
string strSql = "select count(*) from userTable where (userName like '" + user.strName + "')";
SqlCommand sqlCommand = new SqlCommand(strSql, conn);
int exist = (int)sqlCommand.ExecuteScalar();
if (exist > 0)
{
return true;
}
else
{
return false;
}
conn.Close();
}
catch (Exception)
{
conn.Close();
return false;
}
}
public bool blogNameIsExist(User user)
{
try
{
conn.Open();
string strSql = "select count(*) from userTable where (blogName like '" + user.strBlogName + "')";
SqlCommand sqlCommand = new SqlCommand(strSql, conn);
int exist = (int)sqlCommand.ExecuteScalar();
if (exist > 0)
{
return true;
}
else
{
return false;
}
conn.Close();
}
catch (Exception)
{
conn.Close();
return false;
}
}
public bool checkUser(User user)
{
try
{
conn.Open();
string strSql = "select count(*) from userTable where (userName like '" + user.strName + "' and userPwd like '" + user.strPwd + "')";
SqlCommand sqlCommand = new SqlCommand(strSql, conn);
int exist = (int)sqlCommand.ExecuteScalar();
if (exist > 0)
{
return true;
}
else
{
return false;
}
conn.Close();
}
catch (Exception)
{
conn.Close();
return false;
}
}
}
在一个用户控件中的调用
protected void btnSubmit_Click(object sender, EventArgs e)
{
User user = new User();
user.strName = this.txtUserName.Value;
user.strPwd = this.txtUserPwd.Text;
if (this.rdoBoy.Checked)
{
user.strSex = "男";
}
else
{
user.strSex = "女";
}
user.strEmail = this.txtEmail.Text;
user.strBlogName = this.txtBlogName.Value;
user.strBlogType = this.ddlBlogType.Text;
以下是问题的关键(单独使用是指 下面两句只选一句调用 而不是分开 既然分开调用 后面也总是false)
//Response.Write(user.userIsExist(user).ToString());//单独使用能正确返回 true or false
//Response.Write(user.blogNameIsExist(user).ToString()); 单独使用能正确返回 true or false
如果一起调用的话
//Response.Write("用户"+user.userIsExist(user).ToString() + "博客名"+user.blogNameIsExist(user).ToString()); //判断用户是否存在能正确返回true or false 判断博客名是否存在总是返回 false
把他们调换位置
//Response.Write("博客名"+user.blogNameIsExist(user).ToString() + "用户"+user.userIsExist(user).ToString() ); //判断博客名是否存在能正确返回true or false 判断用户是否存在总是返回 false
}
谢谢