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;namespace Student
{
    public partial class RegUpd : Form
    {
        public RegUpd()
        {
            InitializeComponent();
            //this.RegUpd_Load();
        }        private SqlConnection NewStuConn;
        private SqlCommand NewStuComd;
        private String NewStuStrConn="Data Source=WWW-F9EEA54BDCA;Initial Catalog=STUDENT;User ID=sa;Pwd=jxlsqd;Integrated Security=False";
        private string NewStuStrComd;
        private SqlDataAdapter NewStusqlda;        private void RegUpd_Load(object sender, EventArgs e)
        {
            try
            {
                DataSet QryStudClassset = new DataSet();                string NewStuStrConnSql = "select NAME from CLASS";
                this.NewStuConn = new SqlConnection(this.NewStuStrConn);
                NewStuConn.Open();
                this.NewStusqlda = new SqlDataAdapter(NewStuStrConnSql, this.NewStuConn);
                QryStudClassset.Clear();
                this.NewStusqlda.Fill(QryStudClassset, "Class");
                for (int i = 0; i < QryStudClassset.Tables[0].Rows.Count; i++)
                {
                    Class.Items.Add(QryStudClassset.Tables[0].Rows[i][0]);
                }                Class.SelectedIndex = 0;
            }            catch
            {
                MessageBox.Show("连接打开错误");
            }            }        private void button6_Click(object sender, EventArgs e)
        {
            //try
            //{                string date = this.birthday.Value.Date.ToString();
                if (this.nam.Text == "")
                {
                    MessageBox.Show("学生姓名必须填写", "提示");
                }                else
                {
                    DataSet NewStudClassset = new DataSet();
                    string NewStuStrConnSql = "select ID from CLASS where NAME =" + " '" + this.Class.SelectedItem.ToString().Trim() + "'";
                    this.NewStuConn = new SqlConnection(this.NewStuStrConn);
                    this.NewStusqlda = new SqlDataAdapter(NewStuStrConnSql, this.NewStuConn);
                    NewStudClassset.Clear();
                    this.NewStusqlda.Fill(NewStudClassset, "Class");
                    string ClassId = NewStudClassset.Tables[0].Rows[0][0].ToString().Trim();
                    DataSet NewStu = new DataSet();
                    string ExStu = "select * from STUDENT where NAME =" + " '" + this.nam.Text.ToString().Trim() + "'" ;
                    this.NewStuConn = new SqlConnection(this.NewStuStrConn);
                    this.NewStusqlda = new SqlDataAdapter(ExStu, this.NewStuConn);
                    NewStu.Clear();
                    this.NewStusqlda.Fill(NewStu, "STUDENT");                    if(RegUpd.Tables[0].Rows.Count == 0)
                    NewStuStrComd = "insert into STUDENT(NAME,SEX,BIRTHDAY,BIRTHPLACE,CLASS_NAME,ADDRESS,TELEPHONE,DEPARTMENT)values" +
                       "(" + " '" + this.nam.Text.ToString().Trim() + " '" + "," + " '" + this.sex.Text.ToString().Trim() + " '" + "," + " '" + date + " '" + "," + " '" + this.birthplace.Text.ToString().Trim
                      () + " '" + "," + " '" + ClassId + " '" + "," + "'" + this.address.Text.ToString().Trim() + " '" + ", " +
                      " '" + this.telephone.Text.ToString().Trim() + " '" + "," + " '" + this.department.Text.ToString().Trim() + " '" + ")";                    NewStuConn = new SqlConnection(NewStuStrConn);
                    NewStuConn.Open();
                    NewStuComd = new SqlCommand(NewStuStrComd, NewStuConn);
                    int Succnum = NewStuComd.ExecuteNonQuery();
                    if (Succnum > 0) MessageBox.Show("录入成功");
                    else
                    {
                        MessageBox.Show("录入失败");
                    }
                }
            //}
            //catch (Exception Ex)
            //{
            //    MessageBox.Show("已经录入或者类型不匹配", "错误", MessageBoxButtons.OKCancel);
            //}        }
             if(RegUpd.Tables[0].Rows.Count == 0) 把这句注释掉可以运行 也可添加数据 但实现不了判断同名的功能  本人SQL新手  那位帮我看哈在那出错了? 谢谢   
          

解决方案 »

  1.   

     if(RegUpd.Tables[0].Rows.Count == 0) 
    大体看了一眼
    貌似 RegUpd是类的名字吧.....
    改称 NewStu.Tables[0].Rows.Count == 0
      

  2.   

    老兄你这又是什么意思呢? 刚一楼说的对 是我写错了 现在该过来了 但在添加数据的时候又报错了 xecuteNonQuery: CommandText 属性尚未初始化   再帮我看看是那点有错了?