我有一个允许用户填写信息的程序,然后将其存储在本地数据库中。我可以执行处理它的存储过程,但是当我通过visual studio检查数据时,更改不存在。但是,如果我运行我的程序并输入一个重复的主键,则会抛出该值已存在的错误。我确保没有重复的文件。
有什么建议吗?
存储过程
CREATE PROCEDURE [dbo].[NewUser]
@username as text,
@password as text,
@attackMethod as text,
@statPreference as text
AS
INSERT INTO Users
VALUES (@username,@password,@attackMethod,@statPreference)
RETURN 0
以及将其投入数据库的代码。
try
{
connection.Open();
SqlCommand command = new SqlCommand("NewUser",connection);
command.Connection = connection;
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("@Username", SqlDbType.NVarChar);
command.Parameters.AddWithValue("@Password", SqlDbType.Text);
command.Parameters.AddWithValue("@attackMethod", SqlDbType.Text);
command.Parameters.AddWithValue("@StatPreference", SqlDbType.Text);
command.Parameters["@Username"].Value = this.Name;
command.Parameters["@Password"].Value = this.Password;
command.Parameters["@attackMethod"].Value = this.AttackMethod;
command.Parameters["@StatPreference"].Value = this.SkillPreference;
command.ExecuteNonQuery();
connection.Close();
return true;
}
catch(Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
connection.Close();
return false;
}
答案 0 :(得分:0)
AddWithValue
方法应该采用parameterName和表示值的对象,而不是SqlDbType
,因此存在问题。 @Username也与@username不同。一个人有首都。
请尝试使用此模式:
command.Parameters.Add("@username ", SqlDbType.Text);
command.Parameters["@username "].Value = this.Name;
答案 1 :(得分:0)
我找到了解决方案。事实证明,visual studio正在将我的数据库副本生成到debug文件夹中。它解释了为什么它会节省但不会显示。为了解决这个问题,我刚刚删除了我的数据连接,并在我的调试目录中的.mdf文件中指出了一个新的数据连接。
谢谢大家的帮助!