问个数据库读取问题(C#)

来源:百度知道 编辑:UC知道 时间:2024/06/02 09:31:03
try
{
string strconnection = "user id=sa;password=;database=SEM系统管理";//连接SQL数据库的字符串
SqlConnection aconnection = new SqlConnection(strconnection);
aconnection.Open();
SqlCommand com = new SqlCommand();
com.CommandText = "select 密码 from 登陆表 where 用户名='"+textBox1.Text+"'";
com.CommandType = CommandType.Text;
com.Connection = aconnection;
SqlDataReader reader1 = com.ExecuteReader();
while (reader1.Read())
{
string AA = null;
AA = reader1.GetString(0);
if (AA == textBox2.Text)
flag = true;
}
reader1.Close();

我用的是SQL2000,我设了密码长度是10,但实际输入的密码长度是5,为什么读出来的密码长度是 密码+5个空格[长度等

你的登录表中的密码字段的类型 最好设置成 Nvarchar(10);
Trim();去掉空格的函数!

reader1.GetString(0).Tram()

.Trim()

把if (AA == textBox2.Text) 改成if (AA == textBox2.Text.Trim()) 就哦了
Trim()是去掉最后一个字符后面的空格