private static void ImportData(string fileName) { string temp = string.Empty; using (StreamReader reader = new StreamReader(fileName, Encoding.Default)) { reader.ReadLine();//去除第一行 while (!string.IsNullOrEmpty(temp = reader.ReadLine())) { var strs = temp.Split(‘,‘); string sql = string.Format("insert into Student(stuName,stuBirthday,stuPhone) values(‘{0}‘,‘{1}‘,‘{2}‘)", strs[0],strs[1], strs[2]); string connStr = "server=.;uid=sa;pwd=123;database=practice"; using (SqlConnection conn = new SqlConnection(connStr)) { using(SqlCommand cmd = conn.CreateCommand()) { conn.Open(); cmd.CommandText = sql; cmd.ExecuteNonQuery(); } } } } }
时间: 2024-12-25 14:01:42