我不确定这是为什么,基本上当我使用以下函数插入一行时:
public bool CreateUser(string username, string password, string email, int age = 0)
{
SqlConnection myConnection = new SqlConnection();
myConnection.ConnectionString = "Data Source=localhost\\SQLEXPRESS;" +
"Initial Catalog=Pubs;Integrated Security=SSPI;Trusted_Connection=True;";
try
{
myConnection.Open();
string query = "INSERT INTO SiteUser VALUES('" + username + "', '" + password + "', '" + email + "', " + age + ")";
SqlCommand cmd = new SqlCommand(query);
cmd.Connection = myConnection;
cmd.ExecuteNonQuery();
myConnection.Close();
}
catch (Exception excep)
{
string error = excep.ToString();
Response.Write(error);
return false;
}
return true;
}
“密码”字段最终包含空格(如“密码”)。所以无论密码是什么,它后面都有空格。
奇怪的是,这不会发生在字段“UserName”上,两者都是 char(50) 类型。
最佳答案
您确定一个不是 varchar(50) 而一个是 char(50)。您会发现指定了 char 的数据库列将始终是该长度。
关于asp.net - 为什么将空格添加到我的 sql 表中的一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4013216/