C# SqlDataAdapter.Update的用法
一般都是这么用的SqlConnectionconn=newSqlConnection("SERVER=.;UID=sa;PWD=sa;DATABASE=pubs");Sq...
一般都是这么用的
SqlConnection conn = new SqlConnection("SERVER=.;UID=sa;PWD=sa;DATABASE=pubs");
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM sales",conn);
SqlCommandBuilder cmdbuilder = new SqlCommandBuilder(adapter);
DataSet ds=new DataSet();
adapter.Fill(ds);
adapter.UpdateCommand = cmdbuilder.GetUpdateCommand();
adapter.Update(ds,ds.Table[0].TableName);
我想知道有没有别的用法,我主要是不想再更新之前还要检索一遍,不检索不可以吗? 展开
SqlConnection conn = new SqlConnection("SERVER=.;UID=sa;PWD=sa;DATABASE=pubs");
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM sales",conn);
SqlCommandBuilder cmdbuilder = new SqlCommandBuilder(adapter);
DataSet ds=new DataSet();
adapter.Fill(ds);
adapter.UpdateCommand = cmdbuilder.GetUpdateCommand();
adapter.Update(ds,ds.Table[0].TableName);
我想知道有没有别的用法,我主要是不想再更新之前还要检索一遍,不检索不可以吗? 展开
1个回答
展开全部
可以啊,你自己将它写成一个类就可以了。
例如:
public DataSet GetData(string selecttext, SqlParameter[] parmes, string keyset, string tablename, Boolean g)
{
try
{
connection = new SqlConnection(connectionstring);
dataadapter = new SqlDataAdapter();
dataadapter.SelectCommand = this.CommandFile(selecttext, parmes, g);
SqlCommandBuilder SCB = new SqlCommandBuilder(dataadapter);
ds = new DataSet();
dataadapter。fill(ds,tablename);
}
catch { }
return ds;
}
返回ds,填充表格。
public Boolean updatate(string dstablename, string selecttext, Boolean g, DataTable dt)
{
Boolean f = false;
int i = 0;
try
{
if (selecttext != null)
{
connection = new SqlConnection(connectionstring);
dataadapter = new SqlDataAdapter();
dataadapter.SelectCommand = this.CommandFile(selecttext, null, g);
SqlCommandBuilder SCB = new SqlCommandBuilder(dataadapter);
}
if (dstablename != null)
{
i = dataadapter.Update(ds.Tables[dstablename]);
}
if (dt != null)
{
i = dataadapter.Update(dt);
}
if (i > 0)
{
f = true;
}
}
catch (Exception ex) { }
return f;
}
根据实际情况来写入参数,调用不同的更新。
例如:
public DataSet GetData(string selecttext, SqlParameter[] parmes, string keyset, string tablename, Boolean g)
{
try
{
connection = new SqlConnection(connectionstring);
dataadapter = new SqlDataAdapter();
dataadapter.SelectCommand = this.CommandFile(selecttext, parmes, g);
SqlCommandBuilder SCB = new SqlCommandBuilder(dataadapter);
ds = new DataSet();
dataadapter。fill(ds,tablename);
}
catch { }
return ds;
}
返回ds,填充表格。
public Boolean updatate(string dstablename, string selecttext, Boolean g, DataTable dt)
{
Boolean f = false;
int i = 0;
try
{
if (selecttext != null)
{
connection = new SqlConnection(connectionstring);
dataadapter = new SqlDataAdapter();
dataadapter.SelectCommand = this.CommandFile(selecttext, null, g);
SqlCommandBuilder SCB = new SqlCommandBuilder(dataadapter);
}
if (dstablename != null)
{
i = dataadapter.Update(ds.Tables[dstablename]);
}
if (dt != null)
{
i = dataadapter.Update(dt);
}
if (i > 0)
{
f = true;
}
}
catch (Exception ex) { }
return f;
}
根据实际情况来写入参数,调用不同的更新。
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询