提交对数据库的更改

时间:2015-05-04 18:03:01

标签: c# database forms

我正在使用VS2k10编写一个需要用户名和密码才能运行的C#程序。我有一个数据库保存在外部文件中,其中包含用户名和密码。当我在登录表单中单击“注册”时,将打开另一个表单,允许您将凭据保存到登录数据库。

问题可能在于注册表,而不是登录表。当我成功注册用户帐户时,登录表单似乎无法识别该用户名。它也不保存到文件中。检查了SE如何提交对数据集的更改,但到目前为止没有任何工作。

我目前的代码是:

    DataRow foundRow = ds.Tables["userstable"].Rows.Find(username.Text);
    if (foundRow != null)
    { MessageBox.Show("Username already exists!"); }
    else
    { 
        DataRow newrow = ds.Tables["userstable"].NewRow();
        newrow["ID"] = username.Text;
        newrow["hash"] = CalculateMD5Hash(password.Text + username.Text + "hambába");
        ds.Tables["userstable"].Rows.Add(newrow);
        username.Text = "";
        password.Text = "";
        repeatpass.Text = "";
        ds.AcceptChanges();
        MessageBox.Show("Registration complete!");
    }

“ds”是使用的数据集。 该表有两列 - “ID”和“hash”,它们包含用户名和从密码计算的哈希值。有什么问题,我错过了什么吗?

3 个答案:

答案 0 :(得分:1)

ds.AcceptChanges();不更新,但请签名忽略更改。要进行更新,您需要使用DataAdapter命令进行更新,并使用Update()方法:

DataAdapter.Update(ds);

答案 1 :(得分:1)

您用于填充DataSet的DataAdapter在哪里?你需要做这样的事情。

clear
clc
close all

%// Read and clean up sample image
A = imread('rice.png');

A = im2bw(A,.5);

A = bwareaopen(A,50);
CC = bwconncomp(A);

%// Same as you.
data = regionprops(CC,'all');

%// Concatenate all the areas into an array. 
AllArea = vertcat(data.Area);

%//========================================
%//==== Apply threshold on area here \\====

AboveAreaIndices = find(AllArea > 150);

%// If you wish to remove the entries from the data structure
% data(AllArea>150) = [];
%//========================================

%// Same for centroids...for display purposes
AllCentroids = vertcat(data.Centroid);


%// Display original and thresholded objects. Use the indices calculated
%// above to "mask" large areas if you want
imshow(A);

hold on

scatter(AllCentroids(:,1),AllCentroids(:,2),40,'b','filled')
scatter(AllCentroids(AboveAreaIndices,1),AllCentroids(AboveAreaIndices,2),40,'r','filled')

有关详细信息,请参阅此链接https://msdn.microsoft.com/en-us/library/xzb1zw3x.aspx

我还建议您从影响UI的代码中分离出写入数据库的代码。在这里查看MVC(模型视图控制器)模式是什么。 http://blog.codinghorror.com/understanding-model-view-controller/并在此处查看有关ASP.NET MVC http://www.w3schools.com/aspnet/mvc_app.asp

的初学者教程

答案 2 :(得分:0)

我不知道你的设置,但是ds.AcceptChanges();不要将更改写入底层数据库。如果你设置它就像你必须显示你的代码,但单独的AcceptChanges是不够的。 MSDN:在DataSet上调用AcceptChanges时,任何仍处于编辑模式的DataRow对象都会成功结束编辑。每个DataRow的RowState属性也会发生变化;添加和修改的行变为未更改,删除的行将被删除。

相关问题