C#中tabpages 存储问题 关联数据库
需要达到的效果:查询数据库里XXX表里的XXX列XXX列有很多行内容组成经过判断内容的行数tabControl增加相应行数的tabpage并逐一添加每行内容显示在Text...
需要达到的效果:
查询数据库里XXX表里的XXX列
XXX列有很多行内容组成
经过判断内容的行数
tabControl 增加相应行数的tabpage并逐一添加每行内容显示在Text上.
通过 输入新内容添加到数据库的XXX列里,并且同时添加新tabpage在tabcontrol上
再次打开软件时tabpage包括原有数据和新加内容的tabpage
自己做到的程度:
private void dataView2Type()
{
//找到数据的行数
string sql1 = string.Format("select foodTypeId from foodTypeTable order by foodTypeId desc");
int count = 0;
try
{
SqlCommand command = new SqlCommand(sql1, DBhelper.connection);
DBhelper.connection.Open();
SqlDataReader dataReader = command.ExecuteReader();
if (dataReader.Read())
{
count = Convert.ToInt32(dataReader[0]);
}
dataReader.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
DBhelper.connection.Close();
}
//按行数添加
for (int i = 1; i <= count; i++)
{
string sql2 = string.Format("select * from dbo.foodTypeTable where foodTypeId = " + i);
try
{
SqlCommand command = new SqlCommand(sql2, DBhelper.connection);
DBhelper.connection.Open();
SqlDataReader dataReader = command.ExecuteReader();
if (dataReader.Read())
{
this.tabPage2.Text = dataReader["foodTypeName"].ToString();
}
dataReader.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
DBhelper.connection.Close();
}
}
}
//在循环里只能添加一个tabpage
string foodName = this.textBox11.Text.Trim();
int add = 0;
string sql = string.Format("select foodTypeName from dbo.foodTypeTable where foodTypeName = '{0}'", foodName);
try
{
SqlCommand command = new SqlCommand(sql, DBhelper.connection);
DBhelper.connection.Open();
SqlDataReader dataReader = command.ExecuteReader();
if (!dataReader.Read())//没找到的时候
{
this.tabControl1.TabPages.Add(foodName);
add = 1;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{ DBhelper.connection.Close(); }
//添加后是暂时性的,软件重启就又恢复了
为什么无人解答呢?盼啊~~~~~ 展开
查询数据库里XXX表里的XXX列
XXX列有很多行内容组成
经过判断内容的行数
tabControl 增加相应行数的tabpage并逐一添加每行内容显示在Text上.
通过 输入新内容添加到数据库的XXX列里,并且同时添加新tabpage在tabcontrol上
再次打开软件时tabpage包括原有数据和新加内容的tabpage
自己做到的程度:
private void dataView2Type()
{
//找到数据的行数
string sql1 = string.Format("select foodTypeId from foodTypeTable order by foodTypeId desc");
int count = 0;
try
{
SqlCommand command = new SqlCommand(sql1, DBhelper.connection);
DBhelper.connection.Open();
SqlDataReader dataReader = command.ExecuteReader();
if (dataReader.Read())
{
count = Convert.ToInt32(dataReader[0]);
}
dataReader.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
DBhelper.connection.Close();
}
//按行数添加
for (int i = 1; i <= count; i++)
{
string sql2 = string.Format("select * from dbo.foodTypeTable where foodTypeId = " + i);
try
{
SqlCommand command = new SqlCommand(sql2, DBhelper.connection);
DBhelper.connection.Open();
SqlDataReader dataReader = command.ExecuteReader();
if (dataReader.Read())
{
this.tabPage2.Text = dataReader["foodTypeName"].ToString();
}
dataReader.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
DBhelper.connection.Close();
}
}
}
//在循环里只能添加一个tabpage
string foodName = this.textBox11.Text.Trim();
int add = 0;
string sql = string.Format("select foodTypeName from dbo.foodTypeTable where foodTypeName = '{0}'", foodName);
try
{
SqlCommand command = new SqlCommand(sql, DBhelper.connection);
DBhelper.connection.Open();
SqlDataReader dataReader = command.ExecuteReader();
if (!dataReader.Read())//没找到的时候
{
this.tabControl1.TabPages.Add(foodName);
add = 1;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{ DBhelper.connection.Close(); }
//添加后是暂时性的,软件重启就又恢复了
为什么无人解答呢?盼啊~~~~~ 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询