レコードをサイクルにレコードセットまたはカーソルのいくつかの種類を必要としますクエリを更新します。
ODBCConeectionオブジェクトを使用してMSAccessデータベースへのデータベース接続を確立するために使用できるJETデータベースドライバがあります。
string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\\PathTo\\Your_Database_Name.mdb; User Id=admin; Password=";
using (OdbcConnection connection =
new OdbcConnection(connectionString))
{
// Suppose you wanted to update the Salary column in a table
// called Employees
string sqlQuery = "UPDATE Employees SET Salary = Salary * Factor";
OdbcCommand command = new OdbcCommand(sqlQuery, connection);
try
{
connection.Open();
command.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
// The connection is automatically closed when the
// code exits the using block.
}
あなたは、接続文字列を生成を助けるためにこれらのウェブサイトを使用することができます。
EDIT - レコードをサイクルにデータリーダーを使用する例ビジネスルールを簡潔にするため
次の例は、(特にデータベースドライバがパラメータ化されたクエリをサポートしている場合)いくつかの点で改善される可能性があることに注意してください。私は、概念を説明するために比較的簡単な例を挙げたかっただけです。
using (OdbcConnection connection =
new OdbcConnection(connectionString))
{
int someNumber;
int employeeID;
OdbcDataReader dr = null;
OdbcCommand selCmd = new OdbcCommand("SELECT EmployeeID, SomeNumber FROM Employees", connection);
OdbcCommand updateCmd = new OdbcCommand("", connection);
try
{
connection.Open();
dr = selCmd.ExecuteReader();
while(dr.Read())
{
employeeID = (int)dr[0];
someNumber = (int)dr[1];
updateCmd.CommandText = "UPDATE Employees SET SomeNumber= " + GetBusinessRule(someNumber) + " WHERE employeeID = " + employeeID;
updateCmd.ExecuteNonQuery();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
// Don't forget to close the reader when we're done
if(dr != null)
dr.Close();
}
// The connection is automatically closed when the
// code exits the using block.
}
SQL UPDATE文*は、検索条件(WHERE句)を追加しない限り、テーブル内のすべてのレコードに影響します。なぜあなたはレコードを循環させる必要があると思いますか? – onedaywhen
私は、更新はサーバー、sthに対して実行できない複雑なビジネスロジックを含んでいるので、私はレコードをtrhoughする必要があると思います。 column1 = ApplyBussinessRule(Column2)のように、サーバーにApplyBussinessRuleを実装する方法はありません。 - Peter 52 secs ago – Peter
msAccessのようなサーバー意味のdatabaseserverまたはdatabaseapp – Peter