943,891 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 830
  • C# RSS
Aug 12th, 2009
0

Save array to DB

Expand Post »
Hi

my question is about saving array consisting of elements of type UIElement or Button. How can I store them in database and read them from db? Must I conert array to any other type(conversion from UIElement to Byte raises errors). What type column od db should have to store this array?

regards
Reputation Points: 10
Solved Threads: 0
Light Poster
krokodajl is offline Offline
31 posts
since May 2009
Aug 12th, 2009
0

Re: Save array to DB

Choose Image/varbinary/blob/ole column (field) datatype.
Write byte array,
c# Syntax (Toggle Plain Text)
  1. byte []b=.....
  2. System.Data.OleDb.OleDbConnection cn = new System.Data.OleDb.OleDbConnection();
  3. System.Data.OleDb.OleDbCommand cmd=new System.Data.OleDb.OleDbCommand("insert into tableName values (col1)", cn);
  4.  
  5. cmd.Parameters.Add("col1",System.Data.OleDb.OleDbType.Binary,b.Length,"col1");
  6. cmd.Parameters[0].Value= b;
  7.  
  8. cn.Open();
  9. cmd.ExecuteNonQuery();
  10. cn.Close();
Read,
c# Syntax (Toggle Plain Text)
  1. ...
  2. cmd=new System.Data.OleDb.OleDbCommand("select * from tableName",cn);
  3. System.Data.OleDb.OleDbDataReader dr;
  4. dr=cmd.ExecuteReader();
  5. while(dr.Read())
  6. {
  7. byte []t=(byte [])dr[0];
  8. ....
  9. }
  10. dr.Close();
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Aug 12th, 2009
0

Re: Save array to DB

thanks

but the problem is in converting array elements from UIElement to byte. .. It raises error
Reputation Points: 10
Solved Threads: 0
Light Poster
krokodajl is offline Offline
31 posts
since May 2009
Aug 12th, 2009
0

Re: Save array to DB

Type UIElement must be serializable. Use binary serialization.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Aug 12th, 2009
0

Re: Save array to DB

could you explain me how can I do that?
Reputation Points: 10
Solved Threads: 0
Light Poster
krokodajl is offline Offline
31 posts
since May 2009
Aug 12th, 2009
0

Re: Save array to DB

c# Syntax (Toggle Plain Text)
  1. [Serializable]
  2. public class Test
  3. {
  4.  
  5. }
  6. class MainA
  7. {
  8. static void Main()
  9. {
  10. Test a = new Test();
  11. System.IO.MemoryStream ms = new System.IO.MemoryStream();
  12. System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bs = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
  13. bs.Serialize(ms, a);
  14.  
  15. byte[] b = ms.ToArray();
  16. Console.WriteLine(b.Length);
  17. }
  18. }
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: How do i capture windows login name for windows service?
Next Thread in C# Forum Timeline: GUI C#





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC