简单程序,插入、查找都无错、无果,何故?给能给远程协助?我QQ:173909445

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.SqlClient;
    using System.Data;namespace WindowsFormsApplication1
    {
        class SqlClass
        {
            public static readonly string connectionString = "server=.\\ZGC-20120911JZX;database=master;uid=sa;pwd=123";
            //得到一个数据连接对象
            private static SqlConnection GetConnection()
            {
                return new SqlConnection(connectionString);
            }
            //得到一个数据表,这个表会绑定到DateGridView控件上
            public static DataTable GetTable(string empDept)
            {
                DataTable dt = new DataTable("dtPerson");
                try
                {
                    using (SqlConnection cn = GetConnection())
                    {
                        cn.Open();
                        //构造sql,使用了连接查询,这样就可以使DataGridView中显示多个表的数据了
                        //另外sql还使用了like关键字来实现模糊查询
                        string sql = "select 姓名 =psnName,性别=psnSex,年龄=psnAge,地址=psnAddress,职务=empDuty,部门=empDept from Person join Employee on Person.psnNo=Employee.psnNo where empDept like '%" + empDept + "%' ";
                        SqlDataAdapter dp = new SqlDataAdapter(sql, cn);
                        dp.Fill(dt);
                        cn.Close();                }
                }
                catch (SqlException e)
                {
                    Console.WriteLine(e.Message);
                }
                return dt;
            }
            //将Person表的信息插入到数据库中,注意Person类,如果不这样用你将写很多参数
            public static void savePerson(Person per)
            {
                string sql = string.Format("insert into Person values('{0}','{1}','{2}','{3}','{4}')",per.No,per.Name,per.Sex,per.Age,per.Address);
                using (SqlConnection cn=GetConnection ())
                {
                    cn.Open();
                    SqlCommand cmd =new SqlCommand(sql,cn);
                    cmd.ExecuteNonQuery();
                }
            }
            //将Employee表的信息插入到数据库中,注意Employee类,如果不这样用你将写很多参数
            public static void SaveEmployee(Employee emp)
            {
                 string sql = string.Format("insert into employee values('{0}','{1}','{2}','{3}','{4}')",emp.No,emp.Duty ,emp.Dept ,emp.InDate ,emp.OutDate );
                using (SqlConnection cn=GetConnection ())
                {
                    cn.Open();
                    SqlCommand cmd =new SqlCommand(sql,cn);
                    cmd.ExecuteNonQuery();
                }
            }    }
    }
      

  2.   

    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;namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void btnSave_Click(object sender, EventArgs e)
            {
                //构造人员实体对象,注意Person类
                Person per = new Person();
                per.No = this.txtNo.Text;
                per.Name = this.txtName.Text;
                per.Sex = this.cbSex.SelectedItem.ToString();
                per.Age = Convert.ToInt32(this.txtAge.Text);
                per.Address = this.txtAddress.Text;
                //构造员工实体对象,注意Employee类
                Employee emp = new Employee();
                emp.No = this.txtNo.Text;
                emp.Duty = this.txtDuty.Text;
                emp.Dept = this.txtDept.Text;
                emp.InDate = DateTime.Now.ToShortDateString();
                emp.OutDate = outTime.Text;
                //将人员的信息表和员工信息表数据分别保存到数据库
                SqlClass.savePerson(per);
                SqlClass.SaveEmployee(emp);
            }        private void btnFind_Click(object sender, EventArgs e)
            {
                this.dataGridView.DataSource = SqlClass.GetTable(fdTxtDept .Text);
            }
        }
    }
      

  3.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace WindowsFormsApplication1
    {
        class Person
        {
            private string psnNo = string.Empty;
            private string psnName = string.Empty;
            private string psnSex = string.Empty;
            private int psnAge;
            private string psnAddress;
            public string No
            {
                get { return psnNo; }
                set { psnNo = value; }
            }
            public string Name
            {
                get { return psnName; }
                set { psnName = value; }
            }
            public string Sex
            {
                get { return psnSex; }
                set { psnSex = value; }
            }
            public int Age
            {
                get { return psnAge; }
                set { psnAge = value; }
            }
            public string Address
            {
                get { return psnAddress; }
                set { psnAddress = value; }
            }    }
    }