asp.net下降文本格式数据导入到数据库中的代码
程序员文章站
2024-03-08 15:42:34
复制代码 代码如下:streamreader sr = new streamreader("e:\\123.txt");//文件路径 string oneline; whi...
复制代码 代码如下:
streamreader sr = new streamreader("e:\\123.txt");//文件路径
string oneline;
while ((oneline = sr.readline()) != null)
{
string[] str = oneline.split('|');
string sql = "insert into tabledx(number,name,produce,xinghao) values('" + str[0].tostring() + "','" + str[1].tostring().replace("'","'") + "','" + str[2].tostring() + "','" + str[3].tostring() + "')";
string strconn = "data source=.;initial catalog=master;user id=sa;password=sa";//连接字符串
sqlconnection conn = new sqlconnection(strconn);
int res = 0;
conn.open();
sqlcommand cmd = new sqlcommand(sql, conn);
res = cmd.executenonquery();
conn.close();
}
sr.close();
this.response.write("<script>alert('导入成功');</script>");