初学C#;
以下一段程序,如在界面中直接写执行没问题。
我把它放在一个类中调用
dbsobj是有数据的并非null,
UpdateByDataSet(dbsobj,string strTblName,string strConnection)
出现'myAdapter.Update(ds,strTblName)'执行ds 未实例化
请问如何在调用涵数时传递 DataSet public int UpdateByDataSet(DataSet ds,string strTblName,string strConnection)
{
try
{
SqlConnection conn = new SqlConnection(strConnection));
SqlDataAdapter myAdapter = new SqlDataAdapter();
SqlCommand myCommand = new SqlCommand("select * from "+strTblName),(SqlConnection)this.conn);
myAdapter.SelectCommand = myCommand;
SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(myAdapter);
myAdapter.Update(ds,strTblName);
return 0;
}
catch(BusinessException errBU)
{
throw errBU;
}
catch(Exception err)
{
throw new BusinessException(err);
}
}
0 我要推荐
以下一段程序,如在界面中直接写执行没问题。
我把它放在一个类中调用
dbsobj是有数据的并非null,
UpdateByDataSet(dbsobj,string strTblName,string strConnection)
出现'myAdapter.Update(ds,strTblName)'执行ds 未实例化
请问如何在调用涵数时传递 DataSet public int UpdateByDataSet(DataSet ds,string strTblName,string strConnection)
{
try
{
SqlConnection conn = new SqlConnection(strConnection));
SqlDataAdapter myAdapter = new SqlDataAdapter();
SqlCommand myCommand = new SqlCommand("select * from "+strTblName),(SqlConnection)this.conn);
myAdapter.SelectCommand = myCommand;
SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(myAdapter);
myAdapter.Update(ds,strTblName);
return 0;
}
catch(BusinessException errBU)
{
throw errBU;
}
catch(Exception err)
{
throw new BusinessException(err);
}
}
0 我要推荐
DataSet ds=new DataSet();
//调用方法:
UpdateByDataSet(ds,"ds表名",conn)
//加上面2行代码就行了public int UpdateByDataSet(DataSet ds,string strTblName,string strConnection)
{
try
{
SqlConnection conn = new SqlConnection(strConnection));
SqlDataAdapter myAdapter = new SqlDataAdapter();
SqlCommand myCommand = new SqlCommand("select * from "+strTblName),(SqlConnection)this.conn);
myAdapter.SelectCommand = myCommand;
SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(myAdapter);
myAdapter.Update(ds,strTblName);
return 0;
}
catch(BusinessException errBU)
{
throw errBU;
}
catch(Exception err)
{
throw new BusinessException(err);
}
}
public int UpdateByDataSet(DataSet ds,string strTblName,string strConnection)需要传递三个参数,
第一个是DataSet类型的参数,你将上面代码建立DataSet实例名ds写进去就行;
第二个是string类型的参数,是你要将数据填充给你的DataSet实例ds的表明;
第三个是string类型的参数,就是你的数据库连接字符串
给方法传递三个参数就行了
。主要是想放在类中多个Winform中调用可少写代码。
6楼说的三个参数都有。调试执行ds 未实例化,提示用new
我在 Try 前面加一句 DataSet Ds=new DataSet(ds.ds.DataSetName); 后面的ds改为Ds
变得没有提示,泥牛入海无消息,也不会更新数据库
还请高手指教
在百度上经较 落水神 指点
不用DataSet传递,改用DataTable传递数据,相应代码改一下,就OK了。