关键字不受支持:'provider'

3
我遇到了这个错误:
关键字不支持:'provider'。
描述:在当前web请求执行期间发生了一个未处理的异常。请查看堆栈跟踪以获取有关错误的更多信息以及代码中的源位置。
异常详细信息:System.ArgumentException:关键字不受支持:'provider'。
源错误:
Line 24:     {
Line 25:         Session["id"] = e.CommandArgument.ToString();
Line 26:         SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
Line 27:            con.Open();
Line 28:             SqlCommand cmd1 = new SqlCommand("INSERT INTO tb2 (id, name) SELECT id, name FROM tb1 where id='"+Session["id"].ToString()+"'", con);

Source File: c:\inetpub\wwwroot\logon\page.aspx    Line: 26 

以下是完整代码:

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

<script runat="server" type="css">

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        bind();
    }
}
protected void bind()
{
    PendingRecordsGridview.DataSourceID = "";
    PendingRecordsGridview.DataSource = sd1;
    PendingRecordsGridview.DataBind();
 }
protected void PendingRecordsGridview_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "accept")
    {
        Session["id"] = e.CommandArgument.ToString();
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            con.Open();
            SqlCommand cmd1 = new SqlCommand("INSERT INTO tb2 (id, name) SELECT id, name FROM tb1 where id='"+Session["id"].ToString()+"'", con);
            SqlCommand cmd2 = new SqlCommand("delete from tb1 where id='"+Session["id"].ToString()+"'", con);
            cmd1.ExecuteNonQuery();
            cmd2.ExecuteNonQuery();
            bind();
    }
}
</script>
<form id="form1" runat="server">
<asp:GridView ID="PendingRecordsGridview" runat="server" AutoGenerateColumns="False" DataKeyNames="id" onrowcommand="PendingRecordsGridview_RowCommand" DataSourceID="sd1">
        <Columns>
            <asp:templatefield HeaderText="Accept">
                <ItemTemplate>
                    <asp:Button CommandArgument='<%# Bind("id") %>' ID="Button1" runat="server" CausesValidation="false" CommandName="accept" Text="Accept" />
                </ItemTemplate>
            </asp:templatefield>
            <asp:templatefield HeaderText="name" SortExpression="name">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("name") %>'>
                    </asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("name") %>'>
                    </asp:Label>
                </ItemTemplate>
            </asp:templatefield>
            <asp:templatefield HeaderText="id" SortExpression="id">
                <EditItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("id") %>'>
                    </asp:Label>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("id") %>'>
                    </asp:Label>
                </ItemTemplate>
            </asp:templatefield>
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="sd1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
        SelectCommand="SELECT * FROM [tb1]" DeleteCommand="DELETE FROM [tb1] WHERE [id] = ?" InsertCommand="INSERT INTO [tb1] ([name]) VALUES (?)"  UpdateCommand="UPDATE [tb1] SET [name] = ? WHERE [id] = ?">
        <DeleteParameters>
            <asp:parameter Name="id" Type="Int32" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:parameter Name="name" Type="String" />
            <asp:parameter Name="id" Type="Int32" />
        </UpdateParameters>
        <InsertParameters>
            <asp:parameter Name="name" Type="String" />
        </InsertParameters>
</asp:SqlDataSource>
</form>       

Web.config

<configuration>
    <connectionStrings>

        <add name="ConnectionString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\inetpub\wwwroot\logon\_private\db1.mdb"
            providerName="System.Data.OleDb" />
    </connectionStrings>
</configuration>

请帮忙!谢谢!

这里是需要翻译的内容。

3
像这样使用字符串拼接,你的代码容易受到SQL注入攻击。 - Oded
2
@PetersonPilares 不要紧......将会生成一个 web.config。你甚至在你的代码中引用它 "configurationManager.ConnectionString"。 - scartag
1
那么,你的连接字符串从哪里来呢?我相信这就是@scartag正在寻找的(实际的连接字符串)。 - Oded
1
@PetersonPilares,请查看项目文件夹。 - scartag
我编辑了我的帖子,请检查我的web.config文件。 - Peterson Pilares
显示剩余4条评论
1个回答

3
看起来您正在尝试使用SQL Server连接对象访问Access数据库。(连接配置是指Jet数据库引擎)
您应该使用OleDbConnection(以及相关的OleDbCommand等)。
有关连接字符串的更多信息,请参见:http://connectionstrings.com/access 并且,正如评论中提到的那样,您的代码容易受到SQL注入攻击。您可能需要阅读如何保护自己免受SQL注入攻击(该文章适用于SQL Server,但许多概念也适用于Access)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接