Access数据库未更新

时间:2012-08-28 20:57:05

标签: c# database ms-access

我正在尝试使用以下字符串连接来连接到访问2010数据库。但是,它不会对数据库进行任何更改。

OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.Oledb.12.0;Data Source=C:\\Program Files\\LogEntry\\LogEntry.accdb; Persist Security Info = False;");

conn.Open();

String text2send = "INSERT INTO TLC(Name,Department,Position,VisitDate,InTime,OutTime,Purpose,HelpedBy,Campus,HelpCode) VALUES(" + name + "," + department + "," + position + "," + date + "," + hourIn + "," + hourOut + "," + purpose + "," + helpedBy + "," + campus + "," + helpcode + ");";

OleDbCommand cmd = new OleDbCommand(text2send,conn);
conn.Close();

编辑: 这是我用于参数查询的编辑代码。

 String name = nameTextbox.Text;
 String department = departmentCBox.Text;
 String purpose = purposeTextbox.Text;
 String position = positionCBox.Text;
 String date = inDate.Value.ToString("MM/dd/yyyy");
 String helpCode = helpCodeCBox.Text;
 String hourOut = ""+OutHour.Text+":"+OutMin+" "+OutMeredian;
 String helpedBy= "";
 String campus= "";
 String helpcode= "";
 String hourIn = "" + DateTime.Now.ToString("hh") + ":" + 

 DateTime.Now.ToString("mm") + " " + DateTime.Now.ToString("tt");

 OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.Oledb.12.0;Data Source=C:\\Program Files\\LogEntry\\LogEntry.accdb; Persist Security Info = False;");
            conn.Open();


 String text2send = "Insert Into TLC([Name],[Department],[Position],[VisitDate],[InTime],[OutTime],[Purpose],[HelpedBy],[Campus],[HelpCode]) VALUE(?,?,?,?,?,?,?,?,?,?);";

 OleDbCommand cmd = new OleDbCommand(text2send,conn);
 cmd.Parameters.AddWithValue("Name", name);
 cmd.Parameters.AddWithValue("Department", department);
 cmd.Parameters.AddWithValue("Position", position);
 cmd.Parameters.AddWithValue("Purpose", purpose);
 cmd.Parameters.AddWithValue("HelpedBy", helpedBy);
 cmd.Parameters.AddWithValue("Campus", campus);
 cmd.Parameters.AddWithValue("HelpCode", helpcode);

 cmd.ExecuteNonQuery();
 conn.Close();

1 个答案:

答案 0 :(得分:3)

在创建命令之后和关闭连接之前

添加cmd.ExecuteNonQuery();