使用什么数据类型,以便图像可以上传到SQL Server?

Jav*_*ier 7 c# sql-server-2005 visual-studio

Visual Studio,c#,SQL 2005服务器.我试图将.dbml表数据类型与我的.cs文件匹配.目标是允许将图像加载到数据库.到目前为止,它无法正常工作.问题似乎与文件类型相关FileContent列.我尝试了几种不同的变体,但都没有.

<Column Name="FileName" Type="System.String" DbType="NVarChar(100)" CanBeNull="true" />
<Column Name="FileType" Type="System.String" DbType="NVarChar(100)" CanBeNull="true" />
<Column Name="FileSize" Type="System.int32" DbType="int" CanBeNull="true" />
<Column Name="FileContent" Type="System.Data.Linq.Binary" DbType="varbinary(MAX)" CanBeNull="true" />
Run Code Online (Sandbox Code Playgroud)

SQL Server列
Applicant_PK(PK,int,notnull)
FileName(nvarchar(100),null)
FileType(nvarchar(100),null)
FileSize(int,null)
FileContent(varbinary(max),null)

 void CreatePreApplication()
{
    Pre_Application = new PreApplication();
    Pre_Application.FileName = Path.GetFileName(ctrFile.PostedFile.FileName);
    Pre_Application.FileType = ctrFile.PostedFile.ContentType;
    Pre_Application.FileSize = ctrFile.PostedFile.ContentLength;
    byte[] fileContent = new byte[ctrFile.PostedFile.ContentLength];
    ctrFile.PostedFile.InputStream.Read(fileContent, 0, ctrFile.PostedFile.ContentLength);
    Pre_Application.FileContent = fileContent;     

public class PreApplication
Run Code Online (Sandbox Code Playgroud)

{public int DatabaseId {get; 组; public String FileName {get; 组; public String FileType {get; 组; public int FileSize {get; 组; public byte [] FileContent {get; 组; public PreApplication()

 {
    PreApplicationsDataContext db =
        new PreApplicationsDataContext(
            "Data Source=THESQLSERVER;Initial Catalog=THECONNECTIONSTRING;Integrated Security=True");
    tblPreApplication preApp = new tblPreApplication();
    preApp.FileName = FileName;
    preApp.FileType = FileType;
    preApp.FileSize = FileSize;
    preApp.FileContent = (byte[])FileContent;
    try

    {
        db.tblPreApplications.InsertOnSubmit(preApp);
        db.SubmitChanges();
        DatabaseId = preApp.Applicant_PK;
        return preApp.Applicant_PK;
    }
    catch
    {
        DatabaseId = 0;
        return 0;   
    }        
}
Run Code Online (Sandbox Code Playgroud)

谢谢你看这个.我是编程的新手,所以如果你问我一个问题,请记住这一点.

JDM*_*DMX 1

我看到问题...您正在创建数据库连接并尝试插入构造函数。

你应该是这样定义的类

public PreApplication() {
}

public DoInsert {
  PreApplicationsDataContext db =
    new PreApplicationsDataContext("Data Source=THESQLSERVER;Initial Catalog=THECONNECTIONSTRING;Integrated Security=True");
  tblPreApplication preApp = new tblPreApplication();
  preApp.FileName = FileName;
  preApp.FileType = FileType;
  preApp.FileSize = FileSize;
  preApp.FileContent = (byte[])FileContent;
  try {
    db.tblPreApplications.InsertOnSubmit(preApp);
    db.SubmitChanges();
    DatabaseId = preApp.Applicant_PK;
    return preApp.Applicant_PK;
  } catch {
    DatabaseId = 0;
    return 0;   
  }  
}
Run Code Online (Sandbox Code Playgroud)

然后是你的执行函数

void CreatePreApplication() {
    Pre_Application p = new PreApplication();
    p.FileName = Path.GetFileName(ctrFile.PostedFile.FileName);
    p.FileType = ctrFile.PostedFile.ContentType;
    p.FileSize = ctrFile.PostedFile.ContentLength;
    byte[] fileContent = new byte[ctrFile.PostedFile.ContentLength];
    ctrFile.PostedFile.InputStream.Read(fileContent, 0, ctrFile.PostedFile.ContentLength);
    p.FileContent = fileContent;

    //do the insert after you have assigned all the variables
    p.DoInsert();
}
Run Code Online (Sandbox Code Playgroud)