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

如何:将数据从对象保存到数据库,第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。

1. C#中怎么把数据保存到ACCESS数据库

Sqlmand是 *** 作sql数据库的,Access用OleDbmand首先定义一个链接对象OleDbConnection conn = new OleDbConnection("[数据库连接字符串]");conn.Open(); 打开数据库连接OleDbmand cmd = new OleDbmand("[Insert/Update/Delte语句]", conn)cmd.ExecuteNonQuery(); 执行 *** 作,如果是查询则不是用这个方法 最后别忘记关闭数据库连接和释放对象。

2. C# 如何将 图片直接存入SQL数据库中

文件转成二进制流出入数据库

private void button2_Click(object sender, EventArgs e)

{

FileStream fs = new FileStream(textBox1.Text, FileMode.Open)

BinaryReader br = new BinaryReader(fs)

Byte[] byData = br.ReadBytes((int)fs.Length)

fs.Close()

string conn = "server=.database=testDBUid=saPwd=sa "

SqlConnection myconn = new SqlConnection(conn)

myconn.Open()

string str = "insert into pro_table (pro_name,pro_file) values('测试文件',@file)"

Sqlmand mym = new Sqlmand(str, myconn)

mym.Parameters.Add("@file", SqlDbType.Binary, byData.Length)

mym.Parameters["@file"].Value = byData

mym.ExecuteNonQuery()

myconn.Close()

}

从数据库中把二进制流读出写入还原成文件

private void button4_Click(object sender, EventArgs e)

{

string conn = "server=.database=testDBUid=saPwd=sa "

string str = "select pro_file from pro_table where pro_name='测试文件' ";

SqlConnection myconn = new SqlConnection(conn)

SqlDataAdapter sda = new SqlDataAdapter(str, conn)

DataSet myds = new DataSet()

myconn.Open()

sda.Fill(myds)

myconn.Close()

Byte[] Files = (Byte[])myds.Tables[0].Rows[0]["pro_file"]

BinaryWriter bw = new BinaryWriter(File.Open("D:\\2.rdlc",FileMode.OpenOrCreate))

bw.Write(Files)

bw.Close()

}

3. c#如何把图片存取到SQL数据库

一楼开玩笑了!!可以保存到数据库的。

首先,你的数据库里要有一个存放二进制数据的字段。然后,用一个文件选择控件,让用户选择图片。

用FileStream.Read把图片文件按照二进制读取到byte[]中,接下来,链接数据库,用sql语句,进行相应的插入 *** 作,将数据库的二进制数据的字段赋值为byte[]就行了。以下是保存和显示的代码:private void SaveImage(string fileName) { Read the file into a byte array using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { byte[] imageData = new Byte[fs.Length]fs.Read(imageData, 0, (int)fs.Length)using (SqlConnection conn = new SqlConnection(connectionString)) { string sql = "insert into image (imagefilename,blobdata) values (@filename,@blobdata)"Sqlmand cmd = new Sqlmand(sql, conn)cmd.Parameters.Add("@filename",SqlDbType.Text)cmd.Parameters["@filename"].Direction = ParameterDirection.Inputcmd.Parameters.Add("@blobdata", SqlDbType.Image)cmd.Parameters["@blobdata"].Direction = ParameterDirection.InputStore the byte array within the image field cmd.Parameters["@filename"].Value = fileNamecmd.Parameters["@blobdata"].Value = imageDataconn.Open()if (cmd.ExecuteNonQuery() == 1) { MessageBox.Show("Done")} } } } private void LoadImage(string fileName) { using (SqlConnection conn = new SqlConnection(connectionString)) { string sql = "select blobdata from Image where ImageFileName like @filename"Sqlmand cmd = new Sqlmand(sql, conn)cmd.Parameters.Add("@filename", SqlDbType.Text)cmd.Parameters["@filename"].Value = fileNameconn.Open()object objImage = cmd.ExecuteScalar()byte[] buffer = (byte[])objImageBinaryWriter bw = new BinaryWriter(new FileStream("C:\\abcd.", FileMode.Create))bw.Write(buffer)bw.Close()MemoryStream ms = new MemoryStream(buffer)Image bgImage = Image.FromStream(ms)ms.Close()this.BackgroundImage = bgImage} } ------------------------------------------------------------------PS:用空情报我踩踩空间,谢谢。

/kxl361。

4. 如何将文件的路径存入SQL数据库中去

建一个表:JpgFiles,其中至少包含一个列:JpgPath,用来存放绝对路径的字符串,所以这个列需要用varchar,长度假设为50,不够的话自己再增加。

string strPath = @"d:\baidu\up"

string[] fileNames = System.IO.Directory.GetFiles(strPath)

SqlConnection Cn=new SqlConnection(这里写你的连接串);

Sqlmand Cmd=new Sqlmand("Insert JpgFiles values (@JpgPath)",Cn)

Cmd.Parameters.Add("@JpgPath",SqlDbType.VarChar,50)

foreach (string strName in fileNames)

{

Cmd.Parameters[0].Value=strName

Cmd.ExecuteNoQuery()

}

5. 怎样将数据存入mysql数据库

MySQL命令行导出数据库:

1,进入MySQL目录下的bin文件夹:cd MySQL中到bin文件夹的目录

如我输入的命令行:cd C:\Program Files\MySQL\MySQL Server 4.1\bin

(或者直接将windows的环境变量path中添加该目录)

2,导出数据库:mysqldump -u 用户名 -p 数据库名 >; 导出的文件名

如我输入的命令行:mysqldump -u root -p news >news.sql (输入后会让你输入进入MySQL的密码)

(如果导出单张表的话在数据库名后面输入表名即可)

3、会看到文件news.sql自动生成到bin文件下

命令行导入数据库:

1,将要导入的.sql文件移至bin文件下,这样的路径比较方便

2,同上面导出的第1步

3,进入MySQL:mysql -u 用户名 -p

如我输入的命令行:mysql -u root -p (输入同样后会让你输入MySQL的密码)

4,在MySQL-Front中新建你要建的数据库,这时是空数据库,如新建一个名为news的目标数据库

5,输入:mysql>use 目标数据库名

如我输入的命令行:mysql>use news

6,导入文件:mysql>source 导入的文件名;

如我输入的命令行:mysql>source news.sql

6. 如何把文件存入到sql server 2008

1.MDF文件

在企业管理器中

右击数据库

点击所有任务

附加数据库

点三个点选择文件

选中U盘中的MDF文件确定即可

2.BAK等备份文件:

新建空数据库,取名最好为原数据库名.

右击新建的数据库

点所有任务

点还原数据库

点从设备

点选择设备

点添加

定位到U盘中您的备份的文件

确定

点选项

点在现有数据库上强制还原

点确定

等待

完成!

另外,站长团上有产品团购,便宜有保证

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

public class Cat

{

    public string Color { get set }

    public int Speed { get set }

    public string Name{ get set }

 }

//序列化

var cat1=new Cat{Color="Write",Speed=50,Name="MiMi" }

XmlSerializer ser = new XmlSerializer(typeof(Cat))

MemoryStream ms = new MemoryStream()

ser.Serialize(ms, cat1)

string xmlString = Encoding.UTF8.GetString(ms.ToArray())

//xmlString就是你要保存到数据库的字符串

//反序列化

XmlSerializer dser = new XmlSerializer(typeof(Cat))

//xmlString是你从数据库获取的字符串

Stream xmlStream = new MemoryStream(Encoding.UTF8.GetBytes(xmlString))

Cat cat2=dser.Deserialize(xmlStream) as Cat//cat2 就是你要得到的class对象


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

原文地址:https://54852.com/sjk/10029056.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存