2012年7月25日 星期三

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


'----自己寫的----
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