[英]C# Visual Studio violation of PK for SQL Server database
I'm doing a reward system for my school project and I need help for solving my violation of PK. 我正在为我的学校项目做奖励系统,我需要帮助解决我违反PK的问题。 Anyone knows why this happens?
谁知道为什么会这样?
Here's my code (points.cs) 这是我的代码(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 table for points 数据库表为点
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)
);
I hope that I am able to save points deducted when btn_done
is clicked. 我希望能够在点击
btn_done
时保存扣除的积分。 However I do not know how to make the trans_no
to become the latest one 但是我不知道如何让
trans_no
成为最新的
My guess is that you're trying to do an INSERT
with the identity column specified in PointsInsert1
. 我的猜测是你试图用
PointsInsert1
指定的标识列进行INSERT
。 Not sure if that's actually where you're getting the error without any sort of stack trace or error messages, but if you try to insert a row with a value in an identity column, and that identity column exists, you're going to have a bad time. 不确定这是否真的在没有任何堆栈跟踪或错误消息的情况下获得错误,但如果您尝试在标识列中插入一个值,并且该标识列存在,那么您将拥有糟糕的时光。
As column trans_no
is identity, so it will auto increment. 由于列
trans_no
是标识,因此它将自动递增。 so don't use column trans_no
at time of insert. 所以在插入时不要使用列
trans_no
。 please use updated PointsInsert1
method: 请使用更新的
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;
}
although trans_no
column is Identity, you are trying to insert trans_no
. 虽然
trans_no
列是Identity,但您尝试插入trans_no
。 if we remove trans_no
column from insert query. 如果我们从插入查询中删除
trans_no
列。 it will work. 它会工作。 Hope it clear your PK violation exception.
希望清除你的PK违规例外。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.