我正在爲我的學校項目做一個獎勵系統,我需要幫助解決我對PK的違規問題。任何人都知道這是爲什麼發生?C#Visual Studio SQL Server數據庫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
成爲最新的
請提供錯誤信息。 – Will
由於列** trans_no **是標識,所以它會自動遞增。所以在插入時不要使用column ** trans_no **。 – Paarth