你确定你的T_Person表存在??,你查看一下你的name是否乱码,我的成功了- private void Form1_Load(object sender, EventArgs e)
- {
- if (openFileDialog1.ShowDialog() == DialogResult.OK)
- {
- using (FileStream fs = File.OpenRead(openFileDialog1.FileName))
- {
- using (StreamReader sr = new StreamReader(fs,Encoding.Default))
- {
- string line = null;
- while ((line = sr.ReadLine()) != null)
- {
- string[] data = line.Split('|');
- string name = data[0];
- string age = data[1];
- string connectionString =
- "Data Source=MXF-PC;Initial Catalog=YILIAN;User ID=sa;Password=sa";
- using (SqlConnection conn = new SqlConnection(connectionString))
- {
- conn.Open();
-
- using (SqlCommand command = conn.CreateCommand())
- {
-
- command.CommandText = "insert into T_Person (name,age)values(@name,@age)";
- command.Parameters.Add(new SqlParameter("name", name));
- command.Parameters.Add(new SqlParameter("age", age));
- command.ExecuteNonQuery();
- MessageBox.Show("插入成功");
-
- }
- }
- }
- }
- }
- }
- }
复制代码 |