題目是:建立一個Web 連接資料庫 資料庫有 鍵盤滑鼠螢幕電腦及其價格
提示:可以用Gridview 用
以下我資料程式碼
.cs檔如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;
using System.Data;
namespace Web_TSEST_2018._4._9
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string connString = "server=127.0.0.1;port=3307;user id=root;password=root;database=2018.4.3-test-price;charset=utf8;";
MySqlConnection conn = new MySqlConnection();
conn.ConnectionString = connString;
if (conn.State != ConnectionState.Open)
conn.Open();
string sql = @"select * from sales";
DataTable dt = new DataTable();
MySqlDataAdapter adapter = new MySqlDataAdapter(sql, conn);
adapter.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
dt.Dispose();
conn.Close();
}
}
}
.asps檔如下 前端如下
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="Web_TSEST_2018._4._9.WebForm2" %>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowPaging="true">
</asp:GridView>
</div>
</form>
使用上面資料庫
顯示解果確是:沒東西
已經算成功一半了
如下圖
求解...
前端 裡面的 GridView 要寫什麼?
裡面要加什麼?
把別人複製過去在貼上去 都不行
SqlConnection Conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["smallworkCC"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
System.Globalization.CultureInfo cag = new System.Globalization.CultureInfo("zh-TW");
cag.DateTimeFormat.Calendar = new System.Globalization.TaiwanCalendar();
System.Threading.Thread.CurrentThread.CurrentCulture = cag;
if (!IsPostBack)
{
ViewState["SortOrder"] = "SerialNum";
ViewState["OrderDire"] = "ASC";
OWNdataBind();
}
Page.MaintainScrollPositionOnPostBack = true;
}
protected void OWNdataBind()
{
string selSTR = "";
SqlDataAdapter myAdapter = new SqlDataAdapter(selSTR, Conn);
DataSet myDs = new DataSet();
try
{
myAdapter.Fill(myDs, "test");
DataView view = myDs.Tables["test"].DefaultView;
string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
view.Sort = sort;
GridView1.DataSource = view;
GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write("<HR/> 錯誤-- " + ex.ToString());
}
}
新手等級的程式碼
給你參考
我的查詢字串(SQL)隱藏了,請忽略