7,765
社区成员
发帖
与我相关
我的任务
分享
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="读取数据" />
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="编号:"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label2" runat="server" Text="名称:"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="修改数据" />
<br />
<br />
</div>
</form>
</body>
</html>
---------------------------------------------------------------------------------
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 修改数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button2_Click(object sender, EventArgs e)
{
//简单读取值,你需要进行判断空值和没有id的判断,这里进行一个最简单的处理
string id = this.TextBox1.Text.Trim();
string name = this.TextBox2.Text.Trim();
//获取文本框中填入的值,进行更新,然后重新绑定GridView
using (SqlConnection con = new SqlConnection("server=localhost;uid=sa;pwd=sa;database=inv_fund_db"))
{
con.Open();
string strSQL = "update t_fund_info set fund_name='"+name+"' where fund_id='"+id+"'";
SqlCommand cmd = new SqlCommand(strSQL, con);
if (cmd.ExecuteNonQuery() > 0)
{
ClientScript.RegisterClientScriptBlock(GetType(), "", "<script>alert('修改成功!');</script>");
//重新绑定GridView,进行显示
BindGridView();
}
}
}
/// <summary>
/// 读取数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button1_Click(object sender, EventArgs e)
{
BindGridView();
}
public void BindGridView()
{
using (SqlConnection con = new SqlConnection("server=localhost;uid=sa;pwd=sa;database=inv_fund_db"))
{
con.Open();
string strSQL = "select fund_id,fund_name from t_fund_info";
SqlDataAdapter adapter = new SqlDataAdapter(strSQL, con);
DataSet ds = new DataSet();
adapter.Fill(ds);
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
}
}
}
SqlParameter para = new SqlParameter("@ID", SqlDbType.Int) { Value = 2 };
using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=inv_fund_db;Persist Security Info=True;User ID=sa;Password=sa"))
{
using (SqlCommand cmd = new SqlCommand("SELECT count(*) FROM dbo.t_fund_info WHERE ID=@ID", conn))
{
cmd.Parameters.Add(para);
conn.Open();
if ((int)cmd.ExecuteScalar() != 0)
{
}
}
}