Why are the white spaces added to a row in my sql table?
I'm not sure why this is, basically when I insert a row using the following function:
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)开发者_StackOverflow社区;
cmd.Connection = myConnection;
cmd.ExecuteNonQuery();
myConnection.Close();
}
catch (Exception excep)
{
string error = excep.ToString();
Response.Write(error);
return false;
}
return true;
}
The field 'Password' ends up having white spaces in it (as in "Password "). So whatever the password is it's trailed by white spaces.
The strange thing is this doesn't happen to the field 'UserName', both are of type char(50).
Are you sure one is not varchar(50) and one is char(50). You will find that database columns with char specified will always be that length.
I don't know if this answers your question but why don't you use varchar type instead of char. Or you can use TRIM() function in your select query.
精彩评论