如何使用实体模型从数据库保存和检索数据

时间:2017-12-10 15:05:42

标签: c# .net

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 个答案:

答案 0 :(得分:0)

#include<iostream>

int main()
{
    int i=5, *p=&i;
    decltype(p) x; //use 1
    decltype(*p) x; //use 2

    return 0;
}

答案 1 :(得分:0)

显示旧的PX SqlConnection con = new SqlConnection(@&#34; Data Source =(LocalDB)\ MSSQLLocalDB; AttachDbFilename = C:\ Users \ Napster \ Documents \ Visual Studio 2015 \ WebSites \ demoweb2 \ App_Data \ fundata.mdf; Integrated Security = True&# 34);

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");
}

答案 2 :(得分:0)

旧方法  &#34; SelectCommand =&#34; SELECT * FROM [demotable]&#34; UpdateCommand =&#34;更新[demotable] set [Name] = @ name,[city] = @ city where [id] = @ id&#34; DeleteCommand =&#34;从[demotable]删除[id] = @ id&#34;&gt;

的.cs public partial class _Default:System.Web.UI.Page {     SqlConnection con = new SqlConnection(@&#34; Data Source =(LocalDB)\ MSSQLLocalDB; AttachDbFilename = C:\ Users \ Napster \ Documents \ Visual Studio 2015 \ WebSites \ Webdemo \ App_Data \ demodatabase.mdf; Integrated Security = True&# 34);     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");

}

}

答案 3 :(得分:0)

使用已连接

进行更新
    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();
   }
}

}