如何:将数据从对象保存到数据库

如何:将数据从对象保存到数据库,第1张

有关更多信息,请参见 TableAdapter 概述。若要保存对象集合中的数据,请循环通过对象集合(例如,for-next 循环),然后使用 TableAdapter 的 DBDirect 方法之一将每个对象的值发送到数据库中。默认情况下,DBDirect 方法在 TableAdapter 上创建,能够直接对数据库执行 *** 作。这些方法可以直接调用,它们不要求 DataSet 或DataTable 对象来协调更改即可将更新发送到数据库。注意配置TableAdapter 时,主查询必须提供足够的信息,才能创建 DBDirect 方法。例如,如果将 TableAdapter 配置为从未定义主键列的表中查询数据,它将不会生成 DBDirect 方法。 TableAdapter DBDirect 方法 说明TableAdapter.Insert向数据库中添加新记录,此汪迹方法允许将值作为方法参数传入各个列。TableAdapter.Update更新数据库中的现有记录。Update 方法将原始列值和新列值用作方法参数。原始值用于定位原始记录,新值用于更新该记录。通过将 DataSet、DataTable、DataRow、或 DataRow 数组用作方法参数,困宴并TableAdapter.Update 方法还可用于将数据集中的更改协调回数据库。TableAdapter.Delete在基于作为方法参数传入的原始列值的数据库中,删除其现有祥差记录。将对象中的新记录保存到数据库中通过将值传递给 TableAdapter.Insert 方法可创建这些记录。下面的示例通过将 currentCustomer 对象中的值传递给 TableAdapter.Insert 方法,在 Customers 表中创建一项新的客户记录。 Visual Basic PrivateSub AddNewCustomer(ByVal currentCustomer As Customer) CustomersTableAdapter.Insert( _ currentCustomer.CustomerID, _ currentCustomer.CompanyName, _ currentCustomer.ContactName, _ currentCustomer.ContactTitle, _ currentCustomer.Address, _ currentCustomer.City, _ currentCustomer.Region, _ currentCustomer.PostalCode, _ currentCustomer.Country, _ currentCustomer.Phone, _ currentCustomer.Fax) EndSub C# privatevoid AddNewCustomers(Customer currentCustomer) { customersTableAdapter.Insert( currentCustomer.CustomerID, currentCustomer.CompanyName, currentCustomer.ContactName, currentCustomer.ContactTitle, currentCustomer.Address, currentCustomer.City, currentCustomer.Region, currentCustomer.PostalCode, currentCustomer.Country, currentCustomer.Phone, currentCustomer.Fax)} J# privatevoid AddNewCustomers(Customer currentCustomer) { northwindDataSetCustomersTableAdapter.Insert( currentCustomer.get_CustomerID(), currentCustomer.get_CompanyName(), currentCustomer.get_ContactName(), currentCustomer.get_ContactTitle(), currentCustomer.get_Address(), currentCustomer.get_City(), currentCustomer.get_Region(), currentCustomer.get_PostalCode(), currentCustomer.get_Country(), currentCustomer.get_Phone(), currentCustomer.get_Fax())}将对象中的现有记录更新到数据库修改记录:调用 TableAdapter.Update 方法并传入新值可更新记录,而传入原始值可定位记录。注意对象需要保留其原始值,才能将它们传递给 Update 方法。此示例使用前缀为 orig 的属性存储原始值。下面的示例通过将 Customer 对象中的新值和原始值传递给 TableAdapter.Update 方法,更新 Customers 表中的现有记录。 Visual Basic PrivateSub UpdateCustomer(ByVal cust As Customer) CustomersTableAdapter.Update( _ cust.CustomerID, _ cust.CompanyName, _ cust.ContactName, _ cust.ContactTitle, _ cust.Address, _ cust.City, _ cust.Region, _ cust.PostalCode, _ cust.Country, _ cust.Phone, _ cust.Fax, _ cust.origCustomerID, _ cust.origCompanyName, _ cust.origContactName, _ cust.origContactTitle, _ cust.origAddress, _ cust.origCity, _ cust.origRegion, _ cust.origPostalCode, _ cust.origCountry, _ cust.origPhone, _ cust.origFax) EndSub C# privatevoid UpdateCustomer(Customer cust) { customersTableAdapter.Update( cust.CustomerID, cust.CompanyName, cust.ContactName, cust.ContactTitle, cust.Address, cust.City, cust.Region, cust.PostalCode, cust.Country, cust.Phone, cust.Fax, cust.origCustomerID, cust.origCompanyName, cust.origContactName, cust.origContactTitle, cust.origAddress, cust.origCity, cust.origRegion, cust.origPostalCode, cust.origCountry, cust.origPhone, cust.origFax)} J# privatevoid UpdateCustomer(Customer cust) { northwindDataSetCustomersTableAdapter.Update( cust.get_CustomerID(), cust.get_CompanyName(), cust.get_ContactName(), cust.get_ContactTitle(), cust.get_Address(), cust.get_City(), cust.get_Region(), cust.get_PostalCode(), cust.get_Country(), cust.get_Phone(), cust.get_Fax(), cust.get_origCustomerID(), cust.get_origCompanyName(), cust.get_origContactName(), cust.get_origContactTitle(), cust.get_origAddress(), cust.get_origCity(), cust.get_origRegion(), cust.get_origPostalCode(), cust.get_origCountry(), cust.get_origPhone(), cust.get_origFax())}删除数据库中的现有记录通过调用 TableAdapter.Delete 方法并传入原始值定位记录,可删除记录。注意对象需要保留其原始值,才能将它们传递给 Delete 方法。 Visual Basic PrivateSub DeleteCustomer(ByVal cust As Customer) CustomersTableAdapter.Delete( _ cust.origCustomerID, _ cust.origCompanyName, _ cust.origContactName, _ cust.origContactTitle, _ cust.origAddress, _ cust.origCity, _ cust.origRegion, _ cust.origPostalCode, _ cust.origCountry, _ cust.origPhone, _ cust.origFax) EndSub C# privatevoid DeleteCustomer(Customer cust) { customersTableAdapter.Delete( cust.origCustomerID, cust.origCompanyName, cust.origContactName, cust.origContactTitle, cust.origAddress, cust.origCity, cust.origRegion, cust.origPostalCode, cust.origCountry, cust.origPhone, cust.origFax)} J# privatevoid DeleteCustomer(Customer cust) { northwindDataSetCustomersTableAdapter.Delete( cust.get_origCustomerID(), cust.get_origCompanyName(), cust.get_origContactName(), cust.get_origContactTitle(), cust.get_origAddress(), cust.get_origCity(), cust.get_origRegion(), cust.get_origPostalCode(), cust.get_origCountry(), cust.get_origPhone(), cust.get_origFax())}安全您必须具有相应的权限,才能对数据库中的表执行选定的 INSERT、UPDATE 或 DELETE。

JDBC环境下

如果使用的是PreparedStatement接口:

String sql = "insert into 表名(date类型的列) values(?)"

PreparedStatement stmt = con.prepareStatement(sql)

stmt.setDate(1, date对象)

stmt.executeUpdate()

如果使用的是Statement接口:扒此蚂

java.text.SimpleDateFormat fmt1 = new java.text.SimpleDateFormat("yyyy-MM-dd") // mssql、mysql格式

java.text.SimpleDateFormat fmt2 = new java.text.SimpleDateFormat("dd-MM月-yyyy") // oracle格式

String sql = String.format("春埋insert into 表名(date类型的列) values('%s')", fmt1.format(date对象扒中))

Statement stmt = con.createStatement()

stmt.executeUpdate(sql)

使用Hibernate和mybaits的确就不用关心类型转换问题了~

为了避免多次进行数型差据库 *** 作,使用批处理,即如下的代码:

String sql = "insert into employee (name, city, phone) values (?, ?, ?)"

Connection connection = new getConnection()

PreparedStatement ps = connection.prepareStatement(sql)

final int batchSize = 1000

int count = 0

for (Employee employee: employees) {

    ps.setString(1, 卜弯皮employee.getName())

    ps.setString(2, employee.getCity())

    ps.setString(3, employee.getPhone())

    ps.addBatch()

    if(++count % batchSize == 0) 闹含{

        ps.executeBatch()

    }

}

ps.executeBatch() // insert remaining records

ps.close()

connection.close()


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11971220.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存