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);
}