C#Visual Studio违反了SQL Server数据库的PK

本文关键字:Server 数据库 PK SQL Studio C#Visual | 更新日期: 2023-09-27 17:59:04

我正在为我的学校项目制定奖励系统,我需要帮助解决我违反PK的问题。有人知道为什么会发生这种情况吗?

这是我的代码(points.cs)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public class Points
{
    //string _connStr = Configuration.ConnectionStringSettings _connStr;
    string connStr = ConfigurationManager.ConnectionStrings["EBizDBContext"].ConnectionString;
    private string _username = "";
    private int _trans_no = 0;
    private string _date = "";
    private int _points_added = 0;
    private int _points_deducted = 100;
    private string _description = "";
    public Points()
    {
    }
    public Points(string username, int transNo, string date, int points_added, int points_deducted)
    {
        _username = username;
        _trans_no = transNo;
        _date = date;
        _points_added = points_added;
        _points_deducted = points_deducted;
    }
    public Points(string username, string date, int points_added, int points_deducted) : this(username, 0, date, points_added, points_deducted)
    {
    }
    public Points(int trans_no) : this("", trans_no, "", 0, 0)
    {
    }
    public Points(string date, int points_added, int points_deducted)
    {
        // TODO: Complete member initialization
        this._date = date;
        this._points_added = points_added;
        this._points_deducted = points_deducted;
    }
    public Points(int trans_no, string date, int points_added, int points_deducted)
    {
        this._trans_no = trans_no;
        this._date = date;
        this._points_added = points_added;
        this._points_deducted = points_deducted;
    }
    public int trans_no
    {
        get { return _trans_no; }
        set { _trans_no = value; }
    }
    public string date
    {
        get { return _date; }
        set { _date = value; }
    }
    public int points_added
    {
        get { return _points_added; }
        set { _points_added = value; }
    }
    public int points_deducted
    {
        get { return _points_deducted; }
        set { _points_deducted = value; }
    }
    public string username
    {
        get { return _username; }
       set { _username = value; }
    }
    public Points getPoints(string username)
    {
         Points pointsDetails = null;
         int trans_no, points_added, points_deducted;
         string date;
         string queryStr = "SELECT * FROM Points WHERE username = 'ad';";
         SqlConnection conn = new SqlConnection(connStr);
         SqlCommand cmd = new SqlCommand(queryStr, conn);
         cmd.Parameters.AddWithValue("@username", username);
         conn.Open();
         SqlDataReader dr = cmd.ExecuteReader();
         if (dr.Read())
         {
             trans_no = int.Parse(dr["trans_no"].ToString());
             date = dr["date"].ToString();
             points_added = int.Parse(dr["points_added"].ToString());
             points_deducted = int.Parse(dr["points_deducted"].ToString());
             pointsDetails = new Points(username, trans_no, date, points_added, points_deducted);
        }
        else
        {
            pointsDetails = null;
        }
        conn.Close();
        dr.Close();
        dr.Dispose();
        return pointsDetails;
    }
    public List<Points> getPointsAll()
    {
        List<Points> pointsList = new List<Points>();
        string username, date;
        int trans_no, points_added, points_deducted;
        string queryStr = "SELECT * FROM Points Order By username";
        SqlConnection conn = new SqlConnection(connStr);
        SqlCommand cmd = new SqlCommand(queryStr, conn);
        conn.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            username = dr["username"].ToString();
            trans_no = int.Parse(dr["trans_no"].ToString());
            date = dr["date"].ToString();
            points_added = int.Parse(dr["points_added"].ToString());
            points_deducted = int.Parse(dr["points_deducted"].ToString());
            Points a = new Points(username, trans_no, date, points_added, points_deducted);
            pointsList.Add(a);
        }
        conn.Close();
        dr.Close();
        dr.Dispose();
        return pointsList;
    }
    public List<Points> getPointsByUsername()
    {
        List<Points> pointsList = new List<Points>();
        string date;
        int trans_no, points_added, points_deducted;
        string queryStr = "SELECT trans_no, date, points_added, points_deducted FROM points WHERE username = 'pp';";
        SqlConnection conn = new SqlConnection(connStr);
        SqlCommand cmd = new SqlCommand(queryStr, conn);
        cmd.Parameters.AddWithValue("@username", username);
        conn.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
             trans_no = int.Parse(dr["trans_no"].ToString());
             date = dr["date"].ToString();
             points_added = int.Parse(dr["points_added"].ToString());
             points_deducted = int.Parse(dr["points_deducted"].ToString());
             Points b = new Points(trans_no, date, points_added, points_deducted);
             pointsList.Add(b);
        }
        conn.Close();
        dr.Close();
        dr.Dispose();
        return pointsList;
    } // end of retrieve
    public int PointsInsert1()
    {
        string msg = null;
        int result = 0;
        string queryStr = "INSERT INTO points(username, trans_no, date, points_deducted)"
        + "VALUES(@username, @trans_no, @date,@points_deducted);" + "SELECT @@IDENTTY AS int32;";
        SqlConnection conn = new SqlConnection(connStr);
        SqlCommand cmd = new SqlCommand(queryStr, conn);
        cmd.Parameters.AddWithValue("@username", this.username);
        cmd.Parameters.AddWithValue("@trans_no", this.trans_no);
        cmd.Parameters.AddWithValue("@date", this.date);
        cmd.Parameters.AddWithValue("@points_deducted", this.points_deducted);
        conn.Open();
        result += cmd.ExecuteNonQuery();
        conn.Close();
        return result;
    }
}

点DB表

CREATE TABLE [dbo].[points] 
(
    [username]        NVARCHAR(20) NOT NULL,
    [trans_no]        INT IDENTITY(1, 1)    NOT NULL,
    [date]            DATETIME     NOT NULL,
    [points_added]    INT          NULL,
    [points_deducted] INT          NULL,
    PRIMARY KEY CLUSTERED ([trans_no] ASC)
);

我希望我能够保存点击btn_done时扣除的分数。然而,我不知道如何使trans_no成为最新的

C#Visual Studio违反了SQL Server数据库的PK

我的猜测是,您正在尝试使用PointsInsert1中指定的标识列执行INSERT。不确定这是否真的是在没有任何堆栈跟踪或错误消息的情况下得到错误的地方,但如果你试图在标识列中插入一个具有值的行,而该标识列存在,你会遇到麻烦。

由于列trans_no是标识,因此它将自动递增。所以在插入时不要使用列trans_no。请使用更新的PointsInsert1方法:

public int PointsInsert1()
    {
        string msg = null;
        int result = 0;
        string queryStr = "INSERT INTO points(username,  date, points_deducted)"
        + "VALUES(@username, @date,@points_deducted);" + "SELECT @@IDENTTY AS int32;";
        SqlConnection conn = new SqlConnection(connStr);
        SqlCommand cmd = new SqlCommand(queryStr, conn);
        cmd.Parameters.AddWithValue("@username", this.username);
        cmd.Parameters.AddWithValue("@date", this.date);
        cmd.Parameters.AddWithValue("@points_deducted", this.points_deducted);
        conn.Open();
        result += cmd.ExecuteNonQuery();
        conn.Close();
        return result;
    }

虽然trans_no列是Identity,但您正在尝试插入trans_no。如果我们从插入查询中删除CCD_ 10列。它会起作用的。希望它能清除你的PK违规异常。