private int WriteToDB(string strName, string strType, ref byte[] Buffer)
{
int nFileID = 0; // Create connection
OleDbConnection dbConn = new OleDbConnection(GetConnectionString()); // Create Adapter
OleDbDataAdapter dbAdapt = new OleDbDataAdapter("SELECT * FROM tblFile", dbConn);

// We need this to get an ID back from the database
dbAdapt.MissingSchemaAction = MissingSchemaAction.AddWithKey;

// Create and initialize CommandBuilder
OleDbCommandBuilder dbCB = new OleDbCommandBuilder(dbAdapt); // Open Connection
dbConn.Open();

// New DataSet
DataSet dbSet = new DataSet();

// Populate DataSet with data
dbAdapt.Fill(dbSet, "tblFile"); // Get reference to our table
DataTable dbTable = dbSet.Tables["tblFile"]; // Create new row
DataRow dbRow = dbTable.NewRow(); // Store data in the row
dbRow["FileName"] = strName;
dbRow["FileSize"] = Buffer.Length;
dbRow["ContentType"] = strType;
dbRow["FileData"] = Buffer; // Add row back to table
dbTable.Rows.Add(dbRow); // Update data source
dbAdapt.Update(dbSet, "tblFile"); // Get newFileID
if( !dbRow.IsNull("FileID") )
nFileID = (int)dbRow["FileID"];

// Close connection
dbConn.Close(); // Return FileID
return nFileID;
}

解决方案 »

  1.   

    把下面这些相应改为sql的就是了,把OleDb替换为Sql大概就可以了
    // Create connection
    OleDbConnection dbConn = new OleDbConnection(GetConnectionString());// Create Adapter
    OleDbDataAdapter dbAdapt = new OleDbDataAdapter("SELECT * FROM tblFile", dbConn);

    // We need this to get an ID back from the database
    dbAdapt.MissingSchemaAction = MissingSchemaAction.AddWithKey;

    // Create and initialize CommandBuilder
    OleDbCommandBuilder dbCB = new OleDbCommandBuilder(dbAdapt);
      

  2.   

    程序是大概明白了,但
    ref byte[] buffer
    的文件体的值,我如何赋给?文件一般都在客户端
      

  3.   

    dbAdapt.MissingSchemaAction = MissingSchemaAction.AddWithKey;这个是什么意思?
      

  4.   

    http://www.cndot.net/article/show.asp?id=776
    在ASP.NET中存取图片到数据库的示例
      

  5.   

    dbAdapt.MissingSchemaAction = MissingSchemaAction.AddWithKey;这个是什么意思?AddWithKey 添加必需的列和主键信息以完成架构。有关如何将主键信息添加到 DataTable 的更多信息,请参阅 FillSchema。为与 OLE DB .NET 数据提供程序一起正确运行,AddWithKey 要求本机 OLE DB 提供程序通过设置 DBPROP_UNIQUEROWS 属性获得必需的主键信息,然后通过检查 IColumnsRowset 中的 DBCOLUMN_KEYCOLUMN 确定哪些列是主键列。作为一种备选方法,用户可以在每个 DataTable 上显式设置主键约束。这将确保对与现有记录匹配的传入记录进行更新,而不是追加。当使用 AddWithKey 时,SQL Server .NET 数据提供程序将 FOR BROWSE 子句追加到正在被执行的语句。用户应该注意潜在的副作用,例如对 SET FMTONLY ON 语句的使用产生的干扰。有关更多信息,请参阅“SQL Server 联机图书”。 看了帮助,还是不明白!?
      

  6.   

    根据 baitianhai(hong) 提供的信息,上传下载已搞定,不过出了点问题首先代码如下:
    private void Page_Load(object sender, System.EventArgs e)
    {
    string DangAnNoparam=Request.Params["DangAnNo"];
      
    if (DangAnNoparam==null)
    {
    oaDA_DB DownLoadDA = new oaDA_DB();
    SqlDataReader DangAnFile = DownLoadDA.DownloadDangAnFile(DangAnNoparam);//qer test if (DangAnFile.Read())
    {
    Response.ContentType = DangAnFile.GetString(11);//设定输出文件类型
    Byte[] filebuffer = new Byte[DangAnFile.GetInt32(12)];
    DangAnFile.GetBytes(9,0,filebuffer,0,DangAnFile.GetInt32(12));
    Response.OutputStream.Write(filebuffer, 0, DangAnFile.GetInt32(12));  //输出文件二进制数制   
    Response.End();
    }
    else
    {
    this.Response.Write("此档案没有相关的附件!");
    } }
    else
    {
    this.Response.Write("没有提供档案编号,无法下载附件!");
    }

    // 在此处放置用户代码以初始化页面
    }--------------------------------------------------------------------
    问题是这样的,我在SQL库中保存了原用户上载时的文件名,下载给客户时,我仍然想用原用户的文件名,不知用什么方法设置?
      

  7.   

    ......
    Response.AddHeader("Content-Disposition", "attachment; filename=" + 文件名);
    Response.ContentType = DangAnFile.GetString(11);//设定输出文件类型
    .......