Asp的批插入
从文本中读取三个字段,然后分别插入相对应的数据库中protected void Button1_Click(object sender, EventArgs e)
{
string s = this.TextBox1.Text;
string serverName = "";
string name = "";
string num = "";
string[] srr = s.Split('\n');
SqlConnection con = new SqlConnection("server=.;database=new;uid=sa;");
string sql = "insert into newtable (nservername,ntext,nnum)";
for (int i = 0; i < srr.Length; i++)
{
serverName = srr[i].Substring(0, 20);//截取第一个字段的字符串
name = srr[i].Substring(20, 8);//
num = srr[i].Substring(28);//
if (i < srr.Length - 1)
{
sql += " select '" + serverName + "','" + name + "','"+num+"'union";
}
else
{
sql += " select '" + serverName + "','" + name + "','"+num+"";
}
}
Response.Write(sql);
SqlCommand cmd = new SqlCommand(sql, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Response.Write("<script> alert('插入成功')</script>");
this.TextBox1.Text = "";
}
在文本中是一行一行的显示 读取到文本中也是一行一行的显示 可我用Respose.Write就出现
Aegwynn A 10800 Aerie Peak A 10000 Aerie Peak H 5000 Aggramar A 12000 Aggramar H 9000 Altar of Storms A 1100 Alexstrasza A 6800 Alleria A 8200 Arathor A 7000 Anub'arak H 9000 Baelgun A 7400 Blackhand H 5000 Borean Tundra A 1800 Bronzebeard A 3950 Bronzebeard H 10000 Cenarius A 10000 Darrowmere A 5000 Demon soul A 2850 Draka A 10000 Draenor H 10000 Dunemaul H 4300 Duskwood H 10000 Eredar A 913 Fizzcrank A 7999 Galakrond A 10000 Gilneas A 3000 Gorefiend A 1365
向数据库中插入就出错 说是lenth不对应。 --------------------编程问答-------------------- 是不是数据库中的字段长度限制额
或许你的字符串太长了??? --------------------编程问答-------------------- 字段超过设置长度.设置为text 或ntext
补充:.NET技术 , ASP.NET