
<% @Import Namespace="System.Data" %>
<% @Import namespace="System.Data.SqlClient" %>
<HTML>
	<HEAD>
		<title>获得插入记录标识号的示例</title>
		<script language=C# runat=server>
		
			void Page_Load(object sender, System.EventArgs e)
			{
				// 数据库连接字符串
				string ConnStr = System.Configuration.ConfigurationSettings.AppSettings["ConnectionSqlServer"];
				// 创建插入SQL语句及调用@@identity函数返回标识值
				string insert_query = "insert into Categories (CategoryName,Description) values ('IT', 'Internet');"
						+ "SELECT @@identity AS 'identity';";

				// 执行数据库操作
				SqlCommand myCommand = new SqlCommand(insert_query, new SqlConnection(ConnStr));
				myCommand.Connection.Open();
				myLabel.Text = myCommand.ExecuteScalar().ToString();
				myCommand.Connection.Close();
			}
		
		</script>
	</HEAD>
	<body>
		<form id="Form1" method="post" runat="server">
			<h3>获得插入记录标识号的示例</h3>
			返回的标识值是：
			<asp:Label id="myLabel" runat="server"></asp:Label>
		</form>
	</body>
</HTML>
