2013-01-08 190 views
0

我創建了一個將上傳圖像的頁面。當我點擊「上傳」時,沒有錯誤,這就是爲什麼我認爲圖像已經存儲在我的數據庫中。但沒有成功。數據庫沒有更新......數據庫未更新

這裏是我的代碼:

<%@ Page Language="C#" Debug="true" %> 
<%@ Import Namespace="System" %> 
<%@ Import Namespace="System.Data" %> 
<%@ Import Namespace = "System.Data.SqlClient" %> 
<%@ Import Namespace= "System.Data.OleDb" %> 


<script runat="server" type="text/c#"> 
protected void btnUpload_Click(object sender, EventArgs e) 


{ 
    { 
    byte[] imageSize = new byte 
       [FileUpload1.PostedFile.ContentLength]; 
    HttpPostedFile uploadedImage = FileUpload1.PostedFile; 
    uploadedImage.InputStream.Read 
    (imageSize, 0, (int)FileUpload1.PostedFile.ContentLength); 
// Create SQL Connection 
    OleDbConnection con = new OleDbConnection(); 
    con.ConnectionString = ConfigurationManager.ConnectionStrings["recordsConnectionString"].ConnectionString; 
// Create SQL Command 
OleDbCommand cmd = new OleDbCommand(); 
cmd.CommandText = "INSERT INTO PendingRecords(Image)" + 
        " VALUES (@Image)"; 
cmd.CommandType = CommandType.Text; 
cmd.Connection = con; 

OleDbParameter UploadedImage = new OleDbParameter 
       ("@Image", OleDbType.VarBinary, imageSize.Length); 

UploadedImage.Value = imageSize; 
cmd.Parameters.Add(UploadedImage); 
con.Open(); 
GridView1.DataSourceID = ""; 
GridView1.DataSource = SqlDataSource1; 
GridView1.DataBind(); 
con.Close(); 
} 
} 
</script> 

<form id="form1" runat="server"> 
<div> 
<asp:FileUpload ID="FileUpload1" runat="server"/> 
<asp:Label ID="lblMessage" runat="server"> 
</asp:Label> 
<asp:Button ID="btnUpload" runat="server" 
      OnClick="btnUpload_Click" Text="Upload"/> 
</div> 
<asp:GridView runat="server" id="GridView1" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" DataKeyNames="ID"> 
    <Columns> 
     <asp:boundfield DataField="ID" HeaderText="ID" ReadOnly="True" InsertVisible="False" SortExpression="ID"> 
     </asp:boundfield> 
     <asp:boundfield DataField="Image" HeaderText="Image" SortExpression="Image"> 
     </asp:boundfield> 
    </Columns> 
</asp:GridView> 
<asp:SqlDataSource runat="server" id="SqlDataSource1" ProviderName="<%$ ConnectionStrings:recordsConnectionString.ProviderName %>" ConnectionString="<%$ ConnectionStrings:recordsConnectionString %>" SelectCommand="SELECT * FROM [PendingRecords]" DeleteCommand="DELETE FROM [PendingRecords] WHERE [ID] = ?" InsertCommand="INSERT INTO [PendingRecords] ([Username], [Password], [FirstName], [LastName], [MiddleName], [Address], [Gender], [ContactNumber], [PlateNumber], [Color], [Brand], [LiscensedNumber]) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" UpdateCommand="UPDATE [PendingRecords] SET [Username] = ?, [Password] = ?, [FirstName] = ?, [LastName] = ?, [MiddleName] = ?, [Address] = ?, [Gender] = ?, [ContactNumber] = ?, [PlateNumber] = ?, [Color] = ?, [Brand] = ?, [LiscensedNumber] = ? WHERE [ID] = ?"> 
    <DeleteParameters> 
     <asp:parameter Name="ID" Type="Int32" /> 
    </DeleteParameters> 
    <UpdateParameters> 
     <asp:parameter Name="Username" Type="String" /> 
     <asp:parameter Name="Password" Type="String" /> 
     <asp:parameter Name="FirstName" Type="String" /> 
     <asp:parameter Name="LastName" Type="String" /> 
     <asp:parameter Name="MiddleName" Type="String" /> 
     <asp:parameter Name="Address" Type="String" /> 
     <asp:parameter Name="Gender" Type="String" /> 
     <asp:parameter Name="ContactNumber" Type="String" /> 
     <asp:parameter Name="PlateNumber" Type="String" /> 
     <asp:parameter Name="Color" Type="String" /> 
     <asp:parameter Name="Brand" Type="String" /> 
     <asp:parameter Name="LiscensedNumber" Type="String" /> 
     <asp:parameter Name="ID" Type="Int32" /> 
    </UpdateParameters> 
    <InsertParameters> 
     <asp:parameter Name="Username" Type="String" /> 
     <asp:parameter Name="Password" Type="String" /> 
     <asp:parameter Name="FirstName" Type="String" /> 
     <asp:parameter Name="LastName" Type="String" /> 
     <asp:parameter Name="MiddleName" Type="String" /> 
     <asp:parameter Name="Address" Type="String" /> 
     <asp:parameter Name="Gender" Type="String" /> 
     <asp:parameter Name="ContactNumber" Type="String" /> 
     <asp:parameter Name="PlateNumber" Type="String" /> 
     <asp:parameter Name="Color" Type="String" /> 
     <asp:parameter Name="Brand" Type="String" /> 
     <asp:parameter Name="LiscensedNumber" Type="String" /> 
    </InsertParameters> 
</asp:SqlDataSource> 
</form> 

回答

1

你有沒有執行的創建與參數查詢?按我發現你缺少下面要做的事:

cmd.Parameters.Add(UploadedImage); 
con.Open(); 
cmd.ExceuteNonQuery(); /// you forgot this here.. 
GridView1.DataSourceID = ""; 

從@Remou評論:

圖片是一個保留字,所以加上括號「INSERT INTO PendingRecords([圖片])

+0

謝謝,是的,我忘記了這一行,但我收到了新的錯誤「INSERT INTO語句中的語法錯誤。」異常詳細信息:System.Data.OleDb.OleDbException:INSERT INTO語句中的語法錯誤。我的代碼:)請 –

+0

圖像是[保留字](http://support.microsoft.com/kb/321266),所以添加括號'「INSERT INTO PendingRecords([Image])」' – Fionnuala