0

我需要能够查看一个表并从该表中选择一行并为我正在处理的项目编辑该行中的一个字段。现在我只是使用一个概念验证程序来弄清楚它是如何工作的,这样我就可以将它实现到真实的东西中。所以我得到的第一个错误是服务器无法识别关键字“provider”

protected void ChangeButton_Click(object sender, EventArgs e)
{
    using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
    {
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = conn;
        cmd.CommandText = "UPDATE DevTesting SET testStr = @testStr, testStr2 = @testStr2, testStr3 = @testStr3, testNum = @testNum WHERE PersonID = @PersonID";
        cmd.CommandType = CommandType.Text;


        string strPersonID = GridView1.Rows[1].Cells[1].Text;
        string strTestStr1 = "aerhtsm";   
        cmd.Parameters.Add("@testStr", SqlDbType.NVarChar, 50).Value = strTestStr1;
        conn.Open();
        cmd.ExecuteNonQuery();
    }

    GridView1.EditIndex = -1;
    BindGridView(); 

}

private void BindGridView()
{

    using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLServer2005DBConnectionString"].ToString()))
    {
        DataSet grid1 = new DataSet();
        string strSelectCmd = "SELECT testStr, testStr2, testStr3, testNum FROM GridView1";
        SqlDataAdapter da = new SqlDataAdapter(strSelectCmd, conn);
        conn.Open();
        da.Fill(grid1, "GridView1");
        DataView dvPerson = grid1.Tables["GridView1"].DefaultView;
        GridView1.DataSource = grid1;
        GridView1.DataBind();
    }
}

我得到的另一个错误是当代码到达conn.Open();时连接一直超时。在 changeButton 方法中。有人可以解释我做错了什么。这是我的 web.config 文件的副本,以备不时之需。

     <?xml version="1.0" encoding="utf-8"?>

    <configuration>
   <configSections>

   <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"  requirePermission="false" />
   </configSections>
   <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-WebApplication11-20130731142716;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-WebApplication11-20130731142716.mdf"
  providerName="System.Data.SqlClient" />
     <add name="TestConnectionString" connectionString="Provider=SQLNCLI11;Data Source=HQSQL;Integrated Security=SSPI;Initial Catalog=BG_BACKUP"
  providerName="System.Data.OleDb" />
      <add name="ConnectionString" connectionString="Provider=SQLNCLI11;Data Source=HQSQL;Integrated Security=SSPI;Initial Catalog=BG_BACKUP"
  providerName="System.Data.OleDb" />
     </connectionStrings>
      <system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
<pages>
  <namespaces>
    <add namespace="System.Web.Optimization" />
  </namespaces>
  <controls>
    <add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt" />
  </controls>
</pages>
<authentication mode="Forms">
  <forms loginUrl="~/Account/Login.aspx" timeout="2880" />
</authentication>
<profile defaultProvider="DefaultProfileProvider">
  <providers>
    <add name="DefaultProfileProvider" type="System.Web.Providers.DefaultProfileProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" />
  </providers>
</profile>
<membership defaultProvider="DefaultMembershipProvider">
  <providers>
    <add name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
  </providers>
</membership>
<roleManager defaultProvider="DefaultRoleProvider">
  <providers>
    <add name="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" />
  </providers>
</roleManager>
<sessionState mode="InProc" customProvider="DefaultSessionProvider">
  <providers>
    <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" />
  </providers>
</sessionState>
  </system.web>
 <entityFramework>
  <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
       <parameters>
     <parameter value="v11.0" />
     </parameters>
      </defaultConnectionFactory>
    </entityFramework>
      </configuration>

好的,这是我按下按钮覆盖数据表的一部分时得到的错误图像。我试图自己解决解决方案,并在第二张图片中遇到另一个类似的错误。

4

1 回答 1

0

web.config 的 connectionString 部分中的ConnectionString键用于 anSqlClient.SqlConnection但您指定使用providerName="System.Data.OleDb". 您应该更改为providerName="System.Data.SqlClient"The OleDb client cannot understand that syntax。

然后 UPDATE 命令使用 5 个参数,但您只向参数集合添加了一个。这将是另一个错误,在打开连接之后。

然后在 BindGridView 中你使用了一个没有在任何地方定义的连接字符串,最后这里的 SELECT 语句引用了一个名为 GridView 的表,我怀疑你打错了正确的表名(更新命令中使用的那个叫做DevTesting

对于超时问题,您应该添加连接到超时的异常消息

于 2013-07-31T22:28:02.917 回答