从insert-C#和MySQL获取会话

从insert-C#和MySQL获取会话,c#,mysql,visual-studio,session,sql-insert,C#,Mysql,Visual Studio,Session,Sql Insert,我以前从来没有这样做过,所以我希望你们中的一些人会知道怎么做 基本上,我在CreateModule页面上进行了一次插入,然后我想获取新的ModuleID(它是在数据库中创建的,我还没有插入)和ModuleTitle,并将其带到CreateModule2页面 我真的很感激所有的帮助 C# protectedvoid CreateNewModule\u单击(对象发送方,事件参数e) { //打开新连接 SqlConnection connect1=新的SqlConnection(Configura

我以前从来没有这样做过,所以我希望你们中的一些人会知道怎么做

基本上,我在CreateModule页面上进行了一次插入,然后我想获取新的ModuleID(它是在数据库中创建的,我还没有插入)和ModuleTitle,并将其带到CreateModule2页面

我真的很感激所有的帮助

C#

protectedvoid CreateNewModule\u单击(对象发送方,事件参数e)
{
//打开新连接
SqlConnection connect1=新的SqlConnection(ConfigurationManager.ConnectionString[“DefaultConnection”].ConnectionString);
connect1.Open();
//初始化变量以进行更新
字符串标题=ModuleTitleText.Text;
字符串Mtext=ModuleTextText.Text;
字符串Com=CompulsoryDropdown.Text;
字符串CAT=CATpointsText.Text;
String Lev=LevelText.Text;
字符串Ass=AssessmentText.Text;
字符串MCode=modulecodext.Text;
字符串状态=ModuleStatusDropdown.Text;
//将字符串转换为Int
Int32 Levconverted=转换为Int32(Lev);
Int32 CATconverted=转换为Int32(CAT);
//插入查询以将新的学生记录添加到数据库中的学生记录表中
String queryInsert=“插入模块信息(模块标题、模块文本、强制、CATpoints、级别、评估、模块代码、模块状态)值(“+Title+”、“+Mtext+”、“+Com+”、“+CAT+”、“+Lev+”、“+Ass+”、“+MCode+”、“+Status+”);选择最后一次插入ID();
//执行插入查询
SqlCommand myCommand=newsqlcommand(queryInsert,connect1);
myCommand.Parameters.Add(“@title”,SqlDbType.NVarChar).Value=title;
int idmodule=Convert.ToInt32(myCommand.ExecuteScalar());
//成功上载的警报
回答。写(“”);
回答。写(“警报('已添加新模块。请在下一页中选择一个课程以使模块对齐');”;
Response.Write(“document.location.href='CreateModule2.aspx';”;
回答。写(“”);

}
您的代码很难看,有缺陷,需要SQL注入。只要祈祷没有人输入像“DROP TABLE”Module_Info这样的文本即可;在ModuleTitleText中。。 也就是说,将查询更改为:

String queryInsert = "INSERT INTO Module_Info (ModuleTitle, ModuleText, Compulsory, CATpoints, Level, Assessment, ModuleCode, ModuleStatus) VALUES ('" + Title + "', '" + Mtext + "', '" + Com + "', '" + CAT + "', '" + Lev + "', '" + Ass + "',  '" + MCode + "', '" + Status + "'); SELECT  LAST_INSERT_ID()"
然后使用:

int result = (int) myCommand.ExecuteScalar();
假设您的id列设置为自动增量

编辑。
在SQL Server中,首先将其更改为SCOPE_IDENTITY()

,您应该知道字符串串联在数据库代码中是一种非常糟糕的做法。它会导致错误和解析问题。对于所有这些原因,更好的做法是使用参数化查询,然后返回表中最后插入的标识,修复非常简单

String queryInsert = @"INSERT INTO Module_Info 
        (ModuleTitle, ModuleText, Compulsory, CATpoints, Level, 
         Assessment, ModuleCode, ModuleStatus) VALUES 
        (@title, @mtext, @Com, @cat, @lev, @Ass, @MCode, @Status);
        SELECT SCOPE_IDENTITY()";

SqlCommand myCommand = new SqlCommand(queryInsert, connect1);
myCommand.Parameters.Add("@title", SqlDbType.NVarChar).Value = Title;
.... ...
// and so on with all other parameters required
// paying attention to use the appropriate SqlDbType for the 
// field updated by the parameter value...

// Don't run ExecuteNonQuery, but ExecuteScalar to get the last
// value returned by SCOPE_IDENTITY()
int idmodule = Convert.ToInt32(myCommand.ExecuteScalar());
正如您所看到的,查询包含两条指令,最后一条指令返回插入到module_info表中的ID,您可以使用ExecuteScalar获得它

正在等待MySql与Sql Server问题的澄清。除了MySql,我将向您展示相同的代码

String queryInsert = @"INSERT INTO Module_Info 
        (ModuleTitle, ModuleText, Compulsory, CATpoints, Level, 
         Assessment, ModuleCode, ModuleStatus) VALUES 
        (@title, @mtext, @Com, @cat, @lev, @Ass, @MCode, @Status);
        SELECT last_insert_id()";

MySqlCommand myCommand = new MySqlCommand(queryInsert, connect1);
myCommand.Parameters.Add("@title", MySqlDbType.VarChar).Value = Title;
注意last_insert_id函数的用法,以及MySql而不是Sql Server类的用法

关于将其传递到您的呼叫页面。通常的方法是将结果放入查询字符串中,并在查看QueryString集合的被调用页面中获取它

    Response.Write("<script type='text/javascript'>");
    Response.Write("alert('New Module has been added. Please select a course to align the module to in the next page.');");
    Response.Write("document.location.href='CreateModule2.aspx?ModuleID=" + idmodule + "&Title=" + Title + "';");
    Response.Write("</script>");
Response.Write(“”);
回答。写(“警报('已添加新模块。请在下一页中选择一个课程以使模块对齐');”;
Response.Write(“document.location.href='CreateModule2.aspx?ModuleID=“+idmodule+”&Title=“+Title+”;”);
回答。写(“”);

您需要将其附加到SQL字符串的末尾:

SELECT SCOPE_IDENTITY()
这将返回新创建记录的ID。然后,您可以通过将myCommand.ExecuteQuery()替换为以下内容来获得该值:


然后可以使用响应。使用id重定向到下一页,并使用该id加载新创建的记录中的任何内容。

SCOPE\u IDENTITY()是特定于SQL Server的,他要求使用MySqldatabase@Oscar标记是mysql,但他使用SqlCommand。这里需要澄清我将在问题中编辑我的代码,你可以告诉我我是否做得正确Lyno@Marty不要更改原始问题,否则我们将失去对情况的跟踪。尝试自己的代码,如果您有其他问题,请再次询问或发布新代码question@Marty您的更新没有添加所需的所有参数(每个值一个),请参见上面第一段中的示例和注释。您是使用MySql作为数据库还是Sql Server?代码为SQLServer使用类,但您的标记显示MySql。要检索最后一个标识值,答案将是Differentics Hanks Oscar,这是一个新的东西,所以以这种方式显示。你能解释一下int结果=(int)myCommand.ExecuteScalar()吗;一点在我的颂歌里我把这个放在哪里?我如何在下一页中得到它?是不是像“”
int id = myCommand.ExecuteScalar();