欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net下降文本格式数据导入到数据库中的代码

程序员文章站 2024-03-07 18:56:27
复制代码 代码如下: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>");