Click here to Skip to main content
16,018,202 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I saved elements into string[] BOX, then pass it to access database with:

C#
query = "insert into myDbase ([Name]) values(@cell0)", BOX);
OleCon.Open();
OleCmd.Connection = OleCon;
oleCmd.CommandType = CommandType.Text;
oleCmd.CommandText = query;
OleCmd.Parameters.Add(new OleDbParameter(("@cell[0]"),OleDbType.VarChar)).Value = BOX;
OleCmd.ExecuteNonQuery();
OleCon.Close();

Successful! Then reading the BOX from dataBase with:

query = "select * from myDbase"
OleCon.Open();
OleCmd.Connection = OleCon;
OleCmd.CommandType = CommandType.Text;
OleCmd.CommandText = query;
OleAdpt.SelectCommand = OleCmd;
OleAdpt.SelectCommand.ExecuteNonQuery();
OleCon.Close();
OleAdpt.Fill(dtSet);
return dtSet.Tables[0];


The problem now is:

C#
(string)dtSet.Table[0].Rows[5][0];


gives me string value - System.String[]

How do I get my BOX back just the way I sent it in - i.e. Array of 10 elemets?
Thank you.
Posted
Updated 28-Dec-14 4:45am
v2

set the parameter as
C#
OleCmd.Parameters.Add(new OleDbParameter(("@cell[0]"),OleDbType.VarChar)).Value =string.Join(",", BOX);

then you can read it back as

C#
string[]results  = ((string)dtSet.Table[0].Rows[5][0]).Split(',');


if your strings contain "," character you better use different delimiter character
 
Share this answer
 
v2
Comments
George Jonsson 28-Dec-14 9:41am    
More elegant solution.
DamithSL 28-Dec-14 11:33am    
Thank you, George
Salisu Shaibu 28-Dec-14 11:37am    
Mr. Damith SL,
The output is "System.String[]" and no edit at all.
Thanks.
DamithSL 28-Dec-14 11:39am    
you may have forgot to set the value as "string.Join(",", BOX)"
Salisu Shaibu 28-Dec-14 11:38am    
Sory, I mean:
Mr. Damith SL,
The output is "System.String[]" and no edit of the database at all.
Thanks.
Well, it is most likely because the method BOX.ToString() is implicitly called in your query.
You can test what the code
C#
string s = BOX.ToString();
does.

If you really want to store an array of strings in one column in your database, which you really shouldn't, you need to loop through the array and save it into a string.

Example:
C#
StringBuilder boxString = new StringBuilder();
foreach (string s in BOX)
{
    boxString.Append(s);
    boxString.Append(",");   // [EDIT] Add a delimiter
}

string result = boxString.ToString().TrimEnd(',');


A much better solution would be to create a new table and store the strings in separate rows.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900