C#中如何保存float类型的数组到SQL Server的image字段中(300分)

  • 主题发起人 主题发起人 HunterTeam
  • 开始时间 开始时间
H

HunterTeam

Unregistered / Unconfirmed
GUEST, unregistred user!
保存后又该如何读出?
 
写入
SqlConnection con = new SqlConnection("Server=Darkover;uid=<username>;pwd=<strong password>;database=northwind");
SqlDataAdapter da = new SqlDataAdapter("Select * From MyImages", con);
SqlCommandBuilder MyCB = new SqlCommandBuilder(da);
DataSet ds = new DataSet("MyImages");
da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
FileStream fs = new FileStream(@"C:/winnt/Gone Fishing.BMP", FileMode.OpenOrCreate, FileAccess.Read);

byte[] MyData= new byte[fs.Length];
fs.Read(MyData, 0, System.Convert.ToInt32(fs.Length));

fs.Close();

da.Fill(ds,"MyImages");

DataRow myRow;
myRow=ds.Tables["MyImages"].NewRow();
myRow["Description"] = "This would be description text";
myRow["imgField"] = MyData;
ds.Tables["MyImages"].Rows.Add(myRow);
da.Update(ds, "MyImages");
con.Close();
读出
SqlConnection con = new SqlConnection("Server=Darkover;uid=<username>;pwd=<strong password>;database=northwind");
SqlDataAdapter da = new SqlDataAdapter("Select * From MyImages", con);
SqlCommandBuilder MyCB = new SqlCommandBuilder(da);
DataSet ds = new DataSet("MyImages");
byte[] MyData= new byte[0];

da.Fill(ds, "MyImages");
DataRow myRow;
myRow=ds.Tables["MyImages"].Rows[0];

MyData = (byte[])myRow["imgField"];
int ArraySize = new int();
ArraySize = MyData.GetUpperBound(0);

FileStream fs = new FileStream(@"C:/winnt/Gone Fishing2.BMP", FileMode.OpenOrCreate, FileAccess.Write);
fs.Write(MyData, 0,ArraySize);
fs.Close();
}
原理是一样的,你看一下吧
 
to Corinth:你说的方法我早已知道,文件保存到image中没有问题。问题是float数组无法保存,而byte数组是可以直接保存的,涉及到C#中这两者如何转换的问题。
 
DataRow dr;
dr = dataSet1.Tables["AA01"].NewRow();
float[] data = new float[2];
data[0] = 1.2F;
data[1] = 2.1F;
dr["ZA0100"] = "111111111111";
dr["ZA9998"] = data;
dataSet1.Tables["AA01"].Rows.Add(dr);
float[] d = new float[2];
DataRow[] dr1 = dataSet1.Tables["AA01"].Select("za0100 = '111111111111'");
MessageBox.Show(dr1.Length.ToString());
d = (float[])dr1[0]["za9998"];
MessageBox.Show(d[1].ToString());//显示2.1
MessageBox.Show(d[0].ToString());
我已经试过了,可以的
 
保存呢?上面的代码我也试过可以,但是保存不到数据库,你试过吗?
 
后退
顶部