技巧百问(3):asp.net连接sql和access的方法?
1.sql类 DataView
public DataView getview(string query,string connstr,int startrcd,int pagesize)
{
SqlConnection connection=new SqlConnection(connstr);
SqlDataAdapter myadp=new SqlDataAdapter(query,connection);
DataSet tableset=new DataSet();
myadp.Fill(tableset,"alltable");
myadp.Fill(tableset,startrcd,pagesize,"istable");
DataView isview=new DataView();
isview.Table=tableset.Tables["alltable"];
maxselect=isview.Count;
isview.Table=tableset.Tables["istable"];
return isview;
tableset.Tables[0].Select();
}
{
SqlConnection connection=new SqlConnection(connstr);
SqlDataAdapter myadp=new SqlDataAdapter(query,connection);
DataSet tableset=new DataSet();
myadp.Fill(tableset,"alltable");
myadp.Fill(tableset,startrcd,pagesize,"istable");
DataView isview=new DataView();
isview.Table=tableset.Tables["alltable"];
maxselect=isview.Count;
isview.Table=tableset.Tables["istable"];
return isview;
tableset.Tables[0].Select();
}
DataReader
public SqlDataReader getreader(string query,string connstr)
{
SqlConnection connection=new SqlConnection(connstr);
SqlCommand mycom=new SqlCommand(query,connection);
connection.Open();
SqlDataReader isreader=mycom.ExecuteReader();
return isreader;
}
{
SqlConnection connection=new SqlConnection(connstr);
SqlCommand mycom=new SqlCommand(query,connection);
connection.Open();
SqlDataReader isreader=mycom.ExecuteReader();
return isreader;
}
Nonequery
public int nonequery(string query,string connstr)
{
SqlConnection connection=new SqlConnection(connstr);
SqlCommand command=new SqlCommand(query,connection);
connection.Open();
int n=command.ExecuteNonQuery();
connection.Close();
return n;
}
{
SqlConnection connection=new SqlConnection(connstr);
SqlCommand command=new SqlCommand(query,connection);
connection.Open();
int n=command.ExecuteNonQuery();
connection.Close();
return n;
}
连接Access的就是该哈名称就是了。我是弄成类,很方便。