当前位置:编程学习 > C#/ASP.NET >>

多表插入问题 大神来找茬

 string course = txtCourse.Text.Trim();
         string classes = txtClass.Text.Trim();
         string teacher = txtTName.Text.Trim();
         string period = txtPeriod.Text.Trim();
         string pass = "111";
         string kind = "教师";
         SqlConnection sqlConn = GetConnection();
         sqlConn.Open();
         string sql = "insert into Teacher values('" + course + "','" + classes + "','" + teacher + "'','" + period + "')";
         string sql1 = "insert into Users(UserName,Password,Purview)values('" + teacher + "'','" + pass + "'','" + kind + "')";
         if (txtCourse.Text.Trim() == "")
         {
             Response.Write("<script>alert('课程名不能为空!!')</script>");
             txtCourse.Focus();
         }
         else if (txtClass.Text.Trim() == "")
         {
             Response.Write("<script>alert('班级名不能为空!')</script>");
             txtClass.Focus();
         }
         else if (!IsCourseNotExisted(txtCourse.Text.Trim()))
         {
             Response.Write("<script>alert('该班级已有该课程!')</script>");
             txtCourse.Text = "";
             txtClass.Text = "";
             txtTName.Text = "";
             txtPeriod.Text = "";
             txtCourse.Focus();
         }
 
        else
         {
             SqlCommand sqlCom = new SqlCommand(sql, sqlConn);
             SqlCommand sqlCom1 = new SqlCommand(sql1, sqlConn);
             sqlCom.ExecuteNonQuery();
             sqlCom1.ExecuteNonQuery();
             Response.Write("<script>alert('数据插入成功!')</script>");
             MultiView1.SetActiveView(View2);   
             FillT();
         }

鉴于之前发的某贴,有人回复说代码太多,我这个lazy  person  就随便从中捡了一段  不完善啊   

各位大神,帮忙看看问题啊!!

头痛的很!! asp.net   多表插入 --------------------编程问答-------------------- 你又没说你有什么错。
 string sql = "insert into Teacher values('" + course + "','" + classes + "','" + teacher + "'','" + period + "')";

Teacher表的字段是否就是这四个,如果不是的话,需要指定哪些列(还有顺序)。 --------------------编程问答--------------------
 SqlCommand sqlCom = new SqlCommand(sql, sqlConn);
 SqlCommand sqlCom1 = new SqlCommand(sql1, sqlConn);

直接搞成一个sql 
SqlCommand sqlCom = new SqlCommand(sql+sql1, sqlConn); --------------------编程问答-------------------- 我的问题就是在teachers表那里   只有那四列   但是Users表里面是有UserId的   如果我设置为自动编号   那么Students表又不能这样插入到Users表里面  因为Students表里面是有学号的 --------------------编程问答--------------------
引用 3 楼 u012019910 的回复:
我的问题就是在teachers表那里   只有那四列   但是Users表里面是有UserId的   如果我设置为自动编号   那么Students表又不能这样插入到Users表里面  因为Students表里面是有学号的


给Users表加一列不就行了嘛  --------------------编程问答-------------------- 描述的真乱,有必要写代码吗,这完全是sql表之前的问题; 用文字讲清楚就行了
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,