2011-01-22 7 views
0

我想將我的asp.net Formview鏈接到MySQL數據庫,但沒有成功。如何在ASP.NET中查詢MySQL?

看看我的代碼,並請你告訴我哪裏是我的錯誤:

<asp:FormView ID="FormView1" runat="server" DataKeyNames="ImageID" 
     DataSourceID="SqlDataSource1" EnableModelValidation="True"> 
     <EditItemTemplate> 
      ImageID: 
      <asp:Label ID="ImageIDLabel1" runat="server" Text='<%# Eval("ImageID") %>' /> 
      <br /> 
      Title: 
      <asp:TextBox ID="TitleTextBox" runat="server" Text='<%# Bind("Title") %>' /> 
      <br /> 
      Description: 
      <asp:TextBox ID="DescriptionTextBox" runat="server" 
       Text='<%# Bind("Description") %>' /> 
      <br /> 
      <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" 
       CommandName="Update" Text="Update" /> 
      &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server" 
       CausesValidation="False" CommandName="Cancel" Text="Cancel" /> 
     </EditItemTemplate> 
     <InsertItemTemplate> 
      Title: 
      <asp:TextBox ID="TitleTextBox" runat="server" Text='<%# Bind("Title") %>' /> 
      <br /> 
      Description: 
      <asp:TextBox ID="DescriptionTextBox" runat="server" 
       Text='<%# Bind("Description") %>' /> 
      <br /> 
      <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" 
       CommandName="Insert" Text="Insert" /> 
      &nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server" 
       CausesValidation="False" CommandName="Cancel" Text="Cancel" /> 
     </InsertItemTemplate> 
     <ItemTemplate> 
      ImageID: 
      <asp:Label ID="ImageIDLabel" runat="server" Text='<%# Eval("ImageID") %>' /> 
      <br /> 
      Title: 
      <asp:Label ID="TitleLabel" runat="server" Text='<%# Bind("Title") %>' /> 
      <br /> 
      Description: 
      <asp:Label ID="DescriptionLabel" runat="server" 
       Text='<%# Bind("Description") %>' /> 
      <br /> 
      <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" 
       CommandName="Edit" Text="Edit" /> 
      &nbsp;<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" 
       CommandName="Delete" Text="Delete" /> 
      &nbsp;<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" 
       CommandName="New" Text="New" /> 
     </ItemTemplate> 
    </asp:FormView> 
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
     ConnectionString="<%$ ConnectionStrings:cmsConnectionString %>" 
     InsertCommand="INSERT INTO [tbimages] ([ImageID], [Title], [Description]) VALUES (ImageID, Title, Description)" 
     ProviderName="<%$ ConnectionStrings:cmsConnectionString.ProviderName %>" 
     SelectCommand="SELECT ImageID, Title, Description FROM tbimages" 
     UpdateCommand="UPDATE [tbimages] SET [Title] = ?, [Description] = ? WHERE [ImageID] = ?"> 

     <InsertParameters> 
      <asp:Parameter Name="ImageID" Type="Int64" /> 
      <asp:Parameter Name="Title" Type="String" /> 
      <asp:Parameter Name="Description" Type="String" /> 
     </InsertParameters> 
     <UpdateParameters> 
      <asp:Parameter Name="Title" Type="String" /> 
      <asp:Parameter Name="Description" Type="String" /> 
      <asp:Parameter Name="ImageID" Type="Int64" /> 
     </UpdateParameters> 
    </asp:SqlDataSource> 

我已經在web配置定義我的連接字符串:

<connectionStrings> 
    <add name="cmsConnectionString" connectionString="server=localhost;User Id=root;password=123;database=cms" providerName="MySql.Data.MySqlClient"/> 
</connectionStrings> 

請,如果您有任何想法解決這個讓我知道。

+0

錯誤消息是什麼?和哪裏? – user492238 2011-01-22 11:15:06

+0

錯誤在插入。 它給我所有的東西,但是當我試圖插入新記錄時,它給了我這個錯誤: 你的SQL語法錯誤;檢查與您的MySQL服務器版本相對應的手冊,以在[[tbimages]([ImageID],[Title],[Description])附近使用正確的語法。VALUE(第1行的ImageID,Title,Descripti) – HAJJAJ 2011-01-22 11:21:24

回答

1

您可能希望改變插入命令,像這樣

InsertCommand="INSERT INTO [tbimages] ([ImageID], [Title], [Description]) VALUES (? , ? , ?)" 

更新:

InsertCommand="INSERT INTO [tbimages] ([ImageID], [Title], [Description]) 
VALUES (?ImageID , ?Title , ?Description)" 
+0

我試過這個之前,它給了我另一個錯誤: 參數'?'必須定義!!! – HAJJAJ 2011-01-22 11:29:58