C#中对象名无效
publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}publicintNowPage=1,...
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public int NowPage = 1, PageCount = 1, PageSize = 5;
public string AddSql="";
private void findbtn_Click(object sender, EventArgs e)
{
string FindStr = findstr.Text;
if (FindStr != "") AddSql = "where(UserName like '%" + FindStr + "%' or Address like '%" + FindStr + "%' or Email like '%" + FindStr + "%')";
NowPage = 1;
PageCount = 1;
LoadData(sender, e);
}
private void p_btn_Click(object sender, EventArgs e)
{
NowPage--;
LoadData(sender, e);
}
private void n_btn_Click(object sender, EventArgs e)
{
NowPage++;
LoadData(sender, e);
}
private void LoadData(object sender, EventArgs e)
{
string Afile = "Initial Catalog=Northwind;Data Source=DINGDING;Integrated Security=SSPI";
SqlConnection mycon = new SqlConnection(Afile);
if (mycon.State.ToString() == "Closed") mycon.Open();
SqlCommand Countcmd = new SqlCommand("select count(*) as co from UserTable", mycon);
SqlDataReader Countdr = Countcmd.ExecuteReader();
if (Countdr.Read())
{
PageCount = Int32.Parse(Countdr["co"].ToString()) / PageSize;
if (Int32.Parse(Countdr["co"].ToString()) % PageSize > 0) PageCount = PageCount + 1;
if (PageCount < 1) PageCount = 1;
}
mycon.Close();
pageinfo.Text = "共" + PageCount + "页\t第" + NowPage + "页";
if (NowPage >= PageCount) NowPage = PageCount;
if (NowPage <= 1) NowPage = 1;
int start = (NowPage - 1) * PageSize;
SqlDataAdapter sda = new SqlDataAdapter("select * from UserTable" + AddSql + " order by ID", mycon);
DataSet ds = new DataSet();
sda.Fill(ds, start, PageSize, "search");
dataGrid1.DataSource = ds.Tables["search"].DefaultView;
dataGrid1.SetDataBinding(ds, "search");
}
private void dataGrid1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
emailstr.Text=dataGrid1[dataGrid1.CurrentCell.RowNumber,5].ToString().Trim();
addressstr.Text=dataGrid1[dataGrid1.CurrentCell.RowNumber,6].ToString().Trim();
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“userTableDataSet.UserTable”中。您可以根据需要移动或移除它。
this.userTableTableAdapter.Fill(this.userTableDataSet.UserTable);
// TODO: 这行代码将数据加载到表“dataSet1.a”中。您可以根据需要移动或移除它。
this.aTableAdapter.Fill(this.dataSet1.a);
}
SqlDataReader Countdr = Countcmd.ExecuteReader();
这里他说对象名无效~~各位请教请教了~~~ 展开
{
public Form1()
{
InitializeComponent();
}
public int NowPage = 1, PageCount = 1, PageSize = 5;
public string AddSql="";
private void findbtn_Click(object sender, EventArgs e)
{
string FindStr = findstr.Text;
if (FindStr != "") AddSql = "where(UserName like '%" + FindStr + "%' or Address like '%" + FindStr + "%' or Email like '%" + FindStr + "%')";
NowPage = 1;
PageCount = 1;
LoadData(sender, e);
}
private void p_btn_Click(object sender, EventArgs e)
{
NowPage--;
LoadData(sender, e);
}
private void n_btn_Click(object sender, EventArgs e)
{
NowPage++;
LoadData(sender, e);
}
private void LoadData(object sender, EventArgs e)
{
string Afile = "Initial Catalog=Northwind;Data Source=DINGDING;Integrated Security=SSPI";
SqlConnection mycon = new SqlConnection(Afile);
if (mycon.State.ToString() == "Closed") mycon.Open();
SqlCommand Countcmd = new SqlCommand("select count(*) as co from UserTable", mycon);
SqlDataReader Countdr = Countcmd.ExecuteReader();
if (Countdr.Read())
{
PageCount = Int32.Parse(Countdr["co"].ToString()) / PageSize;
if (Int32.Parse(Countdr["co"].ToString()) % PageSize > 0) PageCount = PageCount + 1;
if (PageCount < 1) PageCount = 1;
}
mycon.Close();
pageinfo.Text = "共" + PageCount + "页\t第" + NowPage + "页";
if (NowPage >= PageCount) NowPage = PageCount;
if (NowPage <= 1) NowPage = 1;
int start = (NowPage - 1) * PageSize;
SqlDataAdapter sda = new SqlDataAdapter("select * from UserTable" + AddSql + " order by ID", mycon);
DataSet ds = new DataSet();
sda.Fill(ds, start, PageSize, "search");
dataGrid1.DataSource = ds.Tables["search"].DefaultView;
dataGrid1.SetDataBinding(ds, "search");
}
private void dataGrid1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
emailstr.Text=dataGrid1[dataGrid1.CurrentCell.RowNumber,5].ToString().Trim();
addressstr.Text=dataGrid1[dataGrid1.CurrentCell.RowNumber,6].ToString().Trim();
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“userTableDataSet.UserTable”中。您可以根据需要移动或移除它。
this.userTableTableAdapter.Fill(this.userTableDataSet.UserTable);
// TODO: 这行代码将数据加载到表“dataSet1.a”中。您可以根据需要移动或移除它。
this.aTableAdapter.Fill(this.dataSet1.a);
}
SqlDataReader Countdr = Countcmd.ExecuteReader();
这里他说对象名无效~~各位请教请教了~~~ 展开
4个回答
展开全部
看看你文件最上面有
using System.Data.SqlClient;
么
using System.Data.SqlClient;
么
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Countdr 换一个名字试一试 比如read
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
需要引用头文件
using System.Data.SqlClient;
using System.Data;
这两个,你看你的有没有,没有的话,你写上就正确了!
using System.Data.SqlClient;
using System.Data;
这两个,你看你的有没有,没有的话,你写上就正确了!
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你的sqldatareader对象没有关闭
不是很确定
不是很确定
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询