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.OleDb;
using System.Reflection;
using System.Xml;
using System.Data.SqlClient;namespace charushuju1
{
    public partial class Form1 : Form
    {
        string connsqlserver = "Data Source=localhost;Initial Catalog=Lee;Integrated Security=True";
        public Form1()
        {
            InitializeComponent();
        }        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (TextBox1.Text == "" || TextBox2.Text == "" || TextBox3.Text == "" || TextBox4.Text == "" || TextBox5.Text == "" || TextBox6.Text == "")
                {
                    MessageBox.Show("请输入完整的联系人信息", "提示信息",
                        System.Windows.Forms.MessageBoxButtons.OK,
                        System.Windows.Forms.MessageBoxIcon.Warning);
                }
                else
                {
                    DataSet ds = new DataSet();
                    string strSqls;
                    strSqls = string.Format("SELECT * FROM ziliao WHERE ID='" + TextBox1.Text.Trim() + "'");
                    SqlConnection con = new SqlConnection(consqlserver);
                    SqlDataAdpter da = new SqlDataAdpaer(strSqls, con);
                    // SqlConnection cons = new SqlConnection(consqlserver);
                    // SqlCommand cmd = new SqlCommand(strSqls, cons);                    da.Fill(ds);
                    if (ds.Tables[0].Rows.Count > 1)
                    {
                        MessageBox.Show("该人ID已经存在,请您更换此人ID", "提示信息",
                        System.Windows.Forms.MessageBoxButtons.OK,
                        System.Windows.Forms.MessageBoxIcon.Error);                    }
                    else
                    {
                        if (MessageBox.Show("确认要保存该人信息吗?", "提示信息",
                        System.Windows.Forms.MessageBoxButtons.YesNo,
                        System.Windows.Forms.MessageBoxIcon.Question) ==
                        System.Windows.Forms.DialogResult.Yes)
                        {
                            strSqls = string.Format(@"INSERT INTO Lee (ID,Name,Age,Sex,xuexiao,zhuanye) VALUES('{0}',{1},{2},{3},{4},{5})", TextBox1.Text.Trim(),
                                TextBox2.Text.Trim(), TextBox3.Text.Trim(), TextBox4.Text.Trim(), TextBox5.Text.Trim(), TextBox6.Text.Trim());
                            SqlConnection cons = new SqlConnection(consqlserver);
                            SqlCommand cmd = new SqlCommand(strSqls, cons);
                            try
                            {
                                cons.Open();           //打开连接
                                cmd.ExecuteNonQuery();   //执行SQL语句
                            }
                            catch { }
                            finally
                            {
                                cons.Close();         //关闭连接
                                cons.Dispose();       //释放连接
                                cmd.Dispose();
                            }
                            MessageBox.Show("信息保存成功", "提示信息",
                            System.Windows.Forms.MessageBoxButtons.OK,
                            System.Windows.Forms.MessageBoxIcon.Information);
                            string sqls = "select * from Lee  ORDER BY ID";
                            SqlDataAdapter = new SqlDataAdapter(sqls, con);
                            da1.Fill(ds);
                            if (ds.Tables[0].Rows.Count > 1)
                            {
                                dataGridView1.DataSource = ds.Tables[0];
                            }                        }                    }
                }            }
            catch { }
        }        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }        private void Form1_Load(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            //定义连接数据源
            string sql = "select * from ziliao";
            //定义SQL Server连接对象
            SqlConnection con = new SqlConnection(consqlserver);
            SqlDataAdapter da = new SqlDataAdapter(sql, con);            try
            {
                da.Fill(ds);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    //将获得数据源赋予数据库控件
                    dataGridView1.DataSource = ds.Tables[0];
                }            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
            finally
            {
                con.Close();
                con.Dispose();
                da.Dispose();
            }
        }
    }
}
提示有一个错误:错误 1 “charushuju.Form1.Dispose(bool)”: 没有找到适合的方法来重写 E:\vs2008例子\charushuju1\charushuju1\Form1.Designer.cs 14 33 charushuju1