删除C#中的DataSet的一条数据 和修改一条数据,怎么写?请给个例子,谢谢! 20
4个回答
展开全部
DataSet是一个数据集,需要一个东西来装里面的数据,我这里是用DataGrid
前台的:
<asp:datagrid id="DataGrid1" runat="Server"
AllowSorting="True"
AutoGenerateColumns="False"
cellpadding="3"
DataKeyField="id"
OnDeleteCommand="ItemsGrid_Dele"
OnEditCommand="ItemsGrid_Edit"
OnCancelCommand="ItemsGrid_Cancel"
OnUpdateCommand="ItemsGrid_Update"
BackColor="White"
BorderStyle="None"
PageSize="20"
PagerStyle-HorizontalAlign="Right"
OnPageIndexChanged="DataGridPage"
AllowPaging="True" Height="100%" Width="100%" >
<PagerStyle HorizontalAlign="Right" Mode="NumericPages"></PagerStyle>
<AlternatingItemStyle BackColor="White"></AlternatingItemStyle>
<ItemStyle BackColor="#99FFCC"></ItemStyle>
<HeaderStyle BackColor="#FFCC99"></HeaderStyle>
<Columns>
<asp:EditCommandColumn
UpdateText="更新"
HeaderText="数据更新"
CancelText="取消"
EditText="编辑">
</asp:EditCommandColumn>
<asp:ButtonColumn
Text="删除"
HeaderText="删除"
CommandName="Delete">
</asp:ButtonColumn>
<asp:TemplateColumn
SortExpression="pid"
HeaderText="编号">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"id")%>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="商品名称">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "CPmingcheng")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="CPmingcheng"
Text='<%#DataBinder.Eval(Container.DataItem,"CPmingcheng")%>' runat="Server"/>
<asp:RequiredFieldValidator ControlToValidate="CPmingcheng" Display="Dynamic" runat="Server" ErrorMessage="<br>输入不能为空!"
ID="Requiredfieldvalidator1" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="商品价格">
<ItemStyle HorizontalAlign="Center">
</ItemStyle>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"CPjiage")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="CPjiage"
Text='<%# DataBinder.Eval(Container.DataItem,"CPjiage")%>' runat="Server"/>
<asp:RequiredFieldValidator ControlToValidate="CPjiage" Display="Dynamic" runat="Server" ErrorMessage="<br>输入不能为空!"
ID="Requiredfieldvalidator2" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="库存">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "kucunqingkuang")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="kucunqingkuang"
Text='<%# DataBinder.Eval(Container.DataItem,"kucunqingkuang")%>' runat="Server"/>
<asp:RequiredFieldValidator ControlToValidate="kucunqingkuang" Display="Dynamic" runat="Server" ErrorMessage="<br>输入不能为空!"
ID="Requiredfieldvalidator3" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="等级">
<ItemStyle HorizontalAlign="Center">
</ItemStyle>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"shangpindengji")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="shangpindengji"
Text='<%# DataBinder.Eval(Container.DataItem,"shangpindengji")%>' runat="Server"/>
<asp:RequiredFieldValidator ControlToValidate="shangpindengji" Display="Dynamic" runat="Server" ErrorMessage="<br>输入不能为空!"
ID="Requiredfieldvalidator4" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="商品型号">
<ItemStyle HorizontalAlign="Center">
</ItemStyle>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"shangpinxinghao")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="shangpinxinghao"
Text='<%# DataBinder.Eval(Container.DataItem,"shangpinxinghao")%>' runat="Server"/>
<asp:RequiredFieldValidator ControlToValidate="shangpinxinghao" Display="Dynamic" runat="Server" ErrorMessage="<br>输入不能为空!"
ID="Requiredfieldvalidator5" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="商品介绍">
<ItemStyle HorizontalAlign="Center">
</ItemStyle>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"shangpinjieshao")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="shangpinjieshao"
Text='<%# DataBinder.Eval(Container.DataItem,"shangpinjieshao")%>' runat="Server"/>
<asp:RequiredFieldValidator ControlToValidate="shangpinjieshao" Display="Dynamic" runat="Server" ErrorMessage="<br>输入不能为空!"
ID="Requiredfieldvalidator6" />
</EditItemTemplate>
</asp:TemplateColumn>
<%--<asp:TemplateColumn HeaderText="分类">
<ItemStyle HorizontalAlign="Center">
</ItemStyle>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"shangpinfenlei")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="shangpinfenlei"
Text='<%# DataBinder.Eval(Container.DataItem,"shangpinfenlei")%>' runat="Server"/>
<asp:RequiredFieldValidator ControlToValidate="shangpinfenlei" Display="Dynamic" runat="Server" ErrorMessage="<br>输入不能为空!"
ID="Requiredfieldvalidator7" />
</EditItemTemplate>
</asp:TemplateColumn>--%>
<asp:TemplateColumn HeaderText="上市日期">
<ItemStyle HorizontalAlign="Center">
</ItemStyle>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"shangshiriqi")%>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
后台:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class admin_Maproducts : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["branch"] == null && Session["username"] == null)
{ Response.Write("<script>window.alert('您还未登录,请您先登录!');window.location.href='../login.aspx';</script>"); }
else if (Session["branch"].ToString() == "0" && !this.IsPostBack)
{
GetSource();
}
}
protected void GetSource()
{
string strSQL="SELECT * FROM products";
SqlConnection A = DB.createcon();
SqlDataAdapter D = new SqlDataAdapter(strSQL,A ) ;
DataSet ds = new DataSet () ;
D.Fill (ds,"products" );
DataGrid1.DataSource = ds.Tables["products"].DefaultView;
DataGrid1.DataBind();
ds.Dispose();
A.Close();
}
protected void ItemsGrid_Dele(Object sender, DataGridCommandEventArgs e)
{
string strSQL = "DELETE FROM products WHERE id =@id";
SqlConnection A = DB.createcon();
SqlCommand B = new SqlCommand(strSQL, A);
B.Parameters.Add(new SqlParameter("@id", SqlDbType.Int, 4)).Value = this.DataGrid1.DataKeys[e.Item.ItemIndex];
A.Open();
B.ExecuteNonQuery();
B.Dispose();
A.Close();
DataGrid1.EditItemIndex = -1;
GetSource();
}
protected void ItemsGrid_Edit(Object sender, DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = e.Item.ItemIndex;
GetSource();
}
protected void ItemsGrid_Cancel(Object sender, DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = -1;
GetSource();
}
protected void ItemsGrid_Update(Object sender, DataGridCommandEventArgs e)
{
TextBox CPmingcheng = (TextBox)e.Item.FindControl("CPmingcheng");
TextBox CPjiage = (TextBox)e.Item.FindControl("CPjiage");
TextBox kucunqingkuang = (TextBox)e.Item.FindControl("kucunqingkuang");
TextBox shangpindengji = (TextBox)e.Item.FindControl("shangpindengji");
TextBox shangpinxinghao = (TextBox)e.Item.FindControl("shangpinxinghao");
TextBox shangpinjieshao = (TextBox)e.Item.FindControl("shangpinjieshao");
TextBox shangpinfenlei = (TextBox)e.Item.FindControl("shangpinfenlei");
string strSQL = "UPDATE products SET ";
strSQL += "CPmingcheng='" + CPmingcheng.Text + "',";
strSQL += "CPjiage='" + CPjiage.Text + "',";
strSQL += "kucunqingkuang='" + kucunqingkuang.Text + "',";
strSQL += "shangpindengji='" + shangpindengji.Text + "',";
strSQL += "shangpinxinghao='" + shangpinxinghao.Text + "',";
strSQL += "shangpinjieshao='" + shangpinjieshao.Text + "',";
strSQL += "shangpinfenlei='" + shangpinfenlei.Text + "'";
strSQL += " WHERE id=@id";
SqlConnection A = DB.createcon();
SqlCommand B = new SqlCommand(strSQL, A);
B.Parameters.Add(new SqlParameter("@id", SqlDbType.Int, 4)).Value = this.DataGrid1.DataKeys[e.Item.ItemIndex];
A.Open();
try
{
B.ExecuteNonQuery();
}
catch { Response.Write("失败!"); }
finally { B.Dispose(); A.Close(); }
DataGrid1.EditItemIndex = -1;
GetSource();
}
protected void DataGridPage(Object sender, DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGrid1.DataBind();
GetSource();
}
protected void Button1_Click(object sender, EventArgs e)
{
string mysql = null;
if (this.DropDownList1.SelectedItem.Text == "商品名称")
{
mysql = "select * from products where CPmingcheng like '%" + this.TextBox1.Text + "%'";
}
if (this.DropDownList1.SelectedItem.Text == "商品型号")
{
mysql = "select * from products where shangpinxinghao like '%" + this.TextBox1.Text + "%'";
}
SqlConnection A = DB.createcon();
A.Open();
SqlDataAdapter D = new SqlDataAdapter(mysql, A);
DataSet ds = new DataSet();
D.Fill(ds, "products");
DataGrid1.DataSource = ds.Tables["products"].DefaultView;
DataGrid1.DataBind();
ds.Dispose();
A.Close();
TextBox1.Text = "";
}
}
你试试看........修改一下就可以用的,我测试过的了!
前台的:
<asp:datagrid id="DataGrid1" runat="Server"
AllowSorting="True"
AutoGenerateColumns="False"
cellpadding="3"
DataKeyField="id"
OnDeleteCommand="ItemsGrid_Dele"
OnEditCommand="ItemsGrid_Edit"
OnCancelCommand="ItemsGrid_Cancel"
OnUpdateCommand="ItemsGrid_Update"
BackColor="White"
BorderStyle="None"
PageSize="20"
PagerStyle-HorizontalAlign="Right"
OnPageIndexChanged="DataGridPage"
AllowPaging="True" Height="100%" Width="100%" >
<PagerStyle HorizontalAlign="Right" Mode="NumericPages"></PagerStyle>
<AlternatingItemStyle BackColor="White"></AlternatingItemStyle>
<ItemStyle BackColor="#99FFCC"></ItemStyle>
<HeaderStyle BackColor="#FFCC99"></HeaderStyle>
<Columns>
<asp:EditCommandColumn
UpdateText="更新"
HeaderText="数据更新"
CancelText="取消"
EditText="编辑">
</asp:EditCommandColumn>
<asp:ButtonColumn
Text="删除"
HeaderText="删除"
CommandName="Delete">
</asp:ButtonColumn>
<asp:TemplateColumn
SortExpression="pid"
HeaderText="编号">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"id")%>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="商品名称">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "CPmingcheng")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="CPmingcheng"
Text='<%#DataBinder.Eval(Container.DataItem,"CPmingcheng")%>' runat="Server"/>
<asp:RequiredFieldValidator ControlToValidate="CPmingcheng" Display="Dynamic" runat="Server" ErrorMessage="<br>输入不能为空!"
ID="Requiredfieldvalidator1" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="商品价格">
<ItemStyle HorizontalAlign="Center">
</ItemStyle>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"CPjiage")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="CPjiage"
Text='<%# DataBinder.Eval(Container.DataItem,"CPjiage")%>' runat="Server"/>
<asp:RequiredFieldValidator ControlToValidate="CPjiage" Display="Dynamic" runat="Server" ErrorMessage="<br>输入不能为空!"
ID="Requiredfieldvalidator2" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="库存">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "kucunqingkuang")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="kucunqingkuang"
Text='<%# DataBinder.Eval(Container.DataItem,"kucunqingkuang")%>' runat="Server"/>
<asp:RequiredFieldValidator ControlToValidate="kucunqingkuang" Display="Dynamic" runat="Server" ErrorMessage="<br>输入不能为空!"
ID="Requiredfieldvalidator3" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="等级">
<ItemStyle HorizontalAlign="Center">
</ItemStyle>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"shangpindengji")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="shangpindengji"
Text='<%# DataBinder.Eval(Container.DataItem,"shangpindengji")%>' runat="Server"/>
<asp:RequiredFieldValidator ControlToValidate="shangpindengji" Display="Dynamic" runat="Server" ErrorMessage="<br>输入不能为空!"
ID="Requiredfieldvalidator4" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="商品型号">
<ItemStyle HorizontalAlign="Center">
</ItemStyle>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"shangpinxinghao")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="shangpinxinghao"
Text='<%# DataBinder.Eval(Container.DataItem,"shangpinxinghao")%>' runat="Server"/>
<asp:RequiredFieldValidator ControlToValidate="shangpinxinghao" Display="Dynamic" runat="Server" ErrorMessage="<br>输入不能为空!"
ID="Requiredfieldvalidator5" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="商品介绍">
<ItemStyle HorizontalAlign="Center">
</ItemStyle>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"shangpinjieshao")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="shangpinjieshao"
Text='<%# DataBinder.Eval(Container.DataItem,"shangpinjieshao")%>' runat="Server"/>
<asp:RequiredFieldValidator ControlToValidate="shangpinjieshao" Display="Dynamic" runat="Server" ErrorMessage="<br>输入不能为空!"
ID="Requiredfieldvalidator6" />
</EditItemTemplate>
</asp:TemplateColumn>
<%--<asp:TemplateColumn HeaderText="分类">
<ItemStyle HorizontalAlign="Center">
</ItemStyle>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"shangpinfenlei")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="shangpinfenlei"
Text='<%# DataBinder.Eval(Container.DataItem,"shangpinfenlei")%>' runat="Server"/>
<asp:RequiredFieldValidator ControlToValidate="shangpinfenlei" Display="Dynamic" runat="Server" ErrorMessage="<br>输入不能为空!"
ID="Requiredfieldvalidator7" />
</EditItemTemplate>
</asp:TemplateColumn>--%>
<asp:TemplateColumn HeaderText="上市日期">
<ItemStyle HorizontalAlign="Center">
</ItemStyle>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"shangshiriqi")%>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
后台:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class admin_Maproducts : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["branch"] == null && Session["username"] == null)
{ Response.Write("<script>window.alert('您还未登录,请您先登录!');window.location.href='../login.aspx';</script>"); }
else if (Session["branch"].ToString() == "0" && !this.IsPostBack)
{
GetSource();
}
}
protected void GetSource()
{
string strSQL="SELECT * FROM products";
SqlConnection A = DB.createcon();
SqlDataAdapter D = new SqlDataAdapter(strSQL,A ) ;
DataSet ds = new DataSet () ;
D.Fill (ds,"products" );
DataGrid1.DataSource = ds.Tables["products"].DefaultView;
DataGrid1.DataBind();
ds.Dispose();
A.Close();
}
protected void ItemsGrid_Dele(Object sender, DataGridCommandEventArgs e)
{
string strSQL = "DELETE FROM products WHERE id =@id";
SqlConnection A = DB.createcon();
SqlCommand B = new SqlCommand(strSQL, A);
B.Parameters.Add(new SqlParameter("@id", SqlDbType.Int, 4)).Value = this.DataGrid1.DataKeys[e.Item.ItemIndex];
A.Open();
B.ExecuteNonQuery();
B.Dispose();
A.Close();
DataGrid1.EditItemIndex = -1;
GetSource();
}
protected void ItemsGrid_Edit(Object sender, DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = e.Item.ItemIndex;
GetSource();
}
protected void ItemsGrid_Cancel(Object sender, DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = -1;
GetSource();
}
protected void ItemsGrid_Update(Object sender, DataGridCommandEventArgs e)
{
TextBox CPmingcheng = (TextBox)e.Item.FindControl("CPmingcheng");
TextBox CPjiage = (TextBox)e.Item.FindControl("CPjiage");
TextBox kucunqingkuang = (TextBox)e.Item.FindControl("kucunqingkuang");
TextBox shangpindengji = (TextBox)e.Item.FindControl("shangpindengji");
TextBox shangpinxinghao = (TextBox)e.Item.FindControl("shangpinxinghao");
TextBox shangpinjieshao = (TextBox)e.Item.FindControl("shangpinjieshao");
TextBox shangpinfenlei = (TextBox)e.Item.FindControl("shangpinfenlei");
string strSQL = "UPDATE products SET ";
strSQL += "CPmingcheng='" + CPmingcheng.Text + "',";
strSQL += "CPjiage='" + CPjiage.Text + "',";
strSQL += "kucunqingkuang='" + kucunqingkuang.Text + "',";
strSQL += "shangpindengji='" + shangpindengji.Text + "',";
strSQL += "shangpinxinghao='" + shangpinxinghao.Text + "',";
strSQL += "shangpinjieshao='" + shangpinjieshao.Text + "',";
strSQL += "shangpinfenlei='" + shangpinfenlei.Text + "'";
strSQL += " WHERE id=@id";
SqlConnection A = DB.createcon();
SqlCommand B = new SqlCommand(strSQL, A);
B.Parameters.Add(new SqlParameter("@id", SqlDbType.Int, 4)).Value = this.DataGrid1.DataKeys[e.Item.ItemIndex];
A.Open();
try
{
B.ExecuteNonQuery();
}
catch { Response.Write("失败!"); }
finally { B.Dispose(); A.Close(); }
DataGrid1.EditItemIndex = -1;
GetSource();
}
protected void DataGridPage(Object sender, DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGrid1.DataBind();
GetSource();
}
protected void Button1_Click(object sender, EventArgs e)
{
string mysql = null;
if (this.DropDownList1.SelectedItem.Text == "商品名称")
{
mysql = "select * from products where CPmingcheng like '%" + this.TextBox1.Text + "%'";
}
if (this.DropDownList1.SelectedItem.Text == "商品型号")
{
mysql = "select * from products where shangpinxinghao like '%" + this.TextBox1.Text + "%'";
}
SqlConnection A = DB.createcon();
A.Open();
SqlDataAdapter D = new SqlDataAdapter(mysql, A);
DataSet ds = new DataSet();
D.Fill(ds, "products");
DataGrid1.DataSource = ds.Tables["products"].DefaultView;
DataGrid1.DataBind();
ds.Dispose();
A.Close();
TextBox1.Text = "";
}
}
你试试看........修改一下就可以用的,我测试过的了!
展开全部
删除的:
private void btnDelete_Click(object sender, EventArgs e)
{
SqlConnection sqlConn = new SqlConnection(strConn);
string strName = txtName.Text.Trim();
string strPassword = txtPassword.Text.Trim();
string strJob = cmbJob.Text.Trim();
try
{
sqlConn.Open();
if (txtName.Text.Trim() == "")
{
MessageBox.Show("删除用户时,请输入用户名!");
}
else
{
string strCommand = "delete from dbo.Administrator where vRole='" + strName + "'";
SqlCommand sqlComm = new SqlCommand(strCommand, sqlConn);
int i = (int)sqlComm.ExecuteNonQuery();
if (i == 1)
{
daAdministrator = new SqlDataAdapter("select * from dbo.Administrator ", sqlConn);
dsAdministrator = new DataSet();
daAdministrator.Fill(dsAdministrator, "Administrator");
dgvAdministrator.DataSource = dsAdministrator.Tables["Administrator"];
MessageBox.Show("删除成功!");
txtName.Text = "";
txtPassword.Text = "";
cmbJob.Text = "";
}
else
{
MessageBox.Show("没有这个用户,无法删除!");
}
}
}
catch (Exception ex1)
{
MessageBox.Show("删除失败,请进行检查!");
}
finally
{
sqlConn.Close();
}
}
修改的:
private void btnUpdate_Click(object sender, EventArgs e)
{
SqlConnection sqlConn = new SqlConnection(strConn);
string strName = txtName.Text.Trim();
string strPassword = txtPassword.Text.Trim();
string strJob = cmbJob.Text.Trim();
try
{
sqlConn.Open();
if (txtName.Text.Trim() == "")
{
MessageBox.Show("更新时以用户名为条件,不能为空!");
}
else
{
if (txtPassword.Text.Trim() != "")
{
string strCommand = "update dbo.Administrator set vPassword='" + strPassword + "' where vRole='" + strName + "'";
SqlCommand sqlComm = new SqlCommand(strCommand, sqlConn);
sqlComm.ExecuteNonQuery();
}
if (cmbJob.Text.Trim() != "")
{
string strCommand = "update dbo.Administrator set job='" + strJob + "' where vRole='" + strName + "'";
SqlCommand sqlComm = new SqlCommand(strCommand, sqlConn);
sqlComm.ExecuteNonQuery();
}
if (txtPassword.Text.Trim() == "" && cmbJob.Text.Trim() == "")
{
MessageBox.Show("修改信息为空,请重新赋值!");
}
else
{
MessageBox.Show("修改成功!");
daAdministrator = new SqlDataAdapter("select * from dbo.Administrator where vRole='" + strName + "'", sqlConn);
dsAdministrator = new DataSet();
daAdministrator.Fill(dsAdministrator, "Administrator");
dgvAdministrator.DataSource = dsAdministrator.Tables["Administrator"];
txtName.Text = "";
txtPassword.Text = "";
cmbJob.Text = "";
}
}
}
catch (Exception ex1)
{
MessageBox.Show("修改失败,请进行检查!");
}
finally
{
sqlConn.Close();
}
}
我用的是SQL2005的数据库
private void btnDelete_Click(object sender, EventArgs e)
{
SqlConnection sqlConn = new SqlConnection(strConn);
string strName = txtName.Text.Trim();
string strPassword = txtPassword.Text.Trim();
string strJob = cmbJob.Text.Trim();
try
{
sqlConn.Open();
if (txtName.Text.Trim() == "")
{
MessageBox.Show("删除用户时,请输入用户名!");
}
else
{
string strCommand = "delete from dbo.Administrator where vRole='" + strName + "'";
SqlCommand sqlComm = new SqlCommand(strCommand, sqlConn);
int i = (int)sqlComm.ExecuteNonQuery();
if (i == 1)
{
daAdministrator = new SqlDataAdapter("select * from dbo.Administrator ", sqlConn);
dsAdministrator = new DataSet();
daAdministrator.Fill(dsAdministrator, "Administrator");
dgvAdministrator.DataSource = dsAdministrator.Tables["Administrator"];
MessageBox.Show("删除成功!");
txtName.Text = "";
txtPassword.Text = "";
cmbJob.Text = "";
}
else
{
MessageBox.Show("没有这个用户,无法删除!");
}
}
}
catch (Exception ex1)
{
MessageBox.Show("删除失败,请进行检查!");
}
finally
{
sqlConn.Close();
}
}
修改的:
private void btnUpdate_Click(object sender, EventArgs e)
{
SqlConnection sqlConn = new SqlConnection(strConn);
string strName = txtName.Text.Trim();
string strPassword = txtPassword.Text.Trim();
string strJob = cmbJob.Text.Trim();
try
{
sqlConn.Open();
if (txtName.Text.Trim() == "")
{
MessageBox.Show("更新时以用户名为条件,不能为空!");
}
else
{
if (txtPassword.Text.Trim() != "")
{
string strCommand = "update dbo.Administrator set vPassword='" + strPassword + "' where vRole='" + strName + "'";
SqlCommand sqlComm = new SqlCommand(strCommand, sqlConn);
sqlComm.ExecuteNonQuery();
}
if (cmbJob.Text.Trim() != "")
{
string strCommand = "update dbo.Administrator set job='" + strJob + "' where vRole='" + strName + "'";
SqlCommand sqlComm = new SqlCommand(strCommand, sqlConn);
sqlComm.ExecuteNonQuery();
}
if (txtPassword.Text.Trim() == "" && cmbJob.Text.Trim() == "")
{
MessageBox.Show("修改信息为空,请重新赋值!");
}
else
{
MessageBox.Show("修改成功!");
daAdministrator = new SqlDataAdapter("select * from dbo.Administrator where vRole='" + strName + "'", sqlConn);
dsAdministrator = new DataSet();
daAdministrator.Fill(dsAdministrator, "Administrator");
dgvAdministrator.DataSource = dsAdministrator.Tables["Administrator"];
txtName.Text = "";
txtPassword.Text = "";
cmbJob.Text = "";
}
}
}
catch (Exception ex1)
{
MessageBox.Show("修改失败,请进行检查!");
}
finally
{
sqlConn.Close();
}
}
我用的是SQL2005的数据库
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
string delete ="delete 表名 where 字段=值";
SqlDataAdapter = adapter = new SqlDataAdapter(delete,连接数据库的变量)
DataSet set = new DataSet();
adapter.Fill(set);
删除的例子
修改也是一样
SQL字符串不同而已
update 表名 set 字段=值 where 字段= 条件
SqlDataAdapter = adapter = new SqlDataAdapter(delete,连接数据库的变量)
DataSet set = new DataSet();
adapter.Fill(set);
删除的例子
修改也是一样
SQL字符串不同而已
update 表名 set 字段=值 where 字段= 条件
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2012-04-15
展开全部
有关java的问题,这里都有很详细的讲解,你可以参考一下,方法有很多
参考资料:http://www.baidu.com/s?wd=%B1%B1%B7%E7%CD%F8&rsv_bp=0&rsv_spt=3&inputT=131
参考资料:http://www.baidu.com/s?wd=%B1%B1%B7%E7%CD%F8&rsv_bp=0&rsv_spt=3&inputT=131
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询