当前位置:首页 » 操作系统 » 数据库保存对象

数据库保存对象

发布时间: 2022-05-13 21:20:21

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

有关更多信息,请参见 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) { .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) { .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) { .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。

⑵ SQL Server中,保存着每个数据库对象的信息的系统表是

保存着每个数据库对象的信息的系统表是 Sysobjects

⑶ C#如何把一个类的对象存入数据库(此时在数据库里面应该保存成什么数据类型)

可以使用.net提供的序列化和反序列化方法来实现,你可将对象序列化成XML字符串,然后存入数据库中,当你要使用对象的时候,再把数据库中保存字符串反序列化成对象就可以使用了,以下为示例代码:

publicclassCat
{
publicstringColor{get;set;}
publicintSpeed{get;set;}
publicstringName{get;set;}
}
//序列化
varcat1=newCat{Color="Write",Speed=50,Name="MiMi"};
XmlSerializerser=newXmlSerializer(typeof(Cat));
MemoryStreamms=newMemoryStream();
ser.Serialize(ms,cat1);
stringxmlString=Encoding.UTF8.GetString(ms.ToArray());
//xmlString就是你要保存到数据库的字符串

//反序列化
XmlSerializerdser=newXmlSerializer(typeof(Cat));
//xmlString是你从数据库获取的字符串
StreamxmlStream=newMemoryStream(Encoding.UTF8.GetBytes(xmlString));
Catcat2=dser.Deserialize(xmlStream)asCat;//cat2就是你要得到的class对象

⑷ 数据库以什么形式存放一个对象

表的形式,比如一个对象是车,那么车有的属性就是表的元素,比如车有牌子,有轮子.这些都是对象的属性 直接用表来记录就行了,至于对象的方法 这个肯定不是对象的内容,不用记录到表里.那个是代码..

⑸ iOS开发数据库存储能用FMDB直接存储对象吗

如果要存储复杂对象类型的数据, 该对象必须遵循NSCoding协议, 并实现对应代理方法
对象类型实现代理后, 在存储之前还有一部操作, 是把对象进行归档, 归档完成才可以使用FMDB进行存储, 对应类型为BLOB类型,.
如果要直接存储复杂对象, 不想写归档什么的玩意, 可以使用一个第三方框架 JRDB , 这个可以直接存储复杂对象, jrdb是基于FMDB进行的二次封装框架, 可以试试噢

⑹ 如何将一个SQL数据库的所有对象保存到另一个数据库,包括表、视图、存储过程、函数···

第一步,备份数据库;
第二步,新建一数据库;
第三步,将第一步的数据库备份文件还原至第二步创建的数据库中。

⑺ 1. 数据库中存储的基本对象是什么

答案:

数据库中存储的基本对象是

热点内容
选择ftp服务器 发布:2024-10-10 04:56:16 浏览:197
php函数fopen 发布:2024-10-10 04:50:20 浏览:238
编程飞鸟站 发布:2024-10-10 04:49:34 浏览:615
数据库基础与应用作业 发布:2024-10-10 04:35:31 浏览:847
淘宝上传图片找同款 发布:2024-10-10 04:31:23 浏览:436
玩具直播脚本 发布:2024-10-10 04:31:19 浏览:629
php后门检测工具 发布:2024-10-10 04:18:46 浏览:275
我的世界怎么把服务器封面更改 发布:2024-10-10 04:18:35 浏览:931
linuxsvnserver 发布:2024-10-10 03:56:51 浏览:544
php数组最大长度 发布:2024-10-10 03:41:22 浏览:679