错误,因为并非所有代码路径返回值

编程入门 行业动态 更新时间:2024-10-27 18:31:41
本文介绍了错误,因为并非所有代码路径返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

你好朋友 以下是我的代码

Hi friends The following is my code

public bool insertstudentsection1(System.Windows.Forms.DataGridView grid,business.Commonpropertis proper) { if (grid.Rows.Count != null) { for (int i = 0; i < grid.Rows.Count - 1; i++) { string storedprocedure = "sp_insertstudentsection"; int admissionid = (int)grid.Rows[i].Cells[0].Value; string StudentName = (string)grid.Rows[i].Cells[1].Value; string Class = (string)grid.Rows[i].Cells[0].Value; char section = (char)grid.Rows[i].Cells[0].Value; SqlParameter[] param = new SqlParameter[6]; param[0] = new SqlParameter("@admissionnumber", admissionid); param[1] = new SqlParameter("@name", StudentName); param[2] = new SqlParameter("@classname", Class); param[3] = new SqlParameter("@sectionname", section); param[4] = new SqlParameter("@datetime", proper.DATETIME); param[5] = new SqlParameter("@adminid", proper.ADMINID); return dbconn.executeInsertStoredProcedure(storedprocedure, param); } } else { return false; } }

此方法显示错误,因为并非所有代码路径返回值.如何解决问题 提前致谢

this method shows an error as not all code path return value. How can solve the problem Advance thanks

推荐答案

您正试图将数据网格中的每一行插入数据库.您不能在每一行之后返回,因此您需要重新考虑执行此方法的方式. 如果您并不真正关心家庭,那么可以成功插入很多东西,您可以像下面这样简单地进行操作: You are trying to insert each row from your datagrid into the database. You can''t return after each row, so you''ll need to rethink the way you are doing this method. If you don''t really care home many successful insertions take place, you could do it simply like this: if(grid.Rows.Count > 0) // (Count will never be null) { bool allInsertsAreOk = true; string storedprocedure = "sp_insertstudentsection"; // Just more efficient. Because it's constant, don't recreat it on every iteration. foreach( var row in grid.Rows ) { int admissionid = (int)row.Cells[0].Value; string StudentName = (string)row.Cells[1].Value; string Class = (string)row.Cells[0].Value; char section = (char)row.Cells[0].Value; SqlParameter[] param = new SqlParameter[6]; param[0] = new SqlParameter("@admissionnumber", admissionid); param[1] = new SqlParameter("@name", StudentName); param[2] = new SqlParameter("@classname", Class); param[3] = new SqlParameter("@sectionname", section); param[4] = new SqlParameter("@datetime", proper.DATETIME); param[5] = new SqlParameter("@adminid", proper.ADMINID); if( !dbconn.executeInsertStoredProcedure(storedprocedure, param) ) { allInsertsAreOk = false; } } return allInsertsAreOk; }

我还建议您不要按原样进行转换.

I would also suggest that you not do your conversions the way you are.

//To convert an integer from string. This won't crash the program if there is a problem with input. int value; if( !Int32.TryParse( row.Cells[0].Value, out value ) ) { // Now if something went wrong in the conversion you can do something about it. }

无法保证代码将进入您的for循环,您需要在for循环之外添加更多的返回值,或者在...的外部添加另一个返回值您的for循环和拳头if语句内. There is no guarantee that the code is going to enter your for loop, you need to more your return outside of the for loop or add another return on the outside of your for loop and inside your fist if statement. public bool insertstudentsection1(System.Windows.Forms.DataGridView grid,business.Commonpropertis proper) { if (grid.Rows.Count != null) { for (int i = 0; i < grid.Rows.Count - 1; i++) { string storedprocedure = "sp_insertstudentsection"; int admissionid = (int)grid.Rows[i].Cells[0].Value; string StudentName = (string)grid.Rows[i].Cells[1].Value; string Class = (string)grid.Rows[i].Cells[0].Value; char section = (char)grid.Rows[i].Cells[0].Value; SqlParameter[] param = new SqlParameter[6]; param[0] = new SqlParameter("@admissionnumber", admissionid); param[1] = new SqlParameter("@name", StudentName); param[2] = new SqlParameter("@classname", Class); param[3] = new SqlParameter("@sectionname", section); param[4] = new SqlParameter("@datetime", proper.DATETIME); param[5] = new SqlParameter("@adminid", proper.ADMINID); //Move this return to where I have my last comment return dbconn.executeInsertStoredProcedure(storedprocedure, param); } //add a return here or more the return out here return false; } else { return false; } }

更多推荐

错误,因为并非所有代码路径返回值

本文发布于:2023-11-10 01:22:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1573976.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路径   返回值   错误   代码

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!