2015-11-05 63 views
0

我需要在我的网站的几个页面上的SQL Server数据库中的视图中显示一些数据。我已通过在我的web.config内使用SqlDataSource,然后在我的.aspx页面内使用GridView控件来显示数据。更好的解决SqlDataSource?

现在,这个工程,但我读了一些论坛,这是不好的做法,使用SqlDataSource?我可能需要有管理员/用户在将来过滤数据的能力,所以我不确定这将如何与我目前的实施工作。

到目前为止我的代码看起来是这样的:

web.config文件:

<connectionStrings> 
    <add name="Test1.ConnectionString" 
     connectionString="Data Source=...." 
     providerName="System.Data.SqlClient" /> 
</connectionStrings> 
<system.web> 
    <compilation debug="true" targetFramework="4.5" /> 
    <httpRuntime targetFramework="4.5" /> 
</system.web> 

而这样的事情在我的aspx

<body id="wrap" > 
    <form runat="server"> 
    <asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" 
     BackColor="White" BorderColor="#CCCCCC" 
     BorderStyle="None" BorderWidth="1px" CellPadding="3" 
     DataSourceID="SqlDataSource1" Height="500px" Width="408px"> 
     <Columns> 
      <asp:BoundField DataField="Title" HeaderText="Title" ReadOnly="True" SortExpression="Title"> 
       <ItemStyle Width="400px" HorizontalAlign="Center" Height="100px" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" /> 
      </asp:BoundField> 
      <asp:BoundField DataField="Result" HeaderText="Result" ReadOnly="True" SortExpression="Result" > 
       <ItemStyle HorizontalAlign="Center" Height="100px" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" /> 
      </asp:BoundField> 
     </Columns> 
     <FooterStyle BackColor="White" ForeColor="#002756" /> 
     <HeaderStyle BackColor="#003466" Font-Bold="True" ForeColor="White" /> 
     <PagerStyle BackColor="White" ForeColor="#002756" HorizontalAlign="Left" /> 
     <RowStyle ForeColor="#002756" /> 
    </asp:GridView> 
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
     ConnectionString="<%$ ConnectionStrings: Test1.ConnectionString %>" 
     SelectCommand="SELECT * FROM [Test1.ConnectionString]"> 
    </asp:SqlDataSource> 
    </form> 
</body> 

所以我的问题是是否有更好的请记住,我可能需要一个用户/管理员按照某些标准过滤数据的功能?

+0

您没有使用代码来获取数据的任何特定原因? – Izzy

+0

我来自一个PHP背景,对于asp.net来说还很新,所以仍然试图让我的头在这附近。但我想这会是更好的解决方案? – MariaL

回答

1

正如我在评论中提及,我会建议你用后面的代码实现这一目标,因为它是容易得多,如果将来需要进行更改。我假设你的数据库中有一个名为Test1的表。我们首先创建一个类来在C#中表示这个类,然后添加一些我们稍后会用到的属性。

public class Test 
{ 
    public string Title { get; set; } 
    public int Result { get; set; } 
} 

现在让我们创建一个从数据库返回值集合的方法。

public List<Test> GetData() 
{ 
    List<Test> myList = new List<Test>(); 

    string sqlQuery = "Select Title, Result From Test"; 
    string connectionString = ConfigurationManager.ConnectionStrings["Test1.ConnectionString"].ConnectionString; //Read connection string from config file 

    using (var con = new SqlConnection(connectionString)) 
    { 
     using (var cmd = new SqlCommand(sqlQuery, con)) 
     { 
      //Add param here if required. 
      con.Open(); //Open connection 
      using (var reader = cmd.ExecuteReader()) 
      { 
       while (reader.Read()) 
       { 
        Test t = new Test(); 
        t.Title = reader["Title"].ToString(); 
        t.Result = Convert.ToInt32(reader["Result"]); 

        myList.Add(t); 
       } 
      } 
     } 
    } 
    return myList; 
} 

最后,您要设置GridView的数据源。我会假设你有一个名为MyGridPage.aspx页面开辟MyGridPage.asps.cs并在Page_Load事件中,你可以设置DataSource为网格为:

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
     Test t = new Test(); 
     GridView1.DataSource = t.GetData(); 
     GridView1.DataBind(); 
    } 
} 

如果你想通过例如用户名来过滤Sql查询你会改变它:

string sqlQuery = "Select Title, Result From Test Where username = @username"; 

然后你就可以在@username参数传递为:

cmd.Parameters.Add("@username", SqlDbType.NVarChar).Value = myUsername; 

myUsername可以是登录到您的应用的人。在打开连接之前,您将传递参数。传递参数也将防止SQL注入,我建议你阅读以防万一你不知道。

注意:建议使用using块来确保连接对象关闭并正确布置。

+0

谢谢!我会放弃这一点。 – MariaL

+0

@MariaL不客气,祝你的项目 – Izzy

+0

只是一个简单的问题 - 我现在得到这个错误:CS1518:期望的类,委托,枚举,接口或公共结构LIST GetData()行。任何想法,为什么我得到这个错误? – MariaL

3

使用SqlDataSource不一定是不好的做法...但它确实倾向于将您的数据访问代码与演示代码混合在一起。此外,您通常可以使用包装视图的ObjectDataSource做得更好。还有一点涉及的代码(你必须创建一个新的类来从你的视图中进行选择),但是最终你会得到方法,这些方法可以在未来轻松更新或替换,以便处理可能需要的任何更改。

0

您可以以编程方式设置网格视图的数据源。

protected void Page_Load(object sender, EventArts e) 
    { 
     using(SqlConnection conn = new SqlConnection(connectionString)) 
     { 
      string query = "SELECT * FROM Test"; //your SQL query goes here 
      SqlCommand cmd = new SqlCommand(query, conn); 
      SqlDataAdapter da = new SqlDataAdapter(cmd); 
      DataTable table = new DataTable(); 
      da.Fill(table); 

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

      cmd.Dispose(); 
      da.Dispose(); 
     } 
    } 
相关问题