今天做小项目的时候,遇到这么一个问题
于是思路就是说,用count(*) 来查询数据库中是否有这个表,我用到如下代码
代码
private void button6_Click(object sender, EventArgs e)
{
SqlConnection sqlCon1 = new SqlConnection("Data Source = BTAP - SHOW;Initial Catalog = myluoyi;Integrated Security = True");
sqlCon1. Open ();
SqlCommand cmdtext = new SqlCommand(" select count ( * ) from information_schema.tables where TABLE_NAME = ' T_union" + str +" ' ", sqlCon1);
int Value = ( int )cmdtext.ExecuteScalar();
// label1. Text = "有" + Value.ToString() + "条记录";
sqlCon1. Close ();
if (Value == 0 )
{
if (MessageBox.Show("数据还未处理过,是否现在处理?", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
SqlCommand cmd1 = new SqlCommand(" select distinct A.LinkID,A.Speed,B.Length,A. Len ,A.Time,A.sampleNum,B.Class into T_union" + str + " from T_youdutwo B inner join T_yongdu A on A.LinkID = B.LinkID", sqlCon1);
DataTable dt = new DataTable("T_youduthree123");
SqlDataAdapter dAd = new SqlDataAdapter(cmd1);
dAd.Fill(dt);
sqlCon1. Close ();
MessageBox.Show("数据处理完毕!");
button6.Enabled = false;
button7.Enabled = true;
button8.Enabled = true;
button9.Enabled = true;
}
}
else
{
MessageBox.Show("数据已经处理过了,您可以直接查询对应道路等级信息了!","提示",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
button6.Enabled = false;
button7.Enabled = true;
button8.Enabled = true;
button9.Enabled = true;
}
for ( int j = 0 ; j < 2500000 ; j ++ ) { }
if (p.Value != p.Maximum)
{
p.PerformStep(); // 按照 Step 属性中指定的数量增加进度栏的值
int l_value = 100 * (p.Value - p.Minimum) / (p.Maximum - p.Minimum);
}
}
{
SqlConnection sqlCon1 = new SqlConnection("Data Source = BTAP - SHOW;Initial Catalog = myluoyi;Integrated Security = True");
sqlCon1. Open ();
SqlCommand cmdtext = new SqlCommand(" select count ( * ) from information_schema.tables where TABLE_NAME = ' T_union" + str +" ' ", sqlCon1);
int Value = ( int )cmdtext.ExecuteScalar();
// label1. Text = "有" + Value.ToString() + "条记录";
sqlCon1. Close ();
if (Value == 0 )
{
if (MessageBox.Show("数据还未处理过,是否现在处理?", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
SqlCommand cmd1 = new SqlCommand(" select distinct A.LinkID,A.Speed,B.Length,A. Len ,A.Time,A.sampleNum,B.Class into T_union" + str + " from T_youdutwo B inner join T_yongdu A on A.LinkID = B.LinkID", sqlCon1);
DataTable dt = new DataTable("T_youduthree123");
SqlDataAdapter dAd = new SqlDataAdapter(cmd1);
dAd.Fill(dt);
sqlCon1. Close ();
MessageBox.Show("数据处理完毕!");
button6.Enabled = false;
button7.Enabled = true;
button8.Enabled = true;
button9.Enabled = true;
}
}
else
{
MessageBox.Show("数据已经处理过了,您可以直接查询对应道路等级信息了!","提示",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
button6.Enabled = false;
button7.Enabled = true;
button8.Enabled = true;
button9.Enabled = true;
}
for ( int j = 0 ; j < 2500000 ; j ++ ) { }
if (p.Value != p.Maximum)
{
p.PerformStep(); // 按照 Step 属性中指定的数量增加进度栏的值
int l_value = 100 * (p.Value - p.Minimum) / (p.Maximum - p.Minimum);
}
}
注意问题的关键就在于下面这句:
select
count
(
*
)
from
information_schema.tables
where
TABLE_NAME
=
‘需要的表名字’