none
如何檢查手建資料與資料庫現有資料是否重複 RRS feed

  • 問題

  • 大家好以下為VS原始檔內容

    <form id="form1" runat="server">
        <br />
        車輛資料登入<asp:detailsview runat="server" height="50px" width="125px" AutoGenerateInsertButton="True" AutoGenerateRows="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataKeyNames="TN" DataSourceID="SqlDataSource1" DefaultMode="Insert" ForeColor="Black" GridLines="Horizontal">
            <EditRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
            <Fields>
                <asp:BoundField DataField="TN" HeaderText="車格號碼" ReadOnly="True" SortExpression="TN" />
                <asp:BoundField DataField="CN" HeaderText="車號" SortExpression="CN" />
                <asp:BoundField DataField="Name" HeaderText="車主姓名" SortExpression="Name" />
                <asp:BoundField DataField="Tel" HeaderText="車主電話" SortExpression="Tel" />
                <asp:BoundField DataField="GROUP_ID" HeaderText="GROUP_ID" SortExpression="GROUP_ID" />
            </Fields>
            <FooterStyle BackColor="#CCCC99" ForeColor="Black" />
            <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
        </asp:detailsview>
    &nbsp;
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <br />
        <br />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConflictDetection="CompareAllValues" ConnectionString="<%$ ConnectionStrings:MISConnectionString2 %>" DeleteCommand="DELETE FROM [PersistCN] WHERE [TN] = @original_TN AND (([CN] = @original_CN) OR ([CN] IS NULL AND @original_CN IS NULL)) AND (([Name] = @original_Name) OR ([Name] IS NULL AND @original_Name IS NULL)) AND (([Tel] = @original_Tel) OR ([Tel] IS NULL AND @original_Tel IS NULL)) AND (([GROUP_ID] = @original_GROUP_ID) OR ([GROUP_ID] IS NULL AND @original_GROUP_ID IS NULL))" InsertCommand="INSERT INTO [PersistCN] ([TN], [CN], [Name], [Tel], [GROUP_ID]) VALUES (@TN, @CN, @Name, @Tel, @GROUP_ID)" OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT [TN], [CN], [Name], [Tel], [GROUP_ID] FROM [PersistCN]" UpdateCommand="UPDATE [PersistCN] SET [CN] = @CN, [Name] = @Name, [Tel] = @Tel, [GROUP_ID] = @GROUP_ID WHERE [TN] = @original_TN AND (([CN] = @original_CN) OR ([CN] IS NULL AND @original_CN IS NULL)) AND (([Name] = @original_Name) OR ([Name] IS NULL AND @original_Name IS NULL)) AND (([Tel] = @original_Tel) OR ([Tel] IS NULL AND @original_Tel IS NULL)) AND (([GROUP_ID] = @original_GROUP_ID) OR ([GROUP_ID] IS NULL AND @original_GROUP_ID IS NULL))">
            <DeleteParameters>
                <asp:Parameter Name="original_TN" Type="String" />
                <asp:Parameter Name="original_CN" Type="String" />
                <asp:Parameter Name="original_Name" Type="String" />
                <asp:Parameter Name="original_Tel" Type="String" />
                <asp:Parameter Name="original_GROUP_ID" Type="Int32" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="TN" Type="String" />
                <asp:Parameter Name="CN" Type="String" />
                <asp:Parameter Name="Name" Type="String" />
                <asp:Parameter Name="Tel" Type="String" />
                <asp:Parameter Name="GROUP_ID" Type="Int32" />
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="CN" Type="String" />
                <asp:Parameter Name="Name" Type="String" />
                <asp:Parameter Name="Tel" Type="String" />
                <asp:Parameter Name="GROUP_ID" Type="Int32" />
                <asp:Parameter Name="original_TN" Type="String" />
                <asp:Parameter Name="original_CN" Type="String" />
                <asp:Parameter Name="original_Name" Type="String" />
                <asp:Parameter Name="original_Tel" Type="String" />
                <asp:Parameter Name="original_GROUP_ID" Type="Int32" />
            </UpdateParameters>
        </asp:SqlDataSource>
    </form>

    主要是"TN"車格號碼,我已在SQL加上索引,"TN"不能有重複資料(皆為數字)

    希望USER在鍵入資料後能檢查是否與資料庫現有值重複

    如有重複可跳出警訊,不知要如何寫判斷式

    希望各位能提點一下

    謝謝

    2014年6月26日 上午 03:21

解答

所有回覆