我的 Visual Studio 2012 网站项目中有关于 SQL Server 的问题。例如; 我有一个名为“user”的表,其中包含“name”(varchar(16))和“surname”(varchar(16))列。我添加了新数据,例如名称:“john” 姓:“holiday”,但 SQL Server 会自动用空格字符填充变量的其余部分。好像。我怎样才能阻止这个?我希望我清楚地解释了我的问题。
protected void LoginButton_Click(object sender, EventArgs e)
{
string connectionString = "Data Source=MYPC-PC\\SQLEXPRESS;Initial Catalog=fff;Integrated Security=True";
SqlConnection connection = new SqlConnection(connectionString);
try
{
connection.Open();
}
catch (Exception s)
{
FailureText.Text = s.ToString();
}
SqlCommand cmd = new SqlCommand("SELECT * FROM [person] WHERE user_name='" + UserName.Text + "' AND user_password='" + Password.Text + "' ", connection);
SqlDataReader dr = null;
dr = cmd.ExecuteReader();
while (dr.Read())
{
if (this.CompareStrings(dr["user_name"].ToString(), UserName.Text) &&
this.CompareStrings(dr["user_password"].ToString(), Password.Text))
{
FailureText.Text = "ok";
}
else
{
FailureText.Text = "no";
}
}
connection.Close();
dr.Close();
}
例如,当我调试 "dr["user_name"]" 变量时,就像 watch 中的那样: