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 に答える