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新手 那位帮我看哈在那出错了? 谢谢
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新手 那位帮我看哈在那出错了? 谢谢
解决方案 »
- C# winform Panel背景色问题!
- 关于 IEnumerable.Cast
- GridView动态添加一列CheckBox
- 请教一个CurrencyManager 的问题
- EXTJS TREE 单击事件 救命!!!!!!!
- 正则表达式高手请进!
- 关于线程............在线等,解决即给分
- 在asp.net里如何在服务器端得到当前运行页的名称及后面跟的参数?
- 下载的aspx源码只有dll和aspx文件,如何编译修改
- 如何取出gridview中绑定的Label的值(Label是单个字母)
- C# 中使用反射生成新对象出现了问题!急
- 用.net2.0中oledb访问access数据是否需要安装MDAC2.7版本以上?
大体看了一眼
貌似 RegUpd是类的名字吧.....
改称 NewStu.Tables[0].Rows.Count == 0