返回首页
当前位置: 主页 > 网络编程 > .Net实例教程 >

C#中的增删改查程序代码

时间:2012-06-12 16:08来源:知行网www.zhixing123.cn 编辑:麦田守望者

using System;
using System.Data;
using System.Data.SqlClient;

namespace BookManage
{
/// <summary>
/// DataAccess 的摘要说明。
/// </summary>
public class DataAccess
{
private SqlConnection conn;
private SqlDataAdapter da;
private DataSet ds;

public DataAccess()
{
this.conn = new SqlConnection("server=.;database=Book;uid=sa;pwd=;");
}

//通过表名查找的方法
public DataTable GetDataSetByTableName(string infoname)
{
string sqlselect = string.Format("select * from {0}",infoname);
this.da = new SqlDataAdapter(sqlselect,this.conn);

this.ds = new DataSet();
this.da.Fill(this.ds,infoname);

return ds.Tables[infoname];
}

//查找特定行的信息
public DataSet GetDataSetBySql(string s)
{
this.da = new SqlDataAdapter(s,this.conn);

this.ds = new DataSet();
this.da.Fill(this.ds,"oneBook");

return this.ds;
}

//更新表的特定行
public bool UpdateDataTable(string[] s,int row)
{
bool b = false;

this.da = new SqlDataAdapter("select * from bookInfo",this.conn);
this.ds = new DataSet();
this.da.Fill(this.ds,"bookInfo");

try
{
this.ds.Tables["bookInfo"].Rows[row][0] = s[0];
this.ds.Tables["bookInfo"].Rows[row][1] = s[1];
this.ds.Tables["bookInfo"].Rows[row][2] = s[2];
this.ds.Tables["bookInfo"].Rows[row][3] = s[3];
this.ds.Tables["bookInfo"].Rows[row][4] = s[4];
this.ds.Tables["bookInfo"].Rows[row][5] = s[5];


SqlCommandBuilder cb = new SqlCommandBuilder(da);

this.da.Update(this.ds,"bookInfo");
b = true;
}
catch(Exception ex)
{
b = false;
}

return b;
}

public bool insertDataTable(string[] s)
{
bool b = false;

this.da = new SqlDataAdapter("select * from bookInfo",this.conn);
this.ds = new DataSet();
this.da.Fill(this.ds,"bookInfo");

try
{
DataRow row = this.ds.Tables["bookInfo"].NewRow();

row[0] = s[0];
row[1] = s[1];
row[2] = s[2];
row[3] = s[3];
row[4] = s[4];
row[5] = s[5];

this.ds.Tables["bookInfo"].Rows.Add(row);

SqlCommandBuilder cb = new SqlCommandBuilder(da);

this.da.Update(this.ds,"bookInfo");
b = true;
}
catch(Exception ex)
{
b = false;
}

return b;
}

public bool deleteDataTable(int r)
{
bool b = false;

this.da = new SqlDataAdapter("select * from bookInfo",this.conn);
this.ds = new DataSet();
this.da.Fill(this.ds,"bookInfo");

try
{
this.ds.Tables["bookInfo"].Rows[r].Delete();

SqlCommandBuilder cb = new SqlCommandBuilder(da);

this.da.Update(this.ds,"bookInfo");
b = true;
}
catch(Exception ex)
{
b = false;
}

return b;
}

public DataTable searchByType(string s)
{
this.da = new SqlDataAdapter(s,this.conn);
this.ds = new DataSet();
this.da.Fill(this.ds,"searchInfo");

return ds.Tables["searchInfo"];
}

public bool updatePic(string s,int r)
{
bool b = false;

this.da = new SqlDataAdapter("select * from bookInfo",this.conn);
this.ds = new DataSet();
this.da.Fill(this.ds,"bookInfo");

try
{
this.ds.Tables["bookInfo"].Rows[r][4] = s;
SqlCommandBuilder cb = new SqlCommandBuilder(this.da);

this.da.Update(this.ds,"bookInfo");
this.ds.AcceptChanges();

b = true;
}
catch(Exception ex)
{
b = false;
}

return b;
}
}
}

------分隔线----------------------------
标签(Tag):C# C#实例教程 c#基础教程 C#源代码 c#技巧
------分隔线----------------------------
推荐内容
猜你感兴趣