C# 无法从Visual Studio 2012向MS Access 2013中的表添加数据
我在VisualStudio2012中制作了一个web表单,并且正在制作一个用户管理表单,以便管理员可以登录并转到该页面,编辑、删除或添加用户。我使用MS Access 2013,我习惯于在VS 2008中工作。这与Access 2013的VS 2008中的代码完全相同。表单上有一个Gridview,它绑定到Access中的表。出于某种原因,按钮中调用我的数据层类中的函数的C# 无法从Visual Studio 2012向MS Access 2013中的表添加数据,c#,ms-access,visual-studio-2012,C#,Ms Access,Visual Studio 2012,我在VisualStudio2012中制作了一个web表单,并且正在制作一个用户管理表单,以便管理员可以登录并转到该页面,编辑、删除或添加用户。我使用MS Access 2013,我习惯于在VS 2008中工作。这与Access 2013的VS 2008中的代码完全相同。表单上有一个Gridview,它绑定到Access中的表。出于某种原因,按钮中调用我的数据层类中的函数的if()语句Click事件一直返回false,因此我的结果标签一直说用户尚未添加到数据库中 我已经尝试将值作为txtUser
if()
语句Click
事件一直返回false
,因此我的结果标签一直说用户尚未添加到数据库中
我已经尝试将值作为txtUser.Text传递,以及现在如何将其编写为ToString()
以下是按钮单击代码:
protected void btnAddUser_Click(object sender, EventArgs e)
{
//makes call to saveusers class to add the new user to the database sending the values with it
if (clsDataLayer.SaveUsers(Server.MapPath("~/App_Data/TPSDB.accdb"),
txtUser.Text.ToString(),
txtPassword.Text.ToString()))
{
//if saveusers function is successful and true then informs the user of the results
grdEditManagers.DataBind();
lblResults.Text = "The user has been added to the database!";
}
//if saveusers function is not successful or false then informs user
else
{
lblResults.Text = "The user has not been added to the database!";
}
}
下面是clsDataLayer
类中的SaveUsers()
代码:
//this function saves the new user
public static bool SaveUsers(string Database, string UserName, string UserPassword)
{
bool recordSaved;
// Creates transaction for rollback in case of error
OleDbTransaction myTransaction = null;
try
{
// creates variable of a new connection to DB
OleDbConnection conn = new OleDbConnection("PROVIDER=Microsoft.ACE.OLEDB.12.0;" +
"Data Source=" + Database);
conn.Open();//opens connection
OleDbCommand command = conn.CreateCommand();
string strSQL;
// beginning of the commit or rollback transaction
myTransaction = conn.BeginTransaction();
command.Transaction = myTransaction;
//strSQL assigned to first and last names only
strSQL = "Insert into tblManagerLogin " +
"(UserName, Password) values ('" +
UserName + "', '" + UserPassword + "')";
// SQL command for stored procedure
command.CommandType = CommandType.Text;
command.CommandText = strSQL;
// statement against connection returns number of rows affected
command.ExecuteNonQuery();
// ends the rollback or commit block
myTransaction.Commit();
// close database connection
conn.Close();
recordSaved = true;
}
catch (Exception ex)
{
// rolls back both entries in case an error is caught
myTransaction.Rollback();
recordSaved = false;
}
return recordSaved;
}
我有点不解为什么该代码应该在VisualStudio2008中工作,因为PASSWORD
是ACE/Jet中的一个密码,我希望如此
INSERT INTO tblManagerLogin (UserName, Password) ...
无论如何都要失败。您需要将Password
更改为[Password]
,因此在执行此操作时,您可以更改代码以使用如下参数化查询
strSQL = "INSERT INTO tblManagerLogin " +
"(UserName, [Password]) VALUES (?, ?)";
// SQL command for stored procedure
command.CommandType = CommandType.Text;
command.CommandText = strSQL;
// add parameters to OleDbCommand
command.Parameters.AddWithValue("?", UserName);
command.Parameters.AddWithValue("?", UserPassword);
// statement against connection returns number of rows affected
command.ExecuteNonQuery();
我在使用studio 2012和access 2013时也遇到同样的问题。我使用C#和VB得到了相同的结果。看起来studio读取初始表值,如果添加了其他行,则不会读取更新的access文件。这很容易复制。非常令人沮丧 当您接受异常时,您不知道为什么recordSaved没有设置为true。我还直接在Access中运行strSQL来检查它是否有效。是的,它有效。这是因为就像你说的密码是一个保留字。我真的没想过。一旦我在密码周围加上[]括号,它就起作用了。我也可以随时更改表名。我还意识到它在2008年起作用的原因是表字段没有命名为Password。非常感谢。另外,对于我的下一个函数,我将在这里添加像您的示例这样的参数。再次感谢。