title: Whitespace in SQL with C#
body: <p>I have a Windows application written in C# which connect to SQL database.</p>
  
  <p>I have text fields inside my application and update the database like this:</p>
  
  <pre><code>string name = textBox60.Text;
  
  string sql = "insert into myTable(Name) values ('" + name + "')";
  DbHelper.ExecuteNonquery(sql);
  
  public static int ExecuteNonquery(string sql)
      {
          using (SqlConnection cn = new SqlConnection(constr))
          {
              if (cn.State == ConnectionState.Closed)
              {
                  cn.Open();
              }
              else if (cn.State == ConnectionState.Open)
              {
                  cn.Close();
                  cn.Open();
              }
              else if (cn.State == ConnectionState.Broken)
              {
                  cn.Close();
                  cn.Open();
              }
              using (SqlCommand cm = new SqlCommand(sql, cn))
              {
                  return cm.ExecuteNonQuery();
              }
          }
      }
  </code></pre>
  
  <p>But for every data with type <code>nchar</code> in the database, they are full of white spaces. For example if I fill in <strong>abc</strong> in the text field, when I check the database it will become <code>"abc___________________"</code> (white spaces) something like this.</p>
  
  <p>How to prevent this other than trim the string only when I read them or to use <code>UPDATE</code> data SET <code>TRIM(data)</code> if I have tons of such data.</p>
  
  <p>Thanks for help.</p>