顯示具有 自動編號 標籤的文章。 顯示所有文章
顯示具有 自動編號 標籤的文章。 顯示所有文章

2012年7月25日 星期三

[Asp.Net]SqlDataSource 取得自動編號欄位新增後的值


在Insert SQL command 的後面加上:
; SELECT @New_ID = SCOPE_IDENTITY()

/* 前面有一個「;」分號 */

在SqlDataSource Inserting事件內:
Private Sub SqlDataSource1_Inserting(sender As Object, e As System.Web.UI.WebControls.SqlDataSourceCommandEventArgs) Handles SqlDataSource1.Inserting
   Dim InsertedKey As Data.SqlClient.SqlParameter
   InsertedKey = New Data.SqlClient.SqlParameter("@NewID", Data.SqlDbType.Int)  '@NewID就是取得最新值
   InsertedKey.Direction = Data.ParameterDirection.Output
   e.Command.Parameters.Add(InsertedKey)
End Sub
在SqlDataSource Inserted事件內:
Private Sub SqlDataSource1_Inserted(sender As Object, e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles SqlDataSource1.Inserted

        '取出自動編號欄位新增後的值
        e.Command.Parameters("@New_ID").Value

End Sub

订单编号怎么写?

http://topic.csdn.net/u/20090807/10/823e4d24-4517-4f60-b2a3-41d62f2041a5.html

避免相同資料 重複輸入(重複新增)


'----自己寫的----
Imports System
Imports System.Web.Configuration
Imports System.Data
Imports System.Data.SqlClient
'----自己寫的----
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Conn As SqlConnection = New SqlConnection
        Conn.ConnectionString = WebConfigurationManager.ConnectionStrings("Web.Config檔案裡面 ConnectionString").ConnectionString
        Conn.Open() '---- 連結DB

        Dim dr As SqlDataReader = Nothing
        Dim cmd As SqlCommand
        cmd = New SqlCommand("select * from User資料表 where 帳號 = '" & Trim(TextBox2.Text) "'", Conn)

        dr = cmd.ExecuteReader() '---- 執行SQL指令,取出資料
        '////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        If dr.HasRows() Then

            Response.Write("Error~ 找到相同的帳號,所以程式停止!!")
            Response.End()
        Else '-- 沒有找到相同標題的文章,所以可以新增這筆資料 '-- 請自己撰寫資料新增的程式碼
            Response.Write("新增資料成功!!")
        End If

        If Not (dr Is Nothing) Then
            cmd.Cancel()
            dr.Close()
        End If
        '---- Close the connection when done with it.
        If (Conn.State = ConnectionState.Open) Then
            Conn.Close()
            Conn.Dispose()
        End If
    End Sub

ASP.NET 生成唯一不重复的订单号 支持多用户并发、持多数据库的实现参考(C#.NET通用权限管理系统组件源码组成部分)

http://www.cnblogs.com/jirigala/archive/2011/02/28/1967172.html
http://www.dotblogs.com.tw/joysdw12/archive/2012/06/07/72668.aspx
我们在日常开发项目过程中往往需要各种订单单号的产生方法,而且是支持多用户并发、支持多种数据库的,我们并不想为每个项目都写一些独立的代码去实现这些功能,往往需要有个通用的函数比较爽一些。
  下面我们以C#.NET通用权限管理系统组件源码的做法,给大家来一个参考,下面是序列(流水号)管理器的效果,这里保存着各种需要的当前状态。
 
  这里是代码的具体位置参考 

如何使用SqlDataSource新增時把IDENTITY欄位的PK同時取回


SqlDataSourceInsertQuerySQl Command 之後加上
SELECT @PK_New = @@IDENTITY;
整的SQl Command大約略如下
INSERT INTO YourTableName (Column1, Column1) VALUES (@Value1, @Value2);SELECT@PK_New = @@IDENTITY;
按下重新整理參數可發現多一個參數PK_New
PK_New後按下顯示進階屬性把
Direction 改成 Output
Type 改成 Int32

SqlDataSourceInserted事件中
就可以由e.Command.Parameters["@PK_New"].Value
取得剛才新增那一筆的PK囉~

SqlDataSource: Getting @@Identity after Insert

http://webcache.googleusercontent.com/search?q=cache:Lyn4f73y81kJ:blog.developers.ie/cgreen/archive/2007/08/20/sqldatasource-getting-identity-after-insert.aspx+&cd=1&hl=zh-TW&ct=clnk&gl=tw

2012年4月17日 星期二

C# asp.net 按年月日自动编号函数

//按年月日编号
protected string getnumber()
        {
            string id = "";
            Random ro = new Random(unchecked((int)DateTime.Now.Ticks));
            id = "XQJH" + DateTime.Now.ToString("yyyyMMdd") + ro.Next(0, 999).ToString("000");
            return id;
        }

GridView 加入自動編號欄位

此範例是示範如何在 GridView 加入一個自動編號的欄位,以標示該資料列的編號。
首先在 GridView 第一欄加入一個 TemplateField,並在 TemplateField 的 ItemTemplate 加入一個 Label (ID=lblNo),asxp 對應程式碼如下。
01<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
02    DataKeyNames="Flag,ID" DataSourceID="SqlDataSource1" EmptyDataText="沒有資料錄可顯示。">
03    <Columns>
04        <asp:TemplateField HeaderText="序號">
05            <ItemTemplate>
06                <asp:Label ID="lblNo" runat="server" Text="Label"></asp:Label>
07            </ItemTemplate>
08            <ItemStyle Wrap="True" />
09            <HeaderStyle Wrap="False" />
10        </asp:TemplateField>
11 
12    </Columns>
13</asp:GridView>

然後在 GridView 的 RowDataBound 事件中,設定每一列的 lblNo 的 Text 屬性值為 RowIndex+1。
因為 RowIndex 起始編號為 0 ,故每列的自動編號為 RowIndex+1。
1Protected Sub GridView1_RowDataBound(ByVal sender As ObjectByVal As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
2    Dim oLabel As Label
3    If e.Row.RowType = DataControlRowType.DataRow Then
4        oLabel = CType(e.Row.Cells(0).FindControl("lblNo"), Label)
5        oLabel.Text = (e.Row.RowIndex + 1).ToString()
6    End If
7End Sub

以上的寫法遇到 GridView 分頁時,都是由 1 開始編號,若需分頁需要接續編號,可改用修改如下。
01Protected Sub GridView1_RowDataBound(ByVal sender As ObjectByVal As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
02    Dim oLabel As Label
03    Dim oGridView As GridView
04 
05    If e.Row.RowType = DataControlRowType.DataRow Then
06        oGridView = CType(sender, GridView)
07        oLabel = CType(e.Row.Cells(0).FindControl("lblNo"), Label)
08        oLabel.Text = (oGridView.PageIndex * oGridView.PageSize) + (e.Row.RowIndex + 1).ToString()
09    End If
10End Sub