flex删除修改数据库
3个回答
2011-12-27
展开全部
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
protected void bind()
{
NorthWndDataContext db = new NorthWndDataContext();
var q = from p in db.Products
where p.UnitPrice > 20 orderby p.UnitPrice ascending
select new
{
p.ProductID,
名称 = p.ProductName,
价格 = p.UnitPrice,
订单 = p.UnitsInStock,
订单状态 = p.UnitsOnOrder > 0 ?"有订单":"无订单"
};
gvProducts.DataSource = q;
gvProducts.DataBind();
}
/// <summary>
/// 插入
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnInsert_Click(object sender, EventArgs e)
{
string strProductName = tbProductName.Text;
var strProductPrice =tbProductPrice.Text;
NorthWndDataContext db = new NorthWndDataContext();
var product = new Products
{
ProductName = strProductName,
UnitPrice = (decimal) 100000.0
};
db.Products.InsertOnSubmit(product);
db.SubmitChanges();
bind();
}
/// <summary>
/// 删除
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnDelete_Command(object sender, CommandEventArgs e)
{
int nID = int.Parse(e.CommandArgument.ToString());
NorthWndDataContext db = new NorthWndDataContext();
Products pro = db.Products.First(c=>c.ProductID==nID);
db.Products.DeleteOnSubmit(pro);
db.SubmitChanges();
bind();
}
protected void Page_Load(object sender, EventArgs e)
{
{
if (!IsPostBack)
{
SelectRecordsByDataSet();
}
}
}
protected void SelectRecordsByDataSet()
{
//获取连接字符串
string strCon = ConfigurationManager.ConnectionStrings["db_0700439"].ToString();
//建立数据源并实例化
SqlConnection con = new SqlConnection(strCon);
con.Open();
//操作数据库
string strSql = "select * from tb_UserInfo";
SqlCommand cmd = new SqlCommand(strSql, con);
//实例化dataAdapter
SqlDataAdapter sda = new SqlDataAdapter();
//设置 command
sda.SelectCommand = cmd;
//实例化dataset
DataSet ds = new DataSet();
//填充
sda.Fill(ds);
con.Close();
//绑定
gvStudent.DataSource = ds;
gvStudent.DataBind();
}
protected void btnLogin_Click(object sender, EventArgs e)
{
//获得用户名和密码
string strUserName = tbUserName.Text;
string strUserPwd = tbUserPwd.Text;
//连接字符串
string strCon = ConfigurationManager.ConnectionStrings["db_0700439"].ToString();
//建立数据源的连接并实例化
SqlConnection con = new SqlConnection(strCon);
con.Open();
//对数据源执行操作
string strSql = "select count(*) from tb_UserInfo where UserName = @pUserName and UserPwd = @pUserPwd";
SqlCommand cmd = new SqlCommand(strSql, con);
//加入 parameters
cmd.Parameters.Add("@pUserName", SqlDbType.VarChar, 50);
cmd.Parameters.Add("@pUserPwd", SqlDbType.VarChar, 50);
//设值
cmd.Parameters["@pUserName"].Value = strUserName;
cmd.Parameters["@pUserPwd"].Value = strUserPwd;
int iRet = (int)cmd.ExecuteScalar();
con.Close();
if (iRet > 0)
{
//有效值
lbMsgShow.Text = "欢迎登录!用户:" + strUserName + " , " + "密码:" + strUserPwd;
}
else
{
//无效值
lbMsgShow.Text = "错误的用户和密码!";
tbUserName.Text = "";
tbUserPwd.Text = "";
}
}
protected void btnAdd_Click(object sender, EventArgs e)
{
//连接数据库
string strCon1 = ConfigurationManager.ConnectionStrings["db_0700439"].ToString();
//建立数据源的连接并实例化
SqlConnection con = new SqlConnection(strCon1);
//对数据源执行操作
SqlCommand cmd1 = new SqlCommand(strCon1, con);
//使用parameters
cmd1.CommandText = "insert into tb_UserInfo(UserName,UserPwd)values(@pUserName,@pUserPwd)";
cmd1.Parameters.AddWithValue("@pUserName", tbUserName.Text);
cmd1.Parameters.AddWithValue("@pUserPwd", tbUserPwd.Text);
try
{
con.Open();
cmd1.ExecuteNonQuery();
lbMsgShow.Text = "记录成功";
//获取连接字符串
string strCon = ConfigurationManager.ConnectionStrings["db_0700439"].ToString();
//实例化con
con = new SqlConnection(strCon1);
//对数据源进行操作并实例化
string strSql = "select * from tb_UserInfo";
SqlCommand cmd = new SqlCommand(strSql, con);
//实例化dataAdapter
SqlDataAdapter sda = new SqlDataAdapter();
//设置 command
sda.SelectCommand = cmd;
//实例化dataset
DataSet ds = new DataSet();
//填充
sda.Fill(ds);
//绑定
gvStudent.DataSource = ds;
gvStudent.DataBind();
}
catch (Exception ex)
{
lbMsgShow.Text = "失败:" + ex.Message;
}
finally
{
cmd1 = null;
con.Close();
con = null;
}
}
protected void btnDelete_Click(object sender, EventArgs e)
{
string strCon1 = ConfigurationManager.ConnectionStrings["db_0700439"].ToString();
SqlConnection con = new SqlConnection(strCon1);
SqlCommand cmd1 = new SqlCommand(strCon1, con);
cmd1.CommandText = "delete from tb_UserInfo where UserName = @pUserName and UserPwd = @pUserPwd";
cmd1.Parameters.AddWithValue("@pUserName", tbUserName.Text);
cmd1.Parameters.AddWithValue("@pUserPwd", tbUserPwd.Text);
try
{
con.Open();
cmd1.ExecuteNonQuery();
lbMsgShow.Text = "删除成功";
string strCon = ConfigurationManager.ConnectionStrings["db_0700439"].ToString();
//实例化数据连接
con = new SqlConnection(strCon);
//随数据库进行操作
string strSql = "select * from tb_UserInfo";
SqlCommand cmd = new SqlCommand(strSql, con);
//实例化dataAdapter
SqlDataAdapter sda = new SqlDataAdapter();
//设置 command
sda.SelectCommand = cmd;
//设置数据
DataSet ds = new DataSet();
//填入数据
sda.Fill(ds);
//数据绑定
gvStudent.DataSource = ds;
gvStudent.DataBind();
}
catch (Exception ex)
{
lbMsgShow.Text = "删除失败:" + ex.Message;
}
finally
{
cmd1 = null;
con.Close();
con = null;
}
}
}
{
if (!IsPostBack)
{
bind();
}
}
protected void bind()
{
NorthWndDataContext db = new NorthWndDataContext();
var q = from p in db.Products
where p.UnitPrice > 20 orderby p.UnitPrice ascending
select new
{
p.ProductID,
名称 = p.ProductName,
价格 = p.UnitPrice,
订单 = p.UnitsInStock,
订单状态 = p.UnitsOnOrder > 0 ?"有订单":"无订单"
};
gvProducts.DataSource = q;
gvProducts.DataBind();
}
/// <summary>
/// 插入
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnInsert_Click(object sender, EventArgs e)
{
string strProductName = tbProductName.Text;
var strProductPrice =tbProductPrice.Text;
NorthWndDataContext db = new NorthWndDataContext();
var product = new Products
{
ProductName = strProductName,
UnitPrice = (decimal) 100000.0
};
db.Products.InsertOnSubmit(product);
db.SubmitChanges();
bind();
}
/// <summary>
/// 删除
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnDelete_Command(object sender, CommandEventArgs e)
{
int nID = int.Parse(e.CommandArgument.ToString());
NorthWndDataContext db = new NorthWndDataContext();
Products pro = db.Products.First(c=>c.ProductID==nID);
db.Products.DeleteOnSubmit(pro);
db.SubmitChanges();
bind();
}
protected void Page_Load(object sender, EventArgs e)
{
{
if (!IsPostBack)
{
SelectRecordsByDataSet();
}
}
}
protected void SelectRecordsByDataSet()
{
//获取连接字符串
string strCon = ConfigurationManager.ConnectionStrings["db_0700439"].ToString();
//建立数据源并实例化
SqlConnection con = new SqlConnection(strCon);
con.Open();
//操作数据库
string strSql = "select * from tb_UserInfo";
SqlCommand cmd = new SqlCommand(strSql, con);
//实例化dataAdapter
SqlDataAdapter sda = new SqlDataAdapter();
//设置 command
sda.SelectCommand = cmd;
//实例化dataset
DataSet ds = new DataSet();
//填充
sda.Fill(ds);
con.Close();
//绑定
gvStudent.DataSource = ds;
gvStudent.DataBind();
}
protected void btnLogin_Click(object sender, EventArgs e)
{
//获得用户名和密码
string strUserName = tbUserName.Text;
string strUserPwd = tbUserPwd.Text;
//连接字符串
string strCon = ConfigurationManager.ConnectionStrings["db_0700439"].ToString();
//建立数据源的连接并实例化
SqlConnection con = new SqlConnection(strCon);
con.Open();
//对数据源执行操作
string strSql = "select count(*) from tb_UserInfo where UserName = @pUserName and UserPwd = @pUserPwd";
SqlCommand cmd = new SqlCommand(strSql, con);
//加入 parameters
cmd.Parameters.Add("@pUserName", SqlDbType.VarChar, 50);
cmd.Parameters.Add("@pUserPwd", SqlDbType.VarChar, 50);
//设值
cmd.Parameters["@pUserName"].Value = strUserName;
cmd.Parameters["@pUserPwd"].Value = strUserPwd;
int iRet = (int)cmd.ExecuteScalar();
con.Close();
if (iRet > 0)
{
//有效值
lbMsgShow.Text = "欢迎登录!用户:" + strUserName + " , " + "密码:" + strUserPwd;
}
else
{
//无效值
lbMsgShow.Text = "错误的用户和密码!";
tbUserName.Text = "";
tbUserPwd.Text = "";
}
}
protected void btnAdd_Click(object sender, EventArgs e)
{
//连接数据库
string strCon1 = ConfigurationManager.ConnectionStrings["db_0700439"].ToString();
//建立数据源的连接并实例化
SqlConnection con = new SqlConnection(strCon1);
//对数据源执行操作
SqlCommand cmd1 = new SqlCommand(strCon1, con);
//使用parameters
cmd1.CommandText = "insert into tb_UserInfo(UserName,UserPwd)values(@pUserName,@pUserPwd)";
cmd1.Parameters.AddWithValue("@pUserName", tbUserName.Text);
cmd1.Parameters.AddWithValue("@pUserPwd", tbUserPwd.Text);
try
{
con.Open();
cmd1.ExecuteNonQuery();
lbMsgShow.Text = "记录成功";
//获取连接字符串
string strCon = ConfigurationManager.ConnectionStrings["db_0700439"].ToString();
//实例化con
con = new SqlConnection(strCon1);
//对数据源进行操作并实例化
string strSql = "select * from tb_UserInfo";
SqlCommand cmd = new SqlCommand(strSql, con);
//实例化dataAdapter
SqlDataAdapter sda = new SqlDataAdapter();
//设置 command
sda.SelectCommand = cmd;
//实例化dataset
DataSet ds = new DataSet();
//填充
sda.Fill(ds);
//绑定
gvStudent.DataSource = ds;
gvStudent.DataBind();
}
catch (Exception ex)
{
lbMsgShow.Text = "失败:" + ex.Message;
}
finally
{
cmd1 = null;
con.Close();
con = null;
}
}
protected void btnDelete_Click(object sender, EventArgs e)
{
string strCon1 = ConfigurationManager.ConnectionStrings["db_0700439"].ToString();
SqlConnection con = new SqlConnection(strCon1);
SqlCommand cmd1 = new SqlCommand(strCon1, con);
cmd1.CommandText = "delete from tb_UserInfo where UserName = @pUserName and UserPwd = @pUserPwd";
cmd1.Parameters.AddWithValue("@pUserName", tbUserName.Text);
cmd1.Parameters.AddWithValue("@pUserPwd", tbUserPwd.Text);
try
{
con.Open();
cmd1.ExecuteNonQuery();
lbMsgShow.Text = "删除成功";
string strCon = ConfigurationManager.ConnectionStrings["db_0700439"].ToString();
//实例化数据连接
con = new SqlConnection(strCon);
//随数据库进行操作
string strSql = "select * from tb_UserInfo";
SqlCommand cmd = new SqlCommand(strSql, con);
//实例化dataAdapter
SqlDataAdapter sda = new SqlDataAdapter();
//设置 command
sda.SelectCommand = cmd;
//设置数据
DataSet ds = new DataSet();
//填入数据
sda.Fill(ds);
//数据绑定
gvStudent.DataSource = ds;
gvStudent.DataBind();
}
catch (Exception ex)
{
lbMsgShow.Text = "删除失败:" + ex.Message;
}
finally
{
cmd1 = null;
con.Close();
con = null;
}
}
}
展开全部
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Select();
}
/// <summary>
/// 插入记录
/// </summary>
protected void InserUser()
{
//获取连接字符串
string strCon = ConfigurationManager.ConnectionStrings["connectionString"].ToString();
//实例化连接
SqlConnection con = new SqlConnection(strCon);
//打开连接
con.Open();
//获取值
string strUserName = txtUserName.Text;
string strUserPwd = txtPassword.Text;
string strSql = "insert into tb_User(UserName,UserPwd) values(@pUserName,@pUserPwd)";
SqlCommand cmd = new SqlCommand(strSql, con);
//添加parameters
cmd.Parameters.Add("@pUserName", SqlDbType.VarChar, 50);
cmd.Parameters.Add("@pUserPwd", SqlDbType.VarChar, 50);
//赋值
cmd.Parameters["@pUserName"].Value = strUserName;
cmd.Parameters["@pUserPwd"].Value = strUserPwd;
cmd.ExecuteNonQuery();
con.Close();
Select();
}
protected void Select()
{
//获取连接字符串
string strCon = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
//实例化连接
SqlConnection con = new SqlConnection(strCon);
//打开连接
con.Open();
//获取值
string strUserName = txtUserName.Text;
string strUserPwd = txtPassword.Text;
string strSql = "select UserName,UserPwd from tb_User";
SqlDataAdapter sda = new SqlDataAdapter(strSql,con);
DataSet ds = new DataSet();
sda.Fill(ds,"tb_User");
con.Close();
//SqlCommand cmd = new SqlCommand(strSql, con);
gvShow.DataSource = ds.Tables["tb_User"];
gvShow.DataBind();
}
public void show(string strUserName, string strUserPwd)
{
//获取连接字符串
string strCon = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
//实例化连接
SqlConnection con = new SqlConnection(strCon);
//打开连接
con.Open();
//获取值
//string strUserName = txtUserName.Text;
//string strUserPwd = txtPassword.Text;
string strSql = "select count(*) as num from tb_User where UserName=@pUserName and UserPwd=@pUserPwd";
SqlCommand cmd = new SqlCommand(strSql, con);
//添加parameters
cmd.Parameters.Add("@pUserName", SqlDbType.VarChar, 50);
cmd.Parameters.Add("@pUserPwd", SqlDbType.VarChar, 50);
//赋值
cmd.Parameters["@pUserName"].Value = strUserName;
cmd.Parameters["@pUserPwd"].Value = strUserPwd;
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
//SqlDataAdapter sda = new SqlDataAdapter(strSql, con);
DataSet ds = new DataSet();
sda.Fill(ds, "tb_User");
con.Close();
gvShow.DataSource = ds.Tables["tb_User"];
gvShow.DataBind();
}
public bool Check(string strUserName,string strUserPwd)
{
//获取连接字符串
string strCon = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
//实例化连接
SqlConnection con = new SqlConnection(strCon);
//打开连接
con.Open();
string strSql = "select count(*) as num from tb_User where UserName=@pUserName and UserPwd=@pUserPwd";
SqlCommand cmd = new SqlCommand(strSql, con);
//添加parameters
cmd.Parameters.Add("@pUserName", SqlDbType.VarChar, 50);
cmd.Parameters.Add("@pUserPwd", SqlDbType.VarChar, 50);
//赋值
cmd.Parameters["@pUserName"].Value = strUserName;
cmd.Parameters["@pUserPwd"].Value = strUserPwd;
//获取值
//string strUserName = txtUserName.Text;
//string strUserPwd = txtPassword.Text;
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
//SqlDataAdapter sda = new SqlDataAdapter(strSql, con);
DataSet ds = new DataSet();
sda.Fill(ds, "tb_User");
return Convert.ToBoolean(cmd.ExecuteScalar());
con.Close();
}
protected void btnInsert_Click(object sender, EventArgs e)
{
this.InserUser();
}
protected void btnCheck_Click(object sender, EventArgs e)
{
string strUserName = txtUserName.Text;
string strUserPwd = txtPassword.Text;
if (strUserName == "" || strUserPwd == "")
{
Response.Write("用户名或密码不能为空!");
}
else if (Check(strUserName, strUserPwd))
{
show(strUserName, strUserPwd);
Response.Write("账号密码验证正确,登录成功!");
}
else
{
Response.Write("用户名或密码错误!");
}
}
}
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Select();
}
/// <summary>
/// 插入记录
/// </summary>
protected void InserUser()
{
//获取连接字符串
string strCon = ConfigurationManager.ConnectionStrings["connectionString"].ToString();
//实例化连接
SqlConnection con = new SqlConnection(strCon);
//打开连接
con.Open();
//获取值
string strUserName = txtUserName.Text;
string strUserPwd = txtPassword.Text;
string strSql = "insert into tb_User(UserName,UserPwd) values(@pUserName,@pUserPwd)";
SqlCommand cmd = new SqlCommand(strSql, con);
//添加parameters
cmd.Parameters.Add("@pUserName", SqlDbType.VarChar, 50);
cmd.Parameters.Add("@pUserPwd", SqlDbType.VarChar, 50);
//赋值
cmd.Parameters["@pUserName"].Value = strUserName;
cmd.Parameters["@pUserPwd"].Value = strUserPwd;
cmd.ExecuteNonQuery();
con.Close();
Select();
}
protected void Select()
{
//获取连接字符串
string strCon = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
//实例化连接
SqlConnection con = new SqlConnection(strCon);
//打开连接
con.Open();
//获取值
string strUserName = txtUserName.Text;
string strUserPwd = txtPassword.Text;
string strSql = "select UserName,UserPwd from tb_User";
SqlDataAdapter sda = new SqlDataAdapter(strSql,con);
DataSet ds = new DataSet();
sda.Fill(ds,"tb_User");
con.Close();
//SqlCommand cmd = new SqlCommand(strSql, con);
gvShow.DataSource = ds.Tables["tb_User"];
gvShow.DataBind();
}
public void show(string strUserName, string strUserPwd)
{
//获取连接字符串
string strCon = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
//实例化连接
SqlConnection con = new SqlConnection(strCon);
//打开连接
con.Open();
//获取值
//string strUserName = txtUserName.Text;
//string strUserPwd = txtPassword.Text;
string strSql = "select count(*) as num from tb_User where UserName=@pUserName and UserPwd=@pUserPwd";
SqlCommand cmd = new SqlCommand(strSql, con);
//添加parameters
cmd.Parameters.Add("@pUserName", SqlDbType.VarChar, 50);
cmd.Parameters.Add("@pUserPwd", SqlDbType.VarChar, 50);
//赋值
cmd.Parameters["@pUserName"].Value = strUserName;
cmd.Parameters["@pUserPwd"].Value = strUserPwd;
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
//SqlDataAdapter sda = new SqlDataAdapter(strSql, con);
DataSet ds = new DataSet();
sda.Fill(ds, "tb_User");
con.Close();
gvShow.DataSource = ds.Tables["tb_User"];
gvShow.DataBind();
}
public bool Check(string strUserName,string strUserPwd)
{
//获取连接字符串
string strCon = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
//实例化连接
SqlConnection con = new SqlConnection(strCon);
//打开连接
con.Open();
string strSql = "select count(*) as num from tb_User where UserName=@pUserName and UserPwd=@pUserPwd";
SqlCommand cmd = new SqlCommand(strSql, con);
//添加parameters
cmd.Parameters.Add("@pUserName", SqlDbType.VarChar, 50);
cmd.Parameters.Add("@pUserPwd", SqlDbType.VarChar, 50);
//赋值
cmd.Parameters["@pUserName"].Value = strUserName;
cmd.Parameters["@pUserPwd"].Value = strUserPwd;
//获取值
//string strUserName = txtUserName.Text;
//string strUserPwd = txtPassword.Text;
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
//SqlDataAdapter sda = new SqlDataAdapter(strSql, con);
DataSet ds = new DataSet();
sda.Fill(ds, "tb_User");
return Convert.ToBoolean(cmd.ExecuteScalar());
con.Close();
}
protected void btnInsert_Click(object sender, EventArgs e)
{
this.InserUser();
}
protected void btnCheck_Click(object sender, EventArgs e)
{
string strUserName = txtUserName.Text;
string strUserPwd = txtPassword.Text;
if (strUserName == "" || strUserPwd == "")
{
Response.Write("用户名或密码不能为空!");
}
else if (Check(strUserName, strUserPwd))
{
show(strUserName, strUserPwd);
Response.Write("账号密码验证正确,登录成功!");
}
else
{
Response.Write("用户名或密码错误!");
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
flex不能直接连接数据库的,可以通过post或get的方式或者第三方控件。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询