如何在不变性的情况下从访问数据库中读取数组。 [英] How to read Array from access dataBase without denature.

查看:77
本文介绍了如何在不变性的情况下从访问数据库中读取数组。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将元素保存到string [] BOX中,然后将其传递给访问数据库:



 query =  插入myDbase([Name])值(@ 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();

成功!然后从 dataBase读取BOX

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 ];





现在的问题是:



字符串)dtSet.Table [ 0 ]。行[ 5 ] [< span class =code-digit> 0 ]; 





给我字符串值 - System.String []



如何按照我发送的方式取回我的BOX - 即10个元素的数组?

谢谢。

解决方案

将参数设置为

 OleCmd.Parameters.Add( new  OleDbParameter((  @ cell [0]),OleDbType.VarChar)) .Value =  string  .Join( ,BOX); 



然后你可以把它读回来



  string  [] results =(( string )dtSet.Table [ 0 ]。行[ 5 ] [ 0 ])。分割(' ,'); 





如果你的字符串包含字符你最好使用不同的分隔符字符


嗯,很可能是因为你的查询中隐式调用了方法BOX.ToString()。

你可以测试代码

  string  s = BOX.ToString(); 

确实。



如果你真的想在数据库的一列中存储一个字符串数组,你真的不应该这样做,你需要遍历数组并将其保存到一个字符串中。



示例:

 StringBuilder boxString =  new  StringBuilder(); 
foreach string s in BOX)
{
boxString.Append(s);
boxString.Append( ); // 添加分隔符
}

< span class =code-keyword> string result = boxString.ToString()。TrimEnd(' , ');





更好的解决方案是创建一个新表并将字符串存储在不同的行中。


I saved elements into string[] BOX, then pass it to access database with:

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:

(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.

解决方案

set the parameter as

OleCmd.Parameters.Add(new OleDbParameter(("@cell[0]"),OleDbType.VarChar)).Value =string.Join(",", BOX);


then you can read it back as

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



if your strings contain "," character you better use different delimiter character


Well, it is most likely because the method BOX.ToString() is implicitly called in your query.
You can test what the code

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:

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.


这篇关于如何在不变性的情况下从访问数据库中读取数组。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆