using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
namespace WindowsApplication3
{
    public partial class Form1 : Form
    {
        int i = 0;//i用来去顶其所在数据表的行号,一开始为第一行
        DataSet ds;
        SqlDataAdapter da;
        string str = "Data Source=20111229-1228\\SQLEXPRESS;Initial Catalog=test;User ID=sa;Password=longhai";
        string strsql = "select * from student";
        SqlConnection cn;        public Form1()
        {
            InitializeComponent();
        }        private void button8_Click(object sender, EventArgs e)
        {
            int k = 0;//用来确定要查找数据行的下标
            if (textBox5.Text.Trim() == string.Empty)
            {
                MessageBox.Show("查找内容不能为空!");
                textBox5.Focus();
            }
            else
                for (k = 0; k < ds.Tables["student"].Rows.Count; k++)
                    if (ds.Tables["student"].Rows[k]["id"].ToString() == textBox5.Text) //找到textBox5.Text文本框中相匹配的数据行,其行号为k
                    {
                        i = k;
                        show(i);
                        break;
                    }
            if (k == ds.Tables["student"].Rows.Count)
                MessageBox.Show("无该学生信息");
            textBox5.Text = "";        }
        void show(int i)
        {
            try
            {
                if (ds != null)
                    ds.Clear();
                else
                {
                    ds = new DataSet();
                    da = new SqlDataAdapter(strsql, str);
                }
                //由于DataSet为断开连接的,因此在显示时对数据集重新填入,从而显示的为更新后的值
                da.Fill(ds, "student");
                DataRow dr = ds.Tables["student"].Rows[i];
                textBox1.Text = dr["id"].ToString();
                textBox2.Text = dr["name"].ToString();
                textBox3.Text = dr["department"].ToString();
                textBox4.Text = dr["age"].ToString();
                pictureBox1.Image = Image.FromFile(dr["image"].ToString());
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }        private void Form1_Load(object sender, EventArgs e)
        {
            cn = new SqlConnection(str);
            show(i);     
        }        private void button4_Click(object sender, EventArgs e)
        {
            i = 0;
            show(i);
        }        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection cn = new SqlConnection(str);
                cn.Open();
                SqlCommand Sqlcmd = new SqlCommand("select count(*) from student where id='" + textBox1.Text.Trim() + "'", cn);                // Sqlcmd.Connection.Open();
                int a = (int)Sqlcmd.ExecuteScalar();//返回的为源表中与输入的学号相等的个数
                Sqlcmd.Connection.Close();
                if (a > 0)
                {
                    MessageBox.Show("此学号已存在,请重新输入");
                    return;
                }
                //上述方法通过求出在student表中,输入的学号与表中的原有数据是否冲突
                ds.Clear();
                da.Fill(ds, "student");
                ds.Tables["student"].PrimaryKey = new DataColumn[] { ds.Tables["student"].Columns["id"] };//设置数据表student 表的主码,为后来的使用Update 方法打下基础
                SqlCommandBuilder sqlCommandBuilder1 = new SqlCommandBuilder(da); //通过 CommandBulider对象自动生成DbDataAdapter 的**Command属性
                DataRow dr = ds.Tables["student"].NewRow();
                dr["id"] = textBox1.Text;
                dr["name"] = textBox2.Text;
                dr["department"] = textBox3.Text;
                dr["image"] =textBox5.Text;                if (textBox4.Text != "")
                    dr["age"] = int.Parse(textBox4.Text);
                else
                    dr["age"] = 0;
                ds.Tables["student"].Rows.Add(dr);
                da.Update(ds, "student");
                ds.AcceptChanges();
                MessageBox.Show("数据插入成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }        private void button2_Click(object sender, EventArgs e)
        {
            for (int k = 0; k < ds.Tables["student"].Rows.Count; k++)
                if (ds.Tables["student"].Rows[k]["id"].ToString() == "" + textBox1.Text + "" && ds.Tables["student"].Rows[k]["id"].ToString() != ds.Tables["student"].Rows[i]["id"].ToString())
                {
                    MessageBox.Show("此学号已存在,请重新输入");
                    textBox1.Text = "";
                    return;
                }
            ds.Clear();
            da.Fill(ds, "student");
            ds.Tables["student"].PrimaryKey = new DataColumn[] { ds.Tables["student"].Columns["id"] };
            SqlCommandBuilder sqlCommandBuilder1 = new SqlCommandBuilder(da);
            ds.BeginInit();//挂起修改;
            ds.Tables["student"].Rows[i]["id"] = textBox1.Text;
            ds.Tables["student"].Rows[i]["name"] = textBox2.Text;
            ds.Tables["student"].Rows[i]["department"] = textBox3.Text;
            if (textBox4.Text != "")
                ds.Tables["student"].Rows[i]["age"] = int.Parse(textBox4.Text);
            else
                ds.Tables["student"].Rows[i]["age"] = 0;
            ds.EndInit();//终止挂起修改
            da.Update(ds, "student");
            ds.AcceptChanges();
            MessageBox.Show("数据修改成功");
            Refresh();
        }        private void button3_Click(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(strsql, str);
            da.Fill(ds, "student");
            ds.Tables["student"].PrimaryKey = new DataColumn[] { ds.Tables["student"].Columns["id"] };
            SqlCommandBuilder sqlCommandBuilder1 = new SqlCommandBuilder(da);
            ds.Tables["student"].Rows[i].Delete();
            da.Update(ds, "student");
            ds.AcceptChanges();
            MessageBox.Show("数据删除成功");
            i = 0;
            show(i);        }        private void button5_Click(object sender, EventArgs e)
        {
            if (i != 0)
            {
                i--;
                show(i);
            }
            else
                MessageBox.Show("已经是第一条记录");        }        private void button6_Click(object sender, EventArgs e)
        {
            if (i < ds.Tables["student"].Rows.Count - 1)
            {
                i++;
                show(i);
            }
            else
                MessageBox.Show("已经是最后一条记录");
        }        private void button7_Click(object sender, EventArgs e)
        {
            i = ds.Tables["student"].Rows.Count - 1;
            show(i);
        }        private void pictureBox1_Click(object sender, EventArgs e)
        {
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {                this.textBox5.Text = openFileDialog1.FileName;
                pictureBox1.Image =Image.FromFile(openFileDialog1.FileName);            } //打开路径,必不可少的     这些都是是在button单击事件里完成的            if (openFileDialog1.FileName.Length > 0) // 判断openFileDialog1路径的长度
            {                string oldName = openFileDialog1.FileName;            //定义一个string类型的变量 用于存放【文件路径】                string[] splitName = oldName.Split('.');                     //为获取文件的扩展名做准备的                string ext = splitName[splitName.Length - 1];          //文件的扩展名                if (ext == "jpg" || ext == "gif" || ext == "bmp" || ext == "JPG")   //限制上传图片的格式
                {                    string dbName = DateTime.Now.ToString("yyyyMMddhhmmss") + "." + ext; //给上传的图片起个名字!以时间命名                    string newName = AppDomain.CurrentDomain.BaseDirectory + dbName;  //新路径!这是个相对路径                    File.Copy(oldName, newName, true);  //把文件从以前的路径复制到新的路径中去                }            }                                 
        }
    }
}
初学写代比较乱.大家指点下