我的問題是用SQL Server數據庫。如何將大文件存儲到SQL Server 2008中並檢索所有文件?
我成功地存儲數據到SQL Server表在C#中使用此代碼:
internal static void SaveAutocadFileOnDataBase(string MapCode, byte[] dataElecMap, string dataElecMapName,byte[] dataCivilMap, string dataCivilMapName, byte[] dataArchiMap, string dataArchiMapName,byte[]dataMechaMap,string dataMechaMapName)
{
string sqlcommand = string.Concat("INSERT INTO AutoCadFiles VALUES ('", MapCode, "' , ", "cast ('", dataElecMap, "' as varbinary(max)),", " '", dataElecMapName, "' ", " , cast ('", dataCivilMap, "' as varbinary(max)) ,", " '", dataCivilMapName, "' ", " , cast ('", dataArchiMap, "' as varbinary(max)) ,", " '", dataArchiMapName, "' "," , cast ('", dataMechaMap, "' as varbinary(max)) ,", " '", dataMechaMapName, "' ", ")"); ExecuteNonQuery(sqlcommand);
}
,並符合本規則進行檢索:
internal static DataTable RetriveMaps(string MapNumber, string MapType)
{
string s = MapType + "Name";
string SqlCommand = string.Concat("SELECT ", MapType, " , ", s, " FROM AutoCadFiles WHERE MapCode='",MapNumber, "'");
return GetBinaryFiles(SqlCommand);
}
private static DataTable GetBinaryFiles(string SqlCommand)
{
SqlConnection SqlConnction = null;
DataTable dt = new DataTable();
SqlCommand sqlcom = GetConnection(SqlCommand, ref SqlConnction);
SqlDataReader sdldr = sqlcom.ExecuteReader();
dt.Load(sdldr);
return dt;
}
我成功地從dataTable中獲取文件並將其保存到文件系統。但存儲的文件量爲0字節
或者13字節比數據庫中存儲的文件小得多。
阿巴斯,你在這裏丟失代碼..?你在哪裏將它保存到'FileStream' ..? – MethodMan
您應該[在您的代碼中閱讀如何避免** SQL注入攻擊**](http://www.a2zdotnet.com/View.aspx?Id=64#.URKpR2d9uK8)!您目前非常脆弱...... –
「我成功存儲了數據」您怎麼知道? (這不成功)。你甚至看過文件的內容嗎?閱讀關於如何做到這一點的教程。 – usr