首页 > 编程 > .NET > 正文

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

2024-07-10 12:42:11
字体:
来源:转载
供稿:网友
代码如下:
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>");
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表