解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    using System.Xml;
    using System.IO;namespace XmlDatabase
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {
                           
                
            }        private void button1_Click(object sender, EventArgs e)
            {
               FileStream file = new FileStream("H:\\ADO.NET\\XmlDatabase\\StudentDetails.txt", FileMode.OpenOrCreate,FileAccess.Read);
               StreamReader sr = new StreamReader(file);
               //String strLine;
                //int str=sr.Read();
                String code, name, sex, classcode, address, birthday;
                SqlConnection con = new SqlConnection();
                //con.ConnectionString = "server=(local);database=student;uid=sa;pwd=123456";
                con.ConnectionString = "server=ideapad\\SQLEXPRESS2008;database=student;uid=sa;pwd=jasmine19940101";
                //string str = "SELECT   *   FROM   OPENROWSET(BULK   N'F:\\ADO.NET\\XmlDatabase\\StudentDetails.txt',   SINGLE_BLOB)   AS   mytable";
                con.Open();
                for (int i = 0; i < 3; i++)
                {
                    code = sr.ReadLine();
                    name = sr.ReadLine();
                    sex = sr.ReadLine();
                    classcode = sr.ReadLine();
                    address = sr.ReadLine();
                    birthday = sr.ReadLine();
                    SqlCommand cmd = new SqlCommand(string.Format("select Count(*) from newtable where code= '{0}'", code), con);
                    string sql = "insert into studenttable(学号,姓名,性别,班级编号,籍贯,出生日期) values('" + code + "','" + name + "','" + sex + "','" + classcode + "','" + address + "','" + birthday + "')";
                    cmd.CommandText = sql;
                    cmd.ExecuteNonQuery();    
                }
                SqlDataAdapter da = new SqlDataAdapter("select * from studenttable", con);
                DataSet ds = new DataSet();
                da.Fill(ds, "学生");
                ds.WriteXml("H:\\ADO.NET\\XmlDatabase\\StudentDetails.xml");
                XmlTextReader student = new XmlTextReader("H:\\ADO.NET\\XmlDatabase\\StudentDetails.xml");
                DataSet d = new DataSet();
                d.ReadXml(student);
                dataGridView1.DataSource = d.Tables[0].DefaultView;
            }
        }
    }
    这是我写的代码,但是读入文件的时候中文读不出来
      

  2.   

    我不用xml,它有令人困惑的“命名空间”繁琐问题。我们使用json。