BULK INSERT table1 FROM 'd:\stu.txt' WITH (FIELDTERMINATOR = ',',ROWTERMINATOR = '\n' )
每行数据以逗号回车结
IELDTERMINATOR =','每个数据是以逗号分隔的,ROWTERMINATOR= ',\n'每行数据以逗号回车结
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "文本文件(*.txt)|*.txt";
openFileDialog1.ShowDialog();
textBox1.Text = openFileDialog1.FileName;
StreamReader SReader = new StreamReader(textBox1.Text, Encoding.Default);
// string fPath = this.fName.PostedFile.FileName;//获得要导入的文本文件
// string extName = fPath.Substring(fPath.LastIndexOf(".") + 1);//获得文件的扩展名
//string connStr = @"server=ZGC-20130925HMO;database=test;uid=sa;pwd=123";
SqlConnection con = new SqlConnection("server=ZGC-20130925HMO;database=test;uid=sa;pwd=123");
//数据库连接对象
con.Open();
SqlCommand com = new SqlCommand("BULK INSERT table1 FROM '" + textBox1.Text + "' WITH (FIELDTERMINATOR = ',',ROWTERMINATOR= '\n')", con);
//其中的 FIELDTERMINATOR=','指明字段间所使用的分隔符为逗号
//其中 ROWTERMINATOR= '\n'指明记录间所使用的分隔符为回车
com.ExecuteNonQuery();
// Response.Write("<script language=javascript& gt;alert('数据导入成功!')</script>");
con.Close();
}