ds更新、插入数据库
来源:互联网 发布:奇门遁甲排盘分析软件 编辑:程序博客网 时间:2024/05/09 02:08
方法一:
//与SQL Server的连接字符串设置
private string _connString;
private string _strSql;
private SqlCommandBuilder sqlCmdBuilder;
private DataSet ds = new DataSet();
private SqlDataAdapter da;
public sqlAccess(string connString,string strSql)
...{
this._connString=connString;
}
private SqlConnection GetConn()
...{
try
...{
SqlConnection Connection = new SqlConnection(this._connString);
Connection.Open();
return Connection;
}
catch (Exception ex)
...{
MessageBox.Show(ex.Message,"数据库连接失败");
throw;
}
}
//根据输入的SQL语句检索数据库数据
public DataSet SelectDb(string strSql,string strTableName)
...{
try
...{
this._strSql = strSql;
this.da = new SqlDataAdapter(this._strSql,this.GetConn());
this.ds.Clear();
this.da.Fill(ds,strTableName);
return ds;//返回填充了数据的DataSet,其中数据表以strTableName给出的字符串命名
}
catch (Exception ex)
...{
MessageBox.Show(ex.Message,"数据库操作失败");
throw;
}
}
//数据库数据更新(传DataSet和DataTable的对象)
public DataSet UpdateDs(DataSet changedDs,string tableName)
...{
try
...{
this.da = new SqlDataAdapter(this._strSql,this.GetConn());
this.sqlCmdBuilder = new SqlCommandBuilder(da);
this.da.Update(changedDs,tableName);
changedDs.AcceptChanges();
return changedDs;//返回更新了的数据库表
}
catch (Exception ex)
...{
MessageBox.Show(ex.Message,"数据库更新失败");
throw;
}
}
private string _connString;
private string _strSql;
private SqlCommandBuilder sqlCmdBuilder;
private DataSet ds = new DataSet();
private SqlDataAdapter da;
public sqlAccess(string connString,string strSql)
...{
this._connString=connString;
}
private SqlConnection GetConn()
...{
try
...{
SqlConnection Connection = new SqlConnection(this._connString);
Connection.Open();
return Connection;
}
catch (Exception ex)
...{
MessageBox.Show(ex.Message,"数据库连接失败");
throw;
}
}
//根据输入的SQL语句检索数据库数据
public DataSet SelectDb(string strSql,string strTableName)
...{
try
...{
this._strSql = strSql;
this.da = new SqlDataAdapter(this._strSql,this.GetConn());
this.ds.Clear();
this.da.Fill(ds,strTableName);
return ds;//返回填充了数据的DataSet,其中数据表以strTableName给出的字符串命名
}
catch (Exception ex)
...{
MessageBox.Show(ex.Message,"数据库操作失败");
throw;
}
}
//数据库数据更新(传DataSet和DataTable的对象)
public DataSet UpdateDs(DataSet changedDs,string tableName)
...{
try
...{
this.da = new SqlDataAdapter(this._strSql,this.GetConn());
this.sqlCmdBuilder = new SqlCommandBuilder(da);
this.da.Update(changedDs,tableName);
changedDs.AcceptChanges();
return changedDs;//返回更新了的数据库表
}
catch (Exception ex)
...{
MessageBox.Show(ex.Message,"数据库更新失败");
throw;
}
}
1. GetConn方法创建一个数据库连接,返回SqlConnection。
2.使用的select命令中必须包含主键,这点大家都知道的!
3. this.da.Fill(ds,strTableName) 填充数据集
4.构造CommandBuilder对象时,将DataAdapter对象作为构造函数参数传入:
this.sqlCmdBuilder = new SqlCommandBuilder(da);
5. 在调用UpdateDs()更新数据库前,请检查changedDs是否已经被更新过,用changedDs.[tableName] GetChanges() != null;
6.用this.da.Update(changedDs,tableName)方法更新数据,然后调用changedDs.AcceptChanges()才能真正的更新数据库,调用 changedDs.RejectChanges() 取消更新。
方法二:
该方法可能存在性能问题
int tab1Count = ds.Tables["ruledetail_setup"].Rows.Count;
for (int i = 0; i < tab1Count; i++)
...{
string sql = "select count(*) from ruledetail_setup where type = " + type + " and ID = ";
sql += ds.Tables["ruledetail_setup"].Rows[i]["ID"];
if (Convert.ToInt32(SqlHelper.ExecuteScalar(CommandType.Text, sql, null)) != 0)
...{
string sqlUpdata = "updata ruledetail_setup set errorLimit={0},scoreInterval={1},detailRule={2},gradeTime={3} ";
sqlUpdata += "where type = " + type + " and ID = " + ds.Tables["ruledetail_setup"].Rows[i]["ID"];
String.Format(sqlUpdata,
ds.Tables["ruledetail_setup"].Rows[i]["errorLimit"],
ds.Tables["ruledetail_setup"].Rows[i]["scoreInterval"],
ds.Tables["ruledetail_setup"].Rows[i]["detailRule"],
ds.Tables["ruledetail_setup"].Rows[i]["gradeTime"]);
SqlHelper.ExecuteNonQuery(CommandType.Text, sqlUpdata, null);
}
else
...{
string sqlIns = "insert into ruledetail_setup (Type,ID,errorLimit,scoreInterval,detailRule,gradeTime) ";
sqlIns += "values ("+type+",{0},{1},{2},{3},{4})";
String.Format(sqlIns,
ds.Tables["ruledetail_setup"].Rows[i]["ID"],
ds.Tables["ruledetail_setup"].Rows[i]["errorLimit"],
ds.Tables["ruledetail_setup"].Rows[i]["scoreInterval"],
ds.Tables["ruledetail_setup"].Rows[i]["detailRule"],
ds.Tables["ruledetail_setup"].Rows[i]["gradeTime"]);
SqlHelper.ExecuteNonQuery(CommandType.Text, sqlIns, null);
}
}
for (int i = 0; i < tab1Count; i++)
...{
string sql = "select count(*) from ruledetail_setup where type = " + type + " and ID = ";
sql += ds.Tables["ruledetail_setup"].Rows[i]["ID"];
if (Convert.ToInt32(SqlHelper.ExecuteScalar(CommandType.Text, sql, null)) != 0)
...{
string sqlUpdata = "updata ruledetail_setup set errorLimit={0},scoreInterval={1},detailRule={2},gradeTime={3} ";
sqlUpdata += "where type = " + type + " and ID = " + ds.Tables["ruledetail_setup"].Rows[i]["ID"];
String.Format(sqlUpdata,
ds.Tables["ruledetail_setup"].Rows[i]["errorLimit"],
ds.Tables["ruledetail_setup"].Rows[i]["scoreInterval"],
ds.Tables["ruledetail_setup"].Rows[i]["detailRule"],
ds.Tables["ruledetail_setup"].Rows[i]["gradeTime"]);
SqlHelper.ExecuteNonQuery(CommandType.Text, sqlUpdata, null);
}
else
...{
string sqlIns = "insert into ruledetail_setup (Type,ID,errorLimit,scoreInterval,detailRule,gradeTime) ";
sqlIns += "values ("+type+",{0},{1},{2},{3},{4})";
String.Format(sqlIns,
ds.Tables["ruledetail_setup"].Rows[i]["ID"],
ds.Tables["ruledetail_setup"].Rows[i]["errorLimit"],
ds.Tables["ruledetail_setup"].Rows[i]["scoreInterval"],
ds.Tables["ruledetail_setup"].Rows[i]["detailRule"],
ds.Tables["ruledetail_setup"].Rows[i]["gradeTime"]);
SqlHelper.ExecuteNonQuery(CommandType.Text, sqlIns, null);
}
}
- ds更新、插入数据库
- 用SqlDataAdapter.Update(DataSet Ds)更新数据库
- access数据库插入更新出错
- 通过DataSet更新、插入数据库
- 数据库数据插入 更新 删除
- 数据库的插入更新语句
- Lotus 插入与更新SQL数据库
- ThinkPHP 数据库操作,插入,更新,删除,查询
- 数据库操作:添加、插入、更新语句
- 数据库操作:添加、插入、更新语句
- kohana数据库更新与插入数据
- Sqlite如果插入或更新数据库
- 数据库操作:添加、插入、更新语句
- 数据库中表的插入、更新、删除
- 数据库 - 数据更新(插入、修改、删除)
- DataAdapter 批量插入、更新、删除数据库!
- jdbc操作数据库插入、更新、删除、查找
- 数据库操作:添加、插入、更新语句
- Domino6.5重新配置
- 数据结构之线性结构
- 一个有意思的博弈问题
- “大整数阶乖”问题的递推算法
- 数据层新思路,写数据库无关的数据层 ORM在数据库内做更为合适
- ds更新、插入数据库
- html dom 教程
- 阿里妈妈广告位价值的估算
- 利用NSIS创建程序安装文件
- 行列转换的简单统计
- 设计模式学习笔记(一)——面向对象设计模式与原则
- 程序员的“爱情程序”
- 使用SQL Server管理IMail邮件账号
- test