本文介绍了如何在SQL和C#中只插入一个ID的五个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我想为每个学生添加五个主题标记...我将如何限制Stduent_ID?如果Stduent_ID = 1并且插入五次然后六个条目我不应该添加...我将如何在C#中执行此操作+ SQL ... 我的尝试: 声明@Counter int 设置@ Counter =(SELECT Count(Student_ID)为'StudentCount' 来自CourseSemOne 其中Student_ID = 3 Group by Student_ID ) if(@Counter> = 6) print'Sorry!你不能为一个stduent添加超过五个主题数据' 其他 打印'插入查询'解决方案 minor correction if ( @ Counter > = 5 ) string sql = SELECT Count(Student_ID)FROM CourseSemOne,其中Student_ID ='" + Student_ID +"'Group by Student_ID" ;; SqlConnection con = new SqlConnection(@Data Source = LOCALHOST \ SQLEXPRESS;初始目录= CS_DB;集成安全性=真); con.Open(); SqlCommand cmd = new SqlCommand(sql,con); int count = Convert.ToInt32(cmd.ExecuteScalar()); con.Close(); if (计数< 5&& count> = 0) { obj.insertSemCourseOne(Student_ID,Course_Code,Course_Title,Total_Marks,Obtain_Marks,Grade,Value,Cr_Hours,Grade_Point,GPA,CGPA); // DatabaseConnnectionClass.UserMessage(" Added"); MessageBox.Show(" Added Subject"); Dataloaddd(); } 其他 { MessageBox.Show(&抱歉!对于这个学号=" + Student_ID +"您不能添加超过5个数据。); } i want to add only five subject marks for each student...how i will restrict an Stduent_ID ?if Stduent_ID=1 and inserted five times then for six entry i should not add...how i will do it in C# + SQL...What I have tried:Declare @Counter int Set @Counter=(SELECT Count(Student_ID) as 'StudentCount'FROM CourseSemOne where Student_ID=3 Group by Student_ID )if(@Counter >= 6)print'Sorry! You cannot add more than five subject data for a single stduent'elseprint 'Insert Query' 解决方案 minor correctionif(@Counter >= 5)string sql = "SELECT Count(Student_ID) FROM CourseSemOne where Student_ID='"+Student_ID+"' Group by Student_ID "; SqlConnection con = new SqlConnection(@"Data Source=LOCALHOST\SQLEXPRESS;Initial Catalog=CS_DB;Integrated Security=True"); con.Open(); SqlCommand cmd = new SqlCommand(sql, con); int count = Convert.ToInt32(cmd.ExecuteScalar()); con.Close(); if (count <5 && count >=0) { obj.insertSemCourseOne(Student_ID, Course_Code, Course_Title, Total_Marks, Obtain_Marks, Grade, Value, Cr_Hours, Grade_Point, GPA, CGPA); // DatabaseConnnectionClass.UserMessage("Added"); MessageBox.Show("Added Subject"); Dataloaddd(); } else { MessageBox.Show("Sorry!For this Student ID = "+Student_ID+" You cannot add more than 5 data."); } 这篇关于如何在SQL和C#中只插入一个ID的五个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-25 07:52