【问题标题】:ASP.NET local SQL Server database C# gridview data binding Visual Studio 2013ASP.NET 本地 SQL Server 数据库 C# gridview 数据绑定 Visual Studio 2013
【发布时间】:2015-01-05 04:34:55
【问题描述】:

我需要一些帮助,因为我一直在尝试不同的事情,但似乎没有任何工作正常,问题本身就是下面的问题。

如何使用 C# 背后的代码将数据绑定到 Visual Studio 2013 中具有本地 SQL Server 数据库的网格视图?

这是我正在尝试的 C#:

protected void Page_Load(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Guillermo\Desktop\HorsensHospital\App_Data\HospitalDB.mdf;Integrated Security=True;Connect Timeout=30");
    con.Open();

    SqlCommand cmd = new SqlCommand("SELECT [Task_temp_name], [Task_templatesID] FROM [Task_templates]", con);

    SqlDataAdapter sda = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();

    GridView1.DataSource = ds;
    GridView1.DataBind();

    cmd.ExecuteNonQuery();

    con.Close();
}

【问题讨论】:

  • 您是否遇到任何错误

标签: c# sql visual-studio-2012 gridview local-database


【解决方案1】:

您只需将空的 DataSet 分配给 Gridview 的 DataSource

您需要使用.Fill method 填写您的DataSetSqlDataAdapter。你不需要使用ExecuteNonQuery。此方法仅执行您的查询。它不会返回任何数据或结果。

还可以使用using statement 来处理您的SqlConnectionSqlCommandSqlDataAdapter。使用时不需要.Close()您的数据库连接和对象。

using(SqlConnection con = new SqlConnection(conString))
using(SqlCommand cmd = con.CreateCommand())
{
    cmd.CommandText = "SELECT [Task_temp_name], [Task_templatesID] FROM [Task_templates]";
    using(SqlDataAdapter sda = new SqlDataAdapter(cmd))
    {
         DataSet ds = new DataSet();
         sda.Fill(ds);
         GridView1.DataSource = ds;
         GridView1.DataBind();
    }
}

【讨论】:

    【解决方案2】:

    你应该使用这样的语句: 当您使用 SqlDataAdapter 时,您应该使用 Fill 方法填充数据集 然后设置GridView的DataSource属性

        SqlCommand cmd = new SqlCommand("SELECT [Task_temp_name], [Task_templatesID] FROM     [Task_templates]", con);
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        sda.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
        con.Close();
    

    【讨论】:

      【解决方案3】:

      您错过了 Fill 填充数据集的方法,这就是我认为您无法显示 gridview 的原因。

      Fill 方法是非常重要的方法 Bcoz 无论您的查询返回什么,都应将其填充到您的数据集中,然后该数据集由您的 Gridiview 绑定 希望对你有帮助

      protected void Page_Load(object sender, EventArgs e)
      {
              SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Guillermo\Desktop\HorsensHospital\App_Data\HospitalDB.mdf;Integrated Security=True;Connect Timeout=30");
              con.Open();
              SqlCommand cmd = new SqlCommand("SELECT [Task_temp_name], [Task_templatesID] FROM [Task_templates]", con);
              SqlDataAdapter sda = new SqlDataAdapter(cmd);
              DataSet ds = new DataSet();
              sda.Fill(ds);
              GridView1.DataSource = ds;
              GridView1.DataBind();
              cmd.ExecuteNonQuery();
              con.Close();
      

      }

      注意:您需要在 Web.Config 中创建连接字符串。 Bcoz 如果您在那里创建了连接字符串,那么您将不需要再次创建它。在那里创建后,您只需要从Web.config 调用它

      SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringNameFromWebConfig"].ConnectionString);
      

      这是从 web.config 调用连接字符串的语法

      【讨论】:

        猜你喜欢
        • 2014-03-11
        • 1970-01-01
        • 2021-08-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多