私の学校プロジェクトの報酬システムを行っています。私はPKの違反を解決するための助けが必要です。誰がなぜこれが起こるのか知っていますか?C#SQL Serverデータベース用のPKのVisual Studio違反
は、ここに私のコード(points.cs)ポイント
私はbtn_done
をクリックしたときに控除ポイントを保存することができる午前ことを願ってい
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)
);
ため
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テーブルです。しかし、私はtrans_no
を最新のものにする方法を知りません
エラーメッセージを入力してください。 – Will
カラム** trans_no **は同一であるため、自動増分されます。挿入時に列** trans_no **を使用しないでください。 – Paarth