This may seem trivial but every example I’ve ever seen of ADO.net almost always has a ‘fill’ before an ‘update’. We really don’t want to fill a dataset that may have 1000’s of blobs, we just want to add(insert) to the table. Is fill required for an update?As an example here’s sample code from MSFT’s web site (we’re doing something similar):
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); // What does this even do?
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"); // are they really filling a dataset with all those images???
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();
You want the call to
da.Fill()in order to get the schema for theMyImagestable. That way you can use theDataRowreturned by the call toNewRow()with the correct schema (columns, keys, etc) when assigning values to the columns.You can tell the
SqlDataAdapterto return only the schema with no data by setting theSqlDataAdapter.FillCommandBehavior:You can alternatively use
da.FillSchema(), with either a singleDataTableor aDataSetif your query returns multiple tables.