高手急救:
  
   我用的c# window form,cs结构,急求数据处理架构(新增,删除,修改)代码,vb的也可以,
   主要是单表或一对多表的处理方法,最好用DataGrid实现,望有这方面的代码的朋友多多支持,分不够再加!
   谢谢!

解决方案 »

  1.   

    微软的DataAccess block 里面封装好了一些数据处理的类,你可以去下载;
      

  2.   

    给你一个类哈,记得给我加分哦,呵呵 ^_^
    using System;
    using System.Data;
    using System.Data.SqlClient;namespace Library.Common
    {
    /// <summary>
    /// 数据库操作:添加,修改,删除,查询.
    /// </summary>
    public class DBOperator
    {
    private string DataBase;
    private string Table;
    private SqlConnection conn;
    private DataSet ds;
    private SqlDataAdapter adapt; public DBOperator()
    {
    //
    // TODO: 在此处添加构造函数逻辑
    //
    } /// <summary>
    /// 构造函数传值
    /// </summary>
    /// <param name="DB">数据库名</param>
    /// <param name="TABLE">表名</param>
    public DBOperator(string DB,string TABLE)
    {
    DataBase=DB;
    Table=TABLE;
    } /// <summary>
    /// 获取或设置DataSet的值
    /// </summary>
    public DataSet DS
    {
    get
    {
    return ds;
    }
    set
    {
    ds=value;
    }
    } /// <summary>
    /// 连接数据库
    /// </summary>
    public void Connecting()
    {
    string strCon="Data Source=(local);Integrated Security=SSPI;Initial Catalog="+DataBase+"";
    try
    {
    conn=new SqlConnection(strCon);
    conn.Open();
    }
    catch(Exception exc)
    {
    conn.Close();
    throw exc;
    }
    } /// <summary>
    /// 初始化DataSet列表
    /// </summary>
    public void Initialization()
    {
    this.Connecting();
    string cond="select * from "+Table+"";
    try
    {
    adapt=new SqlDataAdapter(cond,conn);
    SqlCommandBuilder builder=new SqlCommandBuilder(adapt);
    ds=new DataSet();
    adapt.Fill(ds,"New"+Table);
    }
    catch(Exception exc)
    {
    throw exc;
    }
    finally
    {
    conn.Close();
    }
    } /// <summary>
    /// 查询
    /// </summary>
    /// <param name="condition">条件</param>
    public void Initialization(string condition)
    {
    this.Connecting();
    string cond="select * from "+Table+" "+condition;
    try
    {
    adapt=new SqlDataAdapter(cond,conn);
    SqlCommandBuilder builder=new SqlCommandBuilder(adapt);
    ds=new DataSet();
    adapt.Fill(ds,"New"+Table);
    }
    catch(Exception exc)
    {
    throw exc;
    }
    finally
    {
    conn.Close();
    }
    } /// <summary>
    /// 添加数据
    /// </summary>
    /// <param name="row">添加的行</param>
    public void Insert(object[] row)
    {
    Initialization();
    try
    {
    ds.Tables["New"+Table].Rows.Add(row);
    adapt.Update(ds,"New"+Table);
    }
    catch(Exception exc)
    {
    throw exc;
    } } /// <summary>
    /// 删除数据
    /// </summary>
    /// <param name="primarykeys">主键名</param>
    /// <param name="keys">键值</param>
    public void Delete(DataColumn[] primarykeys,object[] keys)
    {
    Initialization();
    try
    {
    ds.Tables["New"+Table].PrimaryKey=primarykeys; DataRow row=ds.Tables["New"+Table].Rows.Find(keys);
    row.Delete();
    adapt.Update(ds,"New"+Table);
    }
    catch(Exception exc)
    {
    throw exc;
    }
    } /// <summary>
    /// 修改
    /// </summary>
    /// <param name="columnname">列名</param>
    /// <param name="columnvalue">新的值</param>
    /// <param name="condition">条件</param>
    public void Modify(string columnname,string columnvalue,string condition)
    {
    Connecting();
    string cmd="update "+Table+" set "+columnname+" = "+columnvalue+" where "+condition;
    SqlCommand command=new SqlCommand(cmd,conn);
    command.ExecuteNonQuery();
    }

    }
    }
      

  3.   

    B/S的,你改改就可以了:
    页面:<%@ Page language="c#" Codebehind="SingerSearch.aspx.cs" AutoEventWireup="false" Inherits="adminnew.Singer.SingerSearch" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>SingerSearch</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    <LINK href="../css/style.css" type="text/css" rel="stylesheet">
    </HEAD>
    <body MS_POSITIONING="GridLayout" onload="parent.search.cols='179,76%';">
    <form id="Form1" method="post" runat="server">
    <FONT face="宋体">
    <TABLE id="Table1" cellSpacing="1" cellPadding="1" width="100%" border="1">
    <TR>
    <TD>
    <asp:Label id="Label1" runat="server">歌手管理:</asp:Label>
    <asp:Label id="Label2" runat="server">歌手搜索引擎</asp:Label></TD>
    </TR>
    <TR>
    <TD>
    <asp:Label id="lb_Desc" runat="server">请输入搜索条件</asp:Label></TD>
    </TR>
    <TR>
    <TD>
    <TABLE id="Table2" cellSpacing="1" cellPadding="1" width="100%" border="1">
    <TR>
    <TD>
    <asp:Label id="Label3" runat="server">歌手名:</asp:Label></TD>
    <TD>
    <asp:TextBox id="tb_SingerName" runat="server"></asp:TextBox></TD>
    <TD>
    <asp:Label id="Label4" runat="server">类型:</asp:Label></TD>
    <TD>
    <asp:DropDownList id="ddl_AttachTypeID" runat="server"></asp:DropDownList></TD>
    <TD>
    <asp:Label id="Label5" runat="server">歌手类别:</asp:Label></TD>
    <TD>
    <asp:DropDownList id="ddl_SingerTypeID" runat="server"></asp:DropDownList></TD>
    <TD>
    <asp:Button id="bt_Search" runat="server" Text="搜索"></asp:Button></TD>
    </TR>
    </TABLE>
    </TD>
    </TR>
    <TR>
    <TD>
    <asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False" CellPadding="3" BackColor="White"
    BorderWidth="1px" BorderStyle="None" BorderColor="#CCCCCC" Width="100%">
    <FooterStyle ForeColor="#000066" BackColor="White"></FooterStyle>
    <SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#669999"></SelectedItemStyle>
    <ItemStyle ForeColor="#000066"></ItemStyle>
    <HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#006699"></HeaderStyle>
    <Columns>
    <asp:BoundColumn DataField="SingerID" HeaderText="SingerID"></asp:BoundColumn>
    <asp:TemplateColumn HeaderText="歌手名">
    <ItemTemplate>
    <FONT face="宋体">
    <asp:TextBox id="tb_Name" runat="server" MaxLength="100"></asp:TextBox></FONT>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="类型">
    <ItemTemplate>
    <asp:DropDownList id="ddl_TypeID" runat="server"></asp:DropDownList>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="歌手类别">
    <ItemTemplate>
    <asp:DropDownList id="ddl_SingerID" runat="server"></asp:DropDownList>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="排序">
    <ItemTemplate>
    <asp:TextBox id="tb_SingerOrder" runat="server" Width="40px" MaxLength="4"></asp:TextBox>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="相关内容">
    <ItemTemplate>
    <FONT face="宋体">
    <asp:HyperLink id="hl_SingerID" runat="server">相关内容</asp:HyperLink></FONT>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="修改">
    <ItemTemplate>
    <asp:Button id="bt_Modify" runat="server" Text="修改" CommandName="update"></asp:Button>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="删除">
    <ItemTemplate>
    <FONT face="宋体">
    <asp:ImageButton id="ib_del" runat="server" ImageUrl="../images/del.gif" CommandName="delete"></asp:ImageButton></FONT>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    <PagerStyle HorizontalAlign="Left" ForeColor="#000066" BackColor="White" Mode="NumericPages"></PagerStyle>
    </asp:DataGrid>
    <asp:Label id="lb_Result" runat="server" ForeColor="Red"></asp:Label></TD>
    </TR>
    </TABLE>
    </FONT>
    </form>
    </body>
    </HTML>
      

  4.   

    .cs:using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;namespace adminnew.Singer
    {
    /// <summary>
    /// SingerSearch 的摘要说明。
    /// </summary>
    public class SingerSearch : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Label Label1;
    protected System.Web.UI.WebControls.Label Label2;
    protected System.Web.UI.WebControls.DataGrid DataGrid1;
    protected System.Web.UI.WebControls.Label Label3;
    protected System.Web.UI.WebControls.Label Label4;
    protected System.Web.UI.WebControls.Label Label5;
    protected System.Web.UI.WebControls.Label lb_Desc;
    private DataTable dt1;
    protected System.Web.UI.WebControls.Button bt_Search;
    protected System.Web.UI.WebControls.Label lb_Result;
    protected System.Web.UI.WebControls.TextBox tb_SingerName;
    protected System.Web.UI.WebControls.DropDownList ddl_AttachTypeID;
    protected System.Web.UI.WebControls.DropDownList ddl_SingerTypeID;
    private DataTable dt2;

    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    SSP.SingerManage sm=new SSP.SingerManage();
    dt1=sm.GetConfig_AttachType();
    dt2=sm.GetSingerTypeList(); if(!IsPostBack)
    {
    BindDropDownList();
    BindGrid();
    }
    } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.bt_Search.Click += new System.EventHandler(this.bt_Search_Click);
    this.DataGrid1.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_ItemCommand);
    this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion private void BindDropDownList()
    {
    this.ddl_AttachTypeID.DataTextField="AttachDesc";
    this.ddl_AttachTypeID.DataValueField="AttachTypeID";
    this.ddl_AttachTypeID.DataSource=dt1.DefaultView;
    this.ddl_AttachTypeID.DataBind();
    this.ddl_AttachTypeID.Items.Insert(0,new ListItem("全部","0")); this.ddl_SingerTypeID.DataTextField="SingerTypeName";
    this.ddl_SingerTypeID.DataValueField="SingerTypeID";
    this.ddl_SingerTypeID.DataSource=dt2.DefaultView;
    this.ddl_SingerTypeID.DataBind();
    this.ddl_SingerTypeID.Items.Insert(0,new ListItem("全部" ,"0")); } private void BindGrid()
    {
    SSP.SingerManage sm=new SSP.SingerManage();

    string filter="";

    if(this.tb_SingerName.Text!=null && this.tb_SingerName.Text.Trim()!="")
    {
    filter="where A.SingerName like'%'+'"+tb_SingerName.Text.Trim()+"'+'%' ";
    }

    if(this.ddl_AttachTypeID.SelectedIndex>0)
    {

    if(filter.StartsWith("where"))
    {

    filter+="and A.AttachTypeID="+this.ddl_AttachTypeID.SelectedItem.Value;                    
    }
    else
    {
    filter="where A.AttachTypeID="+this.ddl_AttachTypeID.SelectedItem.Value;
    }
    }
    if(this.ddl_SingerTypeID.SelectedIndex>0)
    {

    if(filter.StartsWith("where"))
    {
    filter+="and A.SingerTypeID="+this.ddl_SingerTypeID.SelectedItem.Value;                    
    }
    else
    {
    filter="where A.SingerTypeID="+this.ddl_SingerTypeID.SelectedItem.Value;
    }

    }

    DataTable dt=sm.GetSearchSingerList(filter);
    if(dt.Rows.Count<=0)
    {
    this.lb_Desc.Text="<font color='red'>没有你要查找的结果!</font>";
    this.DataGrid1.Visible=false;
    return;

    }
    else
    {
    this.lb_Desc.Text="<font color=''>请输入搜索条件:</font>";
    this.DataGrid1.Visible=true;
    this.DataGrid1.DataKeyField="SingerID";
    this.DataGrid1.DataSource=dt.DefaultView;
    this.DataGrid1.DataBind();
    for(int i=0;i<dt.Rows.Count;i++)
    {
    TextBox tb=(TextBox)DataGrid1.Items[i].Cells[1].Controls[1];
    tb.Text=dt.Rows[i]["SingerName"].ToString();
    tb=(TextBox)DataGrid1.Items[i].Cells[4].Controls[1];
    tb.Text=dt.Rows[i]["SingerOrder"].ToString();


    DropDownList ddl1=(DropDownList)DataGrid1.Items[i].Cells[2].Controls[1];

    ddl1.DataTextField="AttachDesc";
    ddl1.DataValueField="AttachTypeID";
    ddl1.DataSource=dt1.DefaultView;
    ddl1.DataBind();
    ddl1.SelectedIndex=ddl1.Items.IndexOf(ddl1.Items.FindByValue(dt.Rows[i]["AttachTypeID"].ToString())); DropDownList ddl2=(DropDownList)DataGrid1.Items[i].Cells[3].Controls[1];

    ddl2.DataTextField="SingerTypeName";
    ddl2.DataValueField="SingerTypeID";
    ddl2.DataSource=dt2.DefaultView;
    ddl2.DataBind();
    ddl2.SelectedIndex=ddl2.Items.IndexOf(ddl2.Items.FindByValue(dt.Rows[i]["SingerTypeID"].ToString())); }
    }
    } private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {

    if(e.Item.ItemIndex>-1)
    {
    int SingerID=Convert.ToInt32(DataGrid1.DataKeys[e.Item.ItemIndex]);
    SSP.SingerManage sm=new SSP.SingerManage();

    if(e.CommandName=="delete")
    {
    sm.DeleteSinger(SingerID);
    this.lb_Result.Text="";
    this.Page.RegisterClientScriptBlock("refresh","<script>parent.frames[1].location='SingerTree.aspx'</script>");
    }
    else if(e.CommandName=="update")
    {
    TextBox tb1=(TextBox)DataGrid1.Items[e.Item.ItemIndex].Cells[1].Controls[1];
    TextBox tb2=(TextBox)DataGrid1.Items[e.Item.ItemIndex].Cells[4].Controls[1];

    DropDownList ddl1=(DropDownList)DataGrid1.Items[e.Item.ItemIndex].Cells[2].Controls[1];
    DropDownList ddl2=(DropDownList)DataGrid1.Items[e.Item.ItemIndex].Cells[3].Controls[1];


    if(tb1.Text==null || tb1.Text.Trim()=="")
    {
    this.lb_Result.Text="SingerID号是:"+SingerID+"歌手名输入框必须输入!";
    return; } int order=0;
    if(tb2.Text!=null && tb2.Text.Trim().Length!=0)
    {
    try
    {
    order=Convert.ToInt32(tb2.Text.Trim());
    }
    catch
    {

    this.lb_Result.Text="请在SingerID号是:"+SingerID+"排序栏输入框中输入数值!";
    return;
    }

    }

    if(sm.UpdateSingerInfo(SingerID,tb1.Text.Trim(),order,Convert.ToInt32(ddl1.SelectedItem.Value),Convert.ToInt32(ddl2.SelectedItem.Value)))
    {
    this.lb_Result.Text="SingerID号是:"+SingerID+"修改成功!";
    this.Page.RegisterClientScriptBlock("refresh","<script>parent.frames[1].location='SingerTree.aspx'</script>");
    }
    else
    {
    this.lb_Result.Text="SingerID号是:"+SingerID+"修改失败!";
    }
    }

    this.BindGrid(); }
    } private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    if(e.Item.ItemIndex>-1)
    {
    ((System.Web.UI.WebControls.ImageButton)(e.Item.Cells[DataGrid1.Columns.Count-1].FindControl("ib_del"))).Attributes.Add ("onclick","return confirm (\"确定要删除此项记录吗?\");"); int SingerID=Convert.ToInt32(DataGrid1.DataKeys[e.Item.ItemIndex]);
    System.Web.UI.WebControls.HyperLink hl=((System.Web.UI.WebControls.HyperLink)e.Item.FindControl("hl_SingerID"));
    hl.NavigateUrl="../Search/SearchMain.aspx?SingerID="+SingerID; }
    } private void bt_Search_Click(object sender, System.EventArgs e)
    {
    this.BindGrid();
    this.lb_Result.Text="";
    } }
    }
      

  5.   

    不会吧,有没有application form的,经典一点的哟
      

  6.   

    是不是在Winform中的DataGrid中操作数据?
    http://blog.csdn.net/zhzuo/archive/2004/08/06/67016.aspx
    http://blog.csdn.net/zhzuo/archive/2004/08/06/67037.aspx