protected void btnsubmit_Click(object sender, EventArgs e)
{
dbdataEntities1 dbsdemo = new dbdataEntities1();
string fname = txtname.Text;
string lname = txtlastname.Text;
string email = txtemail.Text;
string gender = Rbgender.SelectedValue;
string phoneno = txtphno.Text;
string role = DropDownList1.SelectedValue;
string filename = System.IO.Path.GetFileName(Fupload.FileName);
Fupload.SaveAs(Server.MapPath("Uploads/") + filename);
tbldata tab = new tbldata();
tab.filename = filename;
tab.firstname = fname;
tab.lastname= lname;
tab.email = email;
tab.gender = gender;
tab.phoneno = Convert.ToInt32(phoneno);
tab.role = role;
dbsdemo.tbldatas.Add(tab);
dbsdemo.SaveChanges();
}
//View.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Napster\documents\visual studio 2015\Projects\WebApplication1\WebApplication1\App_Data\dbdata.mdf;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework");
SqlCommand cmd = new SqlCommand("Select * from tbldata",con);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapter.Fill(ds);
GridView1.DataSource = ds;
//dbdataEntities1 dbsdemo = new dbdataEntities1();
// DataSet query = new DataSet();
// query= (from tb in dbsdemo.tbldatas select tb).ToList();
//GridView1.DataSource = dbsdemo.tbldatas.ToList();
GridView1.DataBind();
}
//view.ASPX
<asp:ImageField DataImageUrlField="filename" DataImageUrlFormatString="~\Uploads\{0}" HeaderText="Photo" ControlStyle-Height="100" ControlStyle-Width="100" >
<ControlStyle Height="100px" Width="100px"></ControlStyle>
</asp:ImageField>
<asp:HyperLinkField DataNavigateUrlFields="id" DataNavigateUrlFormatString="update.aspx?id={0}" DataTextField="id" DataTextFormatString="Edit" HeaderText="Edit" Text="Edit" />
</Columns>
4 个解决方案
#1
0
update.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
using (datademoEntities db = new datademoEntities())
{
var query = from tbl in db.demotables select new{tbl.Id, tbl.FirstName, tbl.gender, tbl.PhoneNumber, tbl.Role};
ddlupdate.DataSource = query.ToList();
ddlupdate.DataTextField = "FirstName";
ddlupdate.DataValueField = "id";
ddlupdate.DataBind();
ddlupdate.Items.Insert(0, new ListItem("select"));
} }
}
static int did;
protected void ddlupdate_SelectedIndexChanged(object sender,
EventArgs e)
{
did = int.Parse(ddlupdate.SelectedValue);
using (datademoEntities db = new datademoEntities())
{
var query = (from tbl in db.demotables
where tbl.Id == did
select tbl).First();
txtname.Text = query.FirstName;
rbgender.DataTextField = query.gender;
txtphone.Text = query.PhoneNumber.ToString();
}
}
protected void btnupdate_Click(object sender, EventArgs e)
{
string dname = txtname.Text;
int dphone = int.Parse(txtphone.Text);
using (datademoEntities db = new datademoEntities())
{
demotable tbl = (from row in db.demotables
where
row.Id==did
select row).First();
tbl.FirstName = dname;
tbl.PhoneNumber = dphone;
db.SaveChanges();
Response.AddHeader("Refresh", "3;url.display.aspx");
}
}
}
}
display.aspx.cs
public partial class display : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
datademoEntities db = new datademoEntities();
var query = (from tbl in db.demotables select tbl).ToList();
GridView1.DataSource = query;
GridView1.DataBind();
}
}
}
INSERT.apx.cs
protected void btnsubmit_Click(object sender, EventArgs e)
{
datademoEntities dm = new datademoEntities();
string dname = txtname.Text;
string drole = ddlrole.SelectedValue;
string dphone = txtphone.Text;
string dgender = rbgender.SelectedValue;
demotable tbl = new demotable();
tbl.FirstName = dname;
tbl.Role = drole;
tbl.PhoneNumber = Convert.ToInt32(dphone);
tbl.gender = dgender;
dm.demotables.Add(tbl);
dm.SaveChanges();
#2
0
DISPLAY OLD AS PX SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Napster\Documents\Visual Studio 2015\WebSites\demoweb2\App_Data\fundata.mdf;Integrated Security=True");
DISPLAY OLD AS PX SqlConnection con = new SqlConnection(@“Data Source =(LocalDB)\ MSSQLLocalDB; AttachDbFilename = C:\ Users \ Napster \ Documents \ Visual Studio 2015 \ WebSites \ demoweb2 \ App_Data \ fundata.mdf; Integrated Security = True “);
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into funtable values('"+txtname.Text+"','"+txtcity.Text+"')";
cmd.ExecuteNonQuery();
con.Close();
Response.Redirect("display.aspx");
}
#3
0
Old method " SelectCommand="SELECT * FROM [demotable]" UpdateCommand="Update [demotable] set [Name]=@name, [city]=@city where [id]=@id" DeleteCommand="delete from [demotable] where [id]=@id">
旧方法“SelectCommand =”SELECT * FROM [demotable]“UpdateCommand =”Update [demotable] set [Name] = @ name,[city] = @ city where [id] = @ id“DeleteCommand =”delete from [demotable]其中[id] = @ id“>
.cs public partial class _Default : System.Web.UI.Page { SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Napster\Documents\Visual Studio 2015\WebSites\Webdemo\App_Data\demodatabase.mdf;Integrated Security=True"); protected void Page_Load(object sender, EventArgs e) {
.cs public partial class _Default:System.Web.UI.Page {SqlConnection con = new SqlConnection(@“Data Source =(LocalDB)\ MSSQLLocalDB; AttachDbFilename = C:\ Users \ Napster \ Documents \ Visual Studio 2015 \ WebSites \ Webdemo \ App_Data \ demodatabase.mdf; Integrated Security = True“); protected void Page_Load(object sender,EventArgs e){
}
protected void Submit_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "insert into demotable values('" + txtname.Text + "','" + txtcity.Text + "')";
cmd.ExecuteNonQuery();
con.Close();
Response.Redirect("default.aspx");
}
}
}
#4
0
Update using connected
使用已连接更新
protected void btnsubmit_Click(object sender, EventArgs e)
{
}
static int did;
protected void ddlupdate_SelectedIndexChange
{
did = int.Parse(ddlupdate.SelectedValue);
SqlConnection con = new SqlConnection(@"");
con.Open();
SqlCommand cmd = new SqlCommand("Select firstname, lastname, email,
gender, role, phoneno, filename from tbldata where Id=" +did, con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
txtname.Text = dr.GetString(0);
txtlastname.Text = dr.GetString(1);
txtemail.Text = dr.GetString(2);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string fname, lastname, email, gender, role, phoneno, filename;
fname = txtname.Text;
lastname = txtlastname.Text;
email = txtemail.Text;
gender = Rbgender.SelectedValue;
phoneno = txtphno.Text;
role = DropDownList1.SelectedValue;
filename = System.IO.Path.GetFileName(Fupload.FileName);
Fupload.SaveAs(Server.MapPath("Uploads/") + filename);
SqlConnection con = new SqlConnection(@"");
con.Open();
SqlCommand cmd = new SqlCommand("Update tbldata set firstname=@fname,lastname=@lastname,email=@email,gender=@gender,role=@role,phoneno=@phoneno,filename=@filename where id=@did", con);
cmd.Parameters.AddWithValue("@firstanme", fname);
con.Close();
}
}
}
}
#1
0
update.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
using (datademoEntities db = new datademoEntities())
{
var query = from tbl in db.demotables select new{tbl.Id, tbl.FirstName, tbl.gender, tbl.PhoneNumber, tbl.Role};
ddlupdate.DataSource = query.ToList();
ddlupdate.DataTextField = "FirstName";
ddlupdate.DataValueField = "id";
ddlupdate.DataBind();
ddlupdate.Items.Insert(0, new ListItem("select"));
} }
}
static int did;
protected void ddlupdate_SelectedIndexChanged(object sender,
EventArgs e)
{
did = int.Parse(ddlupdate.SelectedValue);
using (datademoEntities db = new datademoEntities())
{
var query = (from tbl in db.demotables
where tbl.Id == did
select tbl).First();
txtname.Text = query.FirstName;
rbgender.DataTextField = query.gender;
txtphone.Text = query.PhoneNumber.ToString();
}
}
protected void btnupdate_Click(object sender, EventArgs e)
{
string dname = txtname.Text;
int dphone = int.Parse(txtphone.Text);
using (datademoEntities db = new datademoEntities())
{
demotable tbl = (from row in db.demotables
where
row.Id==did
select row).First();
tbl.FirstName = dname;
tbl.PhoneNumber = dphone;
db.SaveChanges();
Response.AddHeader("Refresh", "3;url.display.aspx");
}
}
}
}
display.aspx.cs
public partial class display : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
datademoEntities db = new datademoEntities();
var query = (from tbl in db.demotables select tbl).ToList();
GridView1.DataSource = query;
GridView1.DataBind();
}
}
}
INSERT.apx.cs
protected void btnsubmit_Click(object sender, EventArgs e)
{
datademoEntities dm = new datademoEntities();
string dname = txtname.Text;
string drole = ddlrole.SelectedValue;
string dphone = txtphone.Text;
string dgender = rbgender.SelectedValue;
demotable tbl = new demotable();
tbl.FirstName = dname;
tbl.Role = drole;
tbl.PhoneNumber = Convert.ToInt32(dphone);
tbl.gender = dgender;
dm.demotables.Add(tbl);
dm.SaveChanges();
#2
0
DISPLAY OLD AS PX SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Napster\Documents\Visual Studio 2015\WebSites\demoweb2\App_Data\fundata.mdf;Integrated Security=True");
DISPLAY OLD AS PX SqlConnection con = new SqlConnection(@“Data Source =(LocalDB)\ MSSQLLocalDB; AttachDbFilename = C:\ Users \ Napster \ Documents \ Visual Studio 2015 \ WebSites \ demoweb2 \ App_Data \ fundata.mdf; Integrated Security = True “);
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into funtable values('"+txtname.Text+"','"+txtcity.Text+"')";
cmd.ExecuteNonQuery();
con.Close();
Response.Redirect("display.aspx");
}
#3
0
Old method " SelectCommand="SELECT * FROM [demotable]" UpdateCommand="Update [demotable] set [Name]=@name, [city]=@city where [id]=@id" DeleteCommand="delete from [demotable] where [id]=@id">
旧方法“SelectCommand =”SELECT * FROM [demotable]“UpdateCommand =”Update [demotable] set [Name] = @ name,[city] = @ city where [id] = @ id“DeleteCommand =”delete from [demotable]其中[id] = @ id“>
.cs public partial class _Default : System.Web.UI.Page { SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Napster\Documents\Visual Studio 2015\WebSites\Webdemo\App_Data\demodatabase.mdf;Integrated Security=True"); protected void Page_Load(object sender, EventArgs e) {
.cs public partial class _Default:System.Web.UI.Page {SqlConnection con = new SqlConnection(@“Data Source =(LocalDB)\ MSSQLLocalDB; AttachDbFilename = C:\ Users \ Napster \ Documents \ Visual Studio 2015 \ WebSites \ Webdemo \ App_Data \ demodatabase.mdf; Integrated Security = True“); protected void Page_Load(object sender,EventArgs e){
}
protected void Submit_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "insert into demotable values('" + txtname.Text + "','" + txtcity.Text + "')";
cmd.ExecuteNonQuery();
con.Close();
Response.Redirect("default.aspx");
}
}
}
#4
0
Update using connected
使用已连接更新
protected void btnsubmit_Click(object sender, EventArgs e)
{
}
static int did;
protected void ddlupdate_SelectedIndexChange
{
did = int.Parse(ddlupdate.SelectedValue);
SqlConnection con = new SqlConnection(@"");
con.Open();
SqlCommand cmd = new SqlCommand("Select firstname, lastname, email,
gender, role, phoneno, filename from tbldata where Id=" +did, con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
txtname.Text = dr.GetString(0);
txtlastname.Text = dr.GetString(1);
txtemail.Text = dr.GetString(2);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string fname, lastname, email, gender, role, phoneno, filename;
fname = txtname.Text;
lastname = txtlastname.Text;
email = txtemail.Text;
gender = Rbgender.SelectedValue;
phoneno = txtphno.Text;
role = DropDownList1.SelectedValue;
filename = System.IO.Path.GetFileName(Fupload.FileName);
Fupload.SaveAs(Server.MapPath("Uploads/") + filename);
SqlConnection con = new SqlConnection(@"");
con.Open();
SqlCommand cmd = new SqlCommand("Update tbldata set firstname=@fname,lastname=@lastname,email=@email,gender=@gender,role=@role,phoneno=@phoneno,filename=@filename where id=@did", con);
cmd.Parameters.AddWithValue("@firstanme", fname);
con.Close();
}
}
}
}