[英]C# SQL: Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints
I'm new to SQL in C# (and SQL in general). 我是C#中SQL的新手(和一般SQL)。
I'm making this simple users management program (which is part of a bigger thing), where you have the option to register a new account, and view all the SQL Data Base content in a DataGrid. 我正在制作这个简单的用户管理程序(这是更大的事情的一部分),在这里您可以选择注册一个新帐户,并查看DataGrid中所有SQL数据库内容。
I can add users, (Read/write from the DB) but for some reason, when I load the WPF page with the DataGrid, it crashed when it tries to load it and gives the error: 我可以添加用户(从数据库读/写),但是由于某种原因,当我用DataGrid加载WPF页面时,在尝试加载它并给出错误时它崩溃了:
Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.
I tried to google an answer, but I couldn't find one, or understand what should I do. 我试图用谷歌搜索一个答案,但找不到答案,或者不知道该怎么办。
this is my C# code: 这是我的C#代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
//SQL
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
namespace FMS_Csharp_GUI
{
/// <summary>
/// Interaction logic for UsersBD.xaml
/// </summary>
public partial class UsersBD : Window
{
SqlConnection connection = new SqlConnection();
string sqlConnectionString;
public UsersBD()
{
InitializeComponent();
sqlConnectionString = ConfigurationManager.ConnectionStrings["FMS_Csharp_GUI.Properties.Settings.UsersConnectionString"].ConnectionString;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
FMS_Csharp_GUI.UsersDataSet usersDataSet = ((FMS_Csharp_GUI.UsersDataSet)(this.FindResource("usersDataSet")));
// Load data into the table UsersTable. You can modify this code as needed.
FMS_Csharp_GUI.UsersDataSetTableAdapters.UsersTableTableAdapter usersDataSetUsersTableTableAdapter = new FMS_Csharp_GUI.UsersDataSetTableAdapters.UsersTableTableAdapter();
/*code crash in this line: */ usersDataSetUsersTableTableAdapter.Fill(usersDataSet.UsersTable);
System.Windows.Data.CollectionViewSource usersTableViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("usersTableViewSource")));
usersTableViewSource.View.MoveCurrentToFirst();
}
private void btnAddUser_Click(object sender, RoutedEventArgs e)
{
string query = "INSERT INTO UsersTable VALUES (@username, @password, @IsAdmin);";
//get input
string username = txtUsername.Text;
string password = txtPassword.Text;
bool admin = chckAdmin.IsChecked == true;
using(connection = new SqlConnection(sqlConnectionString))
using(SqlCommand command = new SqlCommand(query,connection)) {
connection.Open();
command.Parameters.AddWithValue("@username", username);
command.Parameters.AddWithValue("@password", password);
command.Parameters.AddWithValue("@IsAdmin", admin);
command.ExecuteScalar();
}
}
}
}
this is my SQL DB code: 这是我的SQL DB代码:
CREATE TABLE [dbo].[UsersTable] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Name] NCHAR (15) NOT NULL,
[PasswordMD5] NCHAR (32) NOT NULL,
[IsAdmin] BIT DEFAULT ((0)) NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
this is my data base: 这是我的数据库:
Thanks a lot! 非常感谢! PS
FMS_Csharp_GUI
is the name of the program. PS
FMS_Csharp_GUI
是程序的名称。
For some reason I don't konw it didn't work. 由于某种原因,我不知道它没有用。 but now it does, after I tried to do this :
但是现在,在我尝试执行此操作后,它就开始了:
using (connection = new SqlConnection(sqlConnectionString))
using (SqlDataAdapter adapter = new SqlDataAdapter("SELECT ID,Name,PasswordSHA256, IsAdmin"
+ " From UsersTable", connection))
{
//get data
DataTable user = new DataTable();
adapter.Fill(user);
usersTableDataGrid.DataContext = user;
}
then I highlighted this code, and used the one above, and it worked! 然后我突出显示了此代码,并使用了上面的代码,它起作用了!
maybe getting it this why made it work? 也许得到它,为什么它能起作用?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.