1

我正在尝试使用以下字符串连接来访问 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();
4

1 回答 1

3

cmd.ExecuteNonQuery();在创建命令之后关闭连接之前添加

于 2012-08-28T20:59:57.900 回答