为什么数据表忽略了初始空格

时间:2013-11-08 10:51:08

标签: c# asp.net datatable

Datatable不占用初始空格,因此当我将其插入数据库并通过select语句检索它并使用stringBuilder在页面上显示时,初始空格消失,文本不会出现在屏幕上,因为它是在文本框中写的

DataTable1.Rows[0][0]=TextBox1.Text;

这是我插入数据库的方式

 public static void Insert(DataTable table)
        {
            StringBuilder Col = new StringBuilder();
            StringBuilder Val = new StringBuilder();
            string Query, finalVal;
            int i, j;
            int count = 0;
            int ColumnCount = table.Columns.Count;
            for (i = 0; i < ColumnCount; i++)
            {
                if (table.Rows.OfType<DataRow>().Any(r => !r.IsNull(table.Columns[i].ColumnName)))
                {
                    Col.Append(table.Columns[i].ColumnName);
                    Col.Append(",");
                    count++;
                }
            }
            Col.Remove(Col.Length - 1, 1);
            string[] columnName = Col.ToString().Split(',');
            foreach (DataRow r in table.Rows)
            {
                Val.Append(",(");
                for (j = 0; j < count; j++)
                {
                    if (r[columnName[j]] != null)
                    {
                        Val.Append("'" + r[columnName[j]] + "'");
                        Val.Append(",");
                    }
                    else
                    {
                        Val.Append("null,");
                    }
                }
                Val.Append(")");
                Val.Remove(Val.Length - 2, 1);

            }
            finalVal = Val.ToString().Substring(1);
            Query = "Insert into " + table.TableName + "(" + Col + ")" + "Values" + finalVal;
            EduDB.ExecuteNonQuery(Query);
        }

1 个答案:

答案 0 :(得分:2)

在插入数据库之前,您必须修剪字符串。请检查出来