回答
SQL表EmpDetail
根據您的情況可以嘗試下面的代碼
我希望它可以幫助你
protected void GridviewBind()
{
using (SqlConnection con = new SqlConnection("Data Source=RapidProgramming;Integrated Security=true;Initial Catalog=RPDB"))
{
con.Open();
SqlCommand cmd = new SqlCommand("Select Name,Salary FROM YOUR TABLE", con);
SqlDataReader dr = cmd.ExecuteReader();
GridView1.DataSource = dr;
GridView1.DataBind();
con.Close();
}
}
<asp:GridView ID="GridView1" runat="server" BackColor="White"
BorderColor="#3366CC" BorderStyle="None"
BorderWidth="1px" CellPadding="4"
style="text-align: center; margin-left: 409px" Width="350px">
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
<PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
<RowStyle BackColor="White" ForeColor="#003399" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<SortedAscendingCellStyle BackColor="#EDF6F6" />
<SortedAscendingHeaderStyle BackColor="#0D4AC4" />
<SortedDescendingCellStyle BackColor="#D6DFDF" />
<SortedDescendingHeaderStyle BackColor="#002876" />
</asp:GridView>;
你可以簡單地使用SqlDataSource。您可以將SqlDataSource從它所說的Data,SqlDataSource的工具箱中移出。然後您將使用智能標籤配置數據源。然後使用gridview上的智能標記,選擇您放置在aspx頁面上的SqlDataSource。這非常快,需要很少或沒有編碼。 http://msdn.microsoft.com/En-us/Library/z72eefad.aspx這會告訴你更多一點。希望這可以幫助你!
爲了運行這些代碼,你需要與你更換的ConnectionString憑據myServerName \ myInstanceName,MYDATABASE,名爲myUsername,MYPASSWORD
using System.Data;
using System.Data.SqlClient;
string sConnectionString = @"Data Source=myServerName\myInstanceName;Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword;";
protected void Page_Load(object sender, EventArgs e){
if(!IsPostBack)
BindGridView();
}
private void BindGridView() {
DataTable dt = new DataTable();
SqlConnection con = null;
try {
string sQuery = "SELECT ID, Name, Salary FROM EmpDetail";
SqlConnection con = new SqlConnection(sConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand(sQuery, con);
SqlDataReader sdr = cmd.ExecuteReader();
dt.Load(sdr);
Gridview1.DataSource = dt;
Gridview1.DataBind();
}
catch{ }
finally{
dt.Dispose();
con.Close();
}
}
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
bindData();
}
}
public void bindData() {
SqlConnection con=new SqlCponnection(ConnectionStrings);
SqlDataAdapter da = new SqlDataAdapter("select * from Your TableName", con);
DataSet ds = new DataSet();
try {
da.Fill(ds, "YourTableName");
GridView1.DataSource = ds;
GridView1.DataBind();
} catch (Exception e) {
Response.Write(e.Message);
} finally {
ds.Dispose();
da.Dispose();
con.Dispose();
}
use Class7917
select * from Emp
alter table Emp add images varchar(100)
sp_helptext 'usp_emp_insert_update'
alter proc usp_emp_insert_update
@empid int,
@name varchar(50),
@cid int,
@sid int,
@dob datetime,
@isactive int,
@hobbies varchar(100),
@images varchar(100)
as
begin
if(@empid=0)
begin
insert into Emp(Name,cid,sid,dob,isactive,hobbies,images)
values(@Name,@cid,@sid,@dob,@isactive,@hobbies,@images)
end
else
begin
update Emp set [email protected],[email protected],[email protected],
[email protected],[email protected],[email protected],[email protected]
where [email protected]
end
end
truncate table Emp
您的工作發佈整個解決方案的問題(在6部分/答案)讚賞,但它似乎超出了典型的SO格式。 – Alex
歡迎來到SO。請填寫說明,而不是隻寫源代碼。 – cSteusloff
試試這個....
protected void Page_Load(object sender, EventArgs e)
{
using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["myDB"].ConnectionString))
{
SqlCommand cmd = new SqlCommand("select * from Table1", conn);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
GridView1.DataSource = dr;
GridView1.DataBind();
conn.Close();
}
}
<div>
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
</div>
- 1. Gridview數據綁定
- 2. 如何綁定IQueryable數據到asp gridview
- 3. 從多個數據源綁定GridView
- 4. 如何將整數值從數據庫綁定到GridView中的RadioGroup?
- 5. HTML5如何本地數據庫綁定到ASP GridView的
- 6. 如何將數據庫中的值綁定到gridview?
- 7. GridView的數據綁定
- 8. GridView不綁定數據源
- 9. 綁定數據到GridView
- 10. LINQ的數據綁定gridview
- 11. GridView數據綁定問題
- 12. Gridview數據綁定錯誤
- 13. 數據綁定到GRIDVIEW
- 14. GridView中的數據綁定
- 15. 如何將可空屬性綁定到gridview數據綁定()?
- 16. 如何正確綁定gridview數據綁定?
- 17. 的GridView綁定從數據庫和額外的文本
- 18. 如何使用VB從數據庫表格的GridView中綁定下拉列表?
- 19. 如何在從數據庫檢索後綁定到gridview之前編輯列值?
- 20. 如何將CheckBox從GridView綁定到源數據表?
- 21. 如何綁定GridView中的DropDownList與gridview中的數據NOT
- 22. 從DevExpress數據庫中綁定數據
- 23. 如何刷新GridView與數據庫綁定數據時使用asp.net c#
- 24. 將數據庫數據綁定到ASP.Net中的GridView
- 25. 在數據庫更改時更新ASP.NET數據綁定GridView
- 26. 在GridView中綁定數據沒有輸入數據庫
- 27. 如何從數據庫綁定或顯示在文本數據
- 28. 如何將數據從數據庫綁定到控制字段?
- 29. 如何將數據從內部數據庫綁定到devExpress gridcontrol
- 30. 如何將數據從數據庫綁定到silverlight中的treeview?
您不需要關閉連接('con.Close();'),因爲'SqlConnection'類的'IDisposable'實現調用了co上的'.Close()'爲你服務。 –
感謝您的通知 –