.







 
English Deutsch Türkçe
Dili: ASP
 6  toplam sonuçtan  5. Gösterge: 


 
Get New Record ID Get New Record ID Dili:  ASP 
Sometimes it is necessary to get the ID number of a new inserted record immediately. Often this is performed with a second query. This script insert a new value into a table and gets the new ID (Autonumber) of that new inserted record immediately.

Get New Record ID
     [benzer sayfalar] 

 <%@LANGUAGE="VBScript" %>
<%
Option Explicit
Private Const adUseServer = 3
Private Const adOpenDynamic = 2
Private Const adLockOptimistic = 3
Private Const adCmdText = &H0001
Dim objConn
Dim xHTML
Private Function InsertSQL()
     On Error Resume Next
     Dim strSQL
     Dim objRset
     Dim Item
     Dim ColumnName
     Dim ColName
     Dim sValue
     Dim outHTML
     Dim NewestIDValue
     Set objRset = Server.CreateObject("ADODB.Recordset")
     strSQL = "SELECT * FROM DB_CCProviders"
     With objRset
          .CursorLocation = adUseServer
          .Open (strSQL), objConn, adOpenDynamic, adLockOptimistic, adCmdText
          .AddNew
          .Fields("DB_Provider_Name").Value = CStr(Request.Form("DB_Provider_Name"))
          .Fields("DB_Provider_URL").Value = CStr(Request.Form("DB_Provider_URL"))
          outHTML = outHTML & "<TR>" & vbCrLf
          outHTML = outHTML & "<TD BGCOLOR=""#FCFCFC"" WIDTH=""25%"" NOWRAP>DB_Provider_Name</TD>" & vbCrLf
          outHTML = outHTML & "<TD BGCOLOR=""#FCFCFC"" WIDTH=""75%"" NOWRAP>" & .Fields("DB_Provider_Name").Value & "</TD></TR>" & vbCrLf
          outHTML = outHTML & "<TR>" & vbCrLf
          outHTML = outHTML & "<TD BGCOLOR=""#FCFCFC"" WIDTH=""25%"" NOWRAP>DB_Provider_URL</TD>" & vbCrLf
          outHTML = outHTML & "<TD BGCOLOR=""#FCFCFC"" WIDTH=""75%"" NOWRAP>" & .Fields("DB_Provider_URL").Value & "</TD></TR>" & vbCrLf
          .Update
          '' gets the new ID of the new record
          NewestIDValue = .Fields("DB_CCProvidersID").Value
          .Close
     End With
     Set objRset = Nothing
     InsertSQL = InsertSQL & "<TABLE WIDTH=""100%"" CELLSPACING=""1"" CELLPADDING=""2"" BGCOLOR=""#DFDFDF"" ALIGN=""center"" BORDER=""0"">" & vbCrLf
     InsertSQL = InsertSQL & "<TR>" & vbCrLf
     InsertSQL = InsertSQL & "<TD COLSPAN=""2"" WIDTH=""100%"" ALIGN=""center""><B>"
     If Err <> 0 Then
          InsertSQL = InsertSQL & Err.Description
     Else
          InsertSQL = InsertSQL & "Records inserted successfully."
     End If
     InsertSQL = InsertSQL & "</B></TD></TR>"
     InsertSQL = InsertSQL & outHTML & "</TABLE>" & vbCrLf
     InsertSQL = InsertSQL & "<BR>" & vbCrLf
     InsertSQL = InsertSQL & "<BR>" & vbCrLf
     InsertSQL = InsertSQL & "<FONT SIZE=""+1"">ID of this new record: <B>" & NewestIDValue & "</B></FONT>" & vbCrLf
End Function
Public Sub RSInsert()
     Set objConn = Server.CreateObject("ADODB.Connection")
     objConn.Open Application("DBPath")
     xHTML = InsertSQL()
     objConn.Close
     Set objConn = Nothing
     Response.Write (xHTML)
End Sub
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Yurtseven.com »» Insert Records Module</TITLE>
<STYLE TYPE="text/css">
BODY, TD, FORM, INPUT, TEXTAREA {
font-family : Verdana, MS Sans Serif, Arial;
font-size : 8pt;
}
TR {
background-color : #E0E9EF;
}
</STYLE>
</HEAD>
<BODY>
<%
Application.Lock

'' ************* For Access 2000 **************
Application("DBPath") = "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" & Server.MapPath("Database\DB_SQL_Shop.mdb") & ";"
'' ********************************************

'' ************* For SQL Server ***************
''Application("DBPath") = "DRIVER=SQL Server; SERVER=localhost; DATABASE=DB_SQL_Shop; PORT=1433; UID=sa; PASSWORD=admin;"
'' ********************************************

Application.Unlock

If Len(Request.Form("submit")) <> 0 Then
     Call RSInsert()
End If
%>
<BR>
<BR>This code inserts a new value into a table and gets the ID (Autonumber) of that record in one step.
<FORM ACTION="GetNewID.asp" METHOD="post">
<TABLE WIDTH="100%" CELLSPACING="1" CELLPADDING="0" BGCOLOR="#DFDFDF" ALIGN="center" BORDER="0">
<TR>
<TD BGCOLOR="#FCFCFC" WIDTH="25%" HEIGHT="20" NOWRAP><FONT SIZE="2"><B>Field Names</B></FONT></TD>
<TD BGCOLOR="#FCFCFC" WIDTH="75%" NOWRAP><FONT SIZE="2"><B>Values to insert</B></FONT></TD></TR>
<TR>
<TD BGCOLOR="#FCFCFC" WIDTH="25%" NOWRAP>DB_Provider_Name</TD>
<TD BGCOLOR="#FCFCFC" WIDTH="75%" NOWRAP><INPUT TYPE="text" NAME="DB_Provider_Name" SIZE="50" VALUE=""></TD></TR>
<TR>
<TD BGCOLOR="#FCFCFC" WIDTH="25%" NOWRAP>DB_Provider_URL</TD>
<TD BGCOLOR="#FCFCFC" WIDTH="75%" NOWRAP><INPUT TYPE="text" NAME="DB_Provider_URL" SIZE="50" VALUE=""></TD></TR>
<TR>
<TD BGCOLOR="#FCFCFC" WIDTH="25%" NOWRAP> </TD>
<TD BGCOLOR="#FCFCFC" WIDTH="75%" NOWRAP><INPUT TYPE="submit" NAME="submit" VALUE="Submit"></TD></TR>
</TABLE>
</FORM>
</BODY>
</HTML>





 6  toplam sonuçtan  5. Gösterge: 


« Önceki  1  2  3  4  5  6  Sonraki »
Üye Girişi  |  İletişim