日期:2014-05-18  浏览次数:21317 次

C#字符串太长如何换行??
完整字符串:string SqlIns = "insert into tb_abc (A1,A2,A3,A4,A5,A6,A7) values ('" + dataGridView1.Rows[i].Cells[0].Value + "','" + dataGridView1.Rows[i].Cells[1].Value + "','" + dataGridView1.Rows[i].Cells[2].Value + "','" + dataGridView1.Rows[i].Cells[3].Value + "','" + dataGridView1.Rows[i].Cells[4].Value + "','" + dataGridView1.Rows[i].Cells[5].Value + "','" + dataGridView1.Rows[i].Cells[6].Value + "')";


我按以下格式换行老是提示“常量中有换行符”,怎么回事呀。
string SqlIns = "insert into tb_abc (A1,A2,A3,A4,A5,A6,A7) values 
" ('" + dataGridView1.Rows[i].Cells[0].Value + "',"+
"'" + dataGridView1.Rows[i].Cells[1].Value + "',"+
"'" + dataGridView1.Rows[i].Cells[2].Value + "',"+
"'" + dataGridView1.Rows[i].Cells[3].Value + "',"+
"'" + dataGridView1.Rows[i].Cells[4].Value + "',"+
"'" + dataGridView1.Rows[i].Cells[5].Value + "',"+
"'" + dataGridView1.Rows[i].Cells[6].Value + "')";

------解决方案--------------------
用+号连接字符串,
或者你定义一个可变字符串,例如:
 StringBuilder strSql = new StringBuilder();
strSql.Append("select ");
strSql.Append(“name”);
strSql.Append(" FROM ");
strSql.Append(“table”);
------解决方案--------------------
sql语句中不能强行换行的,楼主不能那么写哦!
楼主可以写一个追加字符串的程序,定义一个可变字符串,例如:
 StringBuilder strSql = new StringBuilder();
strSql.Append("select ");
strSql.Append(“name”);
strSql.Append(" FROM ");
strSql.Append(“table”);
这样strsql的最终结果就是"select name from table";