我在Visual Studio 2012網站項目中遇到了有關SQL Server的問題。例如;我有一個名爲「user」的表,它具有列「name」(varchar(16))和「surname」(varchar(16))。我添加了一些新的數據,例如name:「john」surname:「holiday」,但是SQL Server會自動用空白字符填充變量的其餘部分。它好像是。我怎樣才能阻止呢?我希望我明確解釋我的問題。如何防止在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();
}
例如,當我調試「博士[」 USER_NAME「]」變量就是這樣在手錶:
這當類型被定義爲'CHAR(16)通常發生在數據庫或在沿途的一些點'。 – dasblinkenlight
幾個問題:你如何添加到用戶表?其次,[name]列也是varchar?這聽起來像是一個字符列,哪些空間墊。 – DWright
#Mate你究竟是什麼意思?我無法理解。這不是關於代碼,我認爲MSSQL存在問題。 #DWright我從visual studio sql server explorer手動添加。是[名稱]列也是varchar – Jemo