您的位置:首页 > 职场人生

黑马程序员-ADO.NET

2012-04-22 16:38 204 查看
---------------------- Windows Phone 7手机开发.Net培训、期待与您交流! ----------------------

学习ado.net所有做的例子

数据的导入导出

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.IO;

using System.Data.SqlClient;

namespace 数据的导入导出

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)

        {

           

            if (openFileDialog1.ShowDialog() == DialogResult.OK)

            {

                using (FileStream file = File.OpenRead(openFileDialog1.FileName))

                {

                    

                    using (StreamReader streamreader = new StreamReader(file))

                    {

                        using (

   SqlConnection conne = new SqlConnection

                 (@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;

Integrated Security=True;User Instance=True"))

                            {

                                conne.Open();

                                using (SqlCommand cmd = conne.CreateCommand())

                                {

 string line = streamreader.ReadLine();

                                    cmd.CommandText = "insert into t_user(name,age) values(@name,@age)";

                                    while (line!= null)

                                    {

                                       

                                        string[] str = line.Split('|');

                                        string name = str[0];

                                        int age = Convert.ToInt32(str[1]);

                                        cmd.Parameters.Clear();//参数不能重复的添加

                                        cmd.Parameters.Add(new SqlParameter("name", name));

                                        cmd.Parameters.Add(new SqlParameter("age", age));

                                        cmd.ExecuteNonQuery();

                                        line = streamreader.ReadLine();

                                    }

                                }

                            }

                        }

                    MessageBox.Show("导入成功!");

                    }

            }

        

        }

        private void button1_Click_1(object sender, EventArgs e)

        {            

            SaveFileDialog saveFileDialog1 =new SaveFileDialog();

            saveFileDialog1.AddExtension = true;

            saveFileDialog1.Filter = "文本类型(*.txt)|*.txt";

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)

            {                

                    string path = saveFileDialog1.FileName;

                    using (FileStream file = File.OpenWrite(path))

                    {

                        using (StreamWriter file1 = new StreamWriter(file))

                        {

                            using (

  SqlConnection conne = new SqlConnection

                (@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;

Integrated Security=True;User Instance=True"))

                            {

                                conne.Open();                                

                                using (SqlCommand cmd = conne.CreateCommand())

                                {

                                    cmd.CommandText = "select * from t_user";

                                    using (SqlDataReader reader = cmd.ExecuteReader())

                                    {

                                        while (reader.Read())

                                        {

                                            string name = reader.GetString(reader.GetOrdinal("name"));

                                            int age = reader.GetInt32(reader.GetOrdinal("age"));

                                            file1.Write(name + "|" + age+"\r\n");        

                                        }

                                        MessageBox.Show("导出成功!");

                                    }

                                }

                            }

                        }

                    }

            }

        }      

    }

}

省市选择

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.Configuration;

namespace 省市选择

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)

        {           

            //得到配置文件中的连接数据库字符串

            string connstr=ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;

            //连接数据库

            using (SqlConnection conne = new SqlConnection(connstr))

            {

                //打开数据库

                conne.Open();

                //建立对数据库的操作

                using (SqlCommand cmd = conne.CreateCommand())

                {

                    //向数据库中执行一段查询所有市的代码

                    cmd.CommandText = "select * from promary";

                    //使用SqlDateReader进行数据的读取

                    using (SqlDataReader datereader = cmd.ExecuteReader())

                    {

                        //读取所有数据

                        while (datereader.Read())

                        {

                            //实例化一个 ProvincaItam 的类

                            ProvincaItam Itam = new ProvincaItam();

                            //得到当前数据行的省名

                            Itam.Name = datereader.GetString(datereader.GetOrdinal("proName"));

                            //得到当前数据行的ID

                            Itam.Id = datereader.GetInt32(datereader.GetOrdinal("proId"));

                            //把得到的省名添加到列表项  DisplayMember项已设为 Name                            

                            comb省.Items.Add(Itam);

                        }

                    }

                }

            }

        }

        //创建一个用于储存信息的类

        class ProvincaItam

        {

            public string Name { get; set; }

            public int Id { get; set; }

        }

        private void comb省_SelectedIndexChanged(object sender, EventArgs e)

        {

            //实例化一个名为itam的ProvincaItam类  并把当前选的的comb省项的Item值赋给itam

            ProvincaItam itam = (ProvincaItam)comb省.SelectedItem;

            //声明一个int类型的变量并把id的值赋给它

            int proId = itam.Id;

            //清空Items的所有项

            comb市.Items.Clear();

            //得到配置文件中的连接数据库字符串

            string connstr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;

            //连接数据库

            using (SqlConnection conne = new SqlConnection(connstr))

            {

                //打开数据库

                conne.Open();

                //建立对数据库的操作

                using (SqlCommand cmd = conne.CreateCommand())

                {

                    //查数在数据中city表中当前选中省中所有市的数据

                    cmd.CommandText = "select *from city where proid=@proid";

                    cmd.Parameters.Add(new SqlParameter("proid", proId));

                    //使用SqlDateReader进行数据的读取

                    using (SqlDataReader datereader = cmd.ExecuteReader())

                    {

                        //读取当前省中所有市的数据

                        while (datereader.Read())

                        {

                            //得到当前行的市名

                            string name = datereader.GetString(datereader.GetOrdinal("cityname"));

                            //把所有市名添加到comb市列表项中

                            comb市.Items.Add(name);

                        }

                    }

                }

            }          

        }        

    }

}

手机归属地查询

归属地的数据我是上网找的,所以我是根据我下的数据来写的导入

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.IO;

using System.Data.SqlClient;

using System.Configuration;

namespace 手机归属地查询

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)

        {

        }

        private void button1_Click(object sender, EventArgs e)

        {

            OpenFileDialog openfile=new OpenFileDialog();

            if (openfile.ShowDialog() != DialogResult.OK)

            {

                return;

            }

            else

            {

                using(FileStream file=File.OpenRead(openfile.FileName))

                {

                    using (StreamReader reader=new StreamReader(file))

                    {

                        string Connstr = ConfigurationManager.ConnectionStrings["conntrs"].ConnectionString;

                       using( SqlConnection cnne=new SqlConnection(Connstr))

                        {

                            cnne.Open();

                            using (SqlCommand cmd = cnne.CreateCommand())

                            {

                                cmd.CommandText = "insert into pohone(No,Name) values(@No,@Name)";

                                string line = reader.ReadLine();

                                while (line != null)

                                {

                                    string[] str = line.Split('|');

                                    string No = str[0];

                                    string Name = str[1];

                                    cmd.Parameters.Clear();

                                    cmd.Parameters.Add(new SqlParameter("No", No));

                                    cmd.Parameters.Add(new SqlParameter("Name", Name));

                                    cmd.ExecuteNonQuery();

                                    line = reader.ReadLine();

                                }

                            }

                        }

                    }

                    MessageBox.Show("导入成功!");

                }

            }

        }

        private void button2_Click(object sender, EventArgs e)

        {

            string phone = textBox1.Text;

            phone = phone.Substring(0, 7);

              string Connstr = ConfigurationManager.ConnectionStrings["conntrs"].ConnectionString;

              using (SqlConnection cnne = new SqlConnection(Connstr))

              {

                  cnne.Open();

                  using (SqlCommand cmd = cnne.CreateCommand())

                  {

                      cmd.CommandText = "select * from pohone where No=@phone";

                      cmd.Parameters.Add(new SqlParameter("phone", phone));

                      SqlDataReader read = cmd.ExecuteReader();

                      while (read.Read()) 

                      {

                          

                          string Name=read.GetString(read.GetOrdinal("Name"));

                          textBox2.Text =Name;

                      }

                  }

              }

        }

    }

}

---------------------- Windows Phone 7手机开发.Net培训、期待与您交流! ----------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息