想要显示最后插入的id,我是ASP.NET新手请分享代码,如果有人有解决方案。 [英] Want to show last inserted id , I am new to ASP.NET please share code if any one have solution.

查看:91
本文介绍了想要显示最后插入的id,我是ASP.NET新手请分享代码,如果有人有解决方案。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想要显示最后插入的ID,我是asp.net的新用户请分享代码,如果有人有解决方案。



我尝试过:



want to show last inserted id , I am new to asp.net please share code if any one have solution.

What I have tried:

public void AddWorkSheet() {

    if (!DropDownList1.SelectedItem.Text.Equals("--Select Project Name--") && !DropDownList2.SelectedItem.Text.Equals("--Select Department Name--"))
    {

        da = new MySqlDataAdapter("Insert into tbl_dailyworksheet (projectname,departmentname) values('" + DropDownList1.SelectedItem.Text + "','" + DropDownList2.SelectedItem.Text + "') select last_insert_id() ", con);

            da.Fill(ds, "id");
            if (ds.Tables["id"].Rows.Count > 0)
            {
                wsid = ds.Tables["id"].Rows[0][0].ToString();
            }

            Response.Write("alert('Event Added')");

    }
    else
    {
        Response.Write("alert('Select Project and department properly')");
    }
}

推荐答案

首先,不要这样做!永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。总是使用参数化查询。



连接字符串时会导致问题,因为SQL会收到如下命令:

First off, don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'

就SQL而言,用户添加的引号会终止字符串,并且您会遇到问题。但情况可能更糟。如果我来并改为输入:x'; DROP TABLE MyTable; - 然后SQL收到一个非常不同的命令:

The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:

SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'

哪个SQL看作三个单独的命令:

Which SQL sees as three separate commands:

SELECT * FROM MyTable WHERE StreetAddress = 'x';

完全有效的SELECT

A perfectly valid SELECT

DROP TABLE MyTable;

完全有效的删除表格通讯和

A perfectly valid "delete the table" command

--'

其他一切都是评论。

所以它确实:选择任何匹配的行,从数据库中删除表,并忽略其他任何内容。



所以总是使用参数化查询!或者准备好经常从备份中恢复数据库。你定期做备份,不是吗?



其次,在两个命令之间加一个分号。



第三,使用ExecuteScalar而不是在一个值上浪费DataAdapter / DataTable。



四,重新设计你的数据库:你有很多冗余,因为你是多次存储项目和部门名称。为Projects和Departments设置单独的表,并在日常工作表中使用外键 - 您可以在需要时通过JOIN获取名称。

And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

Second, add a semicolon between the two commands.

Third, use ExecuteScalar instead of wasting a DataAdapter / DataTable on a single value.

Fourth, redesign your DB: you have massive redundancy in there since you are storing the project and department names multiple times. Set up separate tables for Projects and Departments, and use a Foreign Key in your daily worksheet table - you can fetch the names when you need them via a JOIN.


IDENT_CURRENT(Transact-SQL)| Microsoft Docs [ ^ ]


如果你有一个主键,那么它变得非常简单......



string q =select * from Table_Name where user_name ='Ali',ID =(从Table_Name中选择MAX(ID));



如果ID为Primary&AutoIncrement
if you have a primary key then it's become very simple...

string q="select * from Table_Name where user_name='Ali' and ID=(Select MAX(ID) from Table_Name)";

this will return you a last you if the "ID" is Primary And AutoIncrement


这篇关于想要显示最后插入的id,我是ASP.NET新手请分享代码,如果有人有解决方案。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆