C#SQL:无法启用约束。一行或多行包含违反非空,唯一或外键约束的值

时间:2015-09-16 14:22:51

标签: c# sql sql-server datagrid

我是C#中的SQL新手(以及一般的SQL)。

我正在制作这个简单的用户管理程序(这是更重要的事情的一部分),您可以选择注册新帐户,并查看DataGrid中的所有SQL数据库内容。

我可以添加用户,(从数据库读取/写入)但由于某种原因,当我使用DataGrid加载WPF页面时,它在尝试加载它时崩溃并发出错误:

Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.

我尝试谷歌答案,但我找不到,或理解我该怎么做。

这是我的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();
        }

    }
}
}

这是我的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)
);

这是我的数据库: enter image description here

非常感谢! 附: FMS_Csharp_GUI是该计划的名称。

1 个答案:

答案 0 :(得分:0)

出于某种原因,我不知道它不起作用。但是现在确实如此,在我尝试这样做之后:

            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;
        }

然后我突出显示了这段代码,并使用了上面的代码,它有效!

也许这就是为什么让它起作用?