我想在VS2008中,将需要的图片根据数据库中一个 风速 的实时表显示的数据显示出来,例如,风速<=15时候,自动显示A图片;风速<=20时候,自动显示B图片;风速<=25时候,自动显示C图片;等等
我写了如下代码,但是代码运行到如下地方的时候就跳过不运行了,没报错和警告,请高手看看,有什么问题。
(1)跳过不运行的代码:
if (Wind <= 15)
{
this.A_W1.Image = Image.FromFile("绿18x18.gif"); }
else if (Wind <= 20)
{
this.A_W1.Image = WindowsFormsApplication1.Properties.Resources.灰18x18; } else if (Wind <= 25)
{
this.A_W1.Load("灰18x18.gif"); } else if (Wind <= 30)
{
this.A_W1.Image = Image.FromFile("WindowsFormsApplication1.Properties.Resources\\绿18x18.gif");
} else if (Wind <= 12000)
{
this.A_W1.Image = Image.FromFile("WindowsFormsApplication1.Properties.Resources\\绿18x18.gif"); } (2)总代码: private void Form1_Paint(object sender, EventArgs e)
{ if (A_W1.Image != null)
{// 清除Picturebox的图片
A_W1.Image.Dispose();
A_W1.Image = null;
}
//设置连接字符串
string connectionString ="data source=localhost;user id=sa;pwd=12345678;database=RailWay;"; //检索WS_Real表的SQL语句
string cmdText= "select Wind_Speed from WS_Real";
//创建SqlConnection类的实例,用于连接SQL Server 数据库 SqlConnection con = new SqlConnection(connectionString);
try
{
//打开连接 con.Open(); //创建数据适配器
SqlDataAdapter da=new SqlDataAdapter(cmdText,con);
//创建DataSet,此时得到的是空数据集
DataSet ds=new DataSet(); // 填充数据集
da.Fill(ds,"WS_Real");
DataTable dt = ds.Tables["WS_Real"]; string str = dt.Rows[0][0].ToString(); int Wind = Convert.ToInt32(str); if (Wind <= 15)
{
this.A_W1.Image = Image.FromFile("绿18x18.gif"); }
else if (Wind <= 20)
{
this.A_W1.Image = WindowsFormsApplication1.Properties.Resources.灰18x18; } else if (Wind <= 25)
{
this.A_W1.Load("灰18x18.gif"); } else if (Wind <= 30)
{
this.A_W1.Image = Image.FromFile("WindowsFormsApplication1.Properties.Resources\\绿18x18.gif");
} else if (Wind <= 12000)
{
this.A_W1.Image = Image.FromFile("WindowsFormsApplication1.Properties.Resources\\绿18x18.gif"); }
} catch (Exception s)
{
Console.WriteLine(s.Message);
}
finally { con.Close(); }
}
我写了如下代码,但是代码运行到如下地方的时候就跳过不运行了,没报错和警告,请高手看看,有什么问题。
(1)跳过不运行的代码:
if (Wind <= 15)
{
this.A_W1.Image = Image.FromFile("绿18x18.gif"); }
else if (Wind <= 20)
{
this.A_W1.Image = WindowsFormsApplication1.Properties.Resources.灰18x18; } else if (Wind <= 25)
{
this.A_W1.Load("灰18x18.gif"); } else if (Wind <= 30)
{
this.A_W1.Image = Image.FromFile("WindowsFormsApplication1.Properties.Resources\\绿18x18.gif");
} else if (Wind <= 12000)
{
this.A_W1.Image = Image.FromFile("WindowsFormsApplication1.Properties.Resources\\绿18x18.gif"); } (2)总代码: private void Form1_Paint(object sender, EventArgs e)
{ if (A_W1.Image != null)
{// 清除Picturebox的图片
A_W1.Image.Dispose();
A_W1.Image = null;
}
//设置连接字符串
string connectionString ="data source=localhost;user id=sa;pwd=12345678;database=RailWay;"; //检索WS_Real表的SQL语句
string cmdText= "select Wind_Speed from WS_Real";
//创建SqlConnection类的实例,用于连接SQL Server 数据库 SqlConnection con = new SqlConnection(connectionString);
try
{
//打开连接 con.Open(); //创建数据适配器
SqlDataAdapter da=new SqlDataAdapter(cmdText,con);
//创建DataSet,此时得到的是空数据集
DataSet ds=new DataSet(); // 填充数据集
da.Fill(ds,"WS_Real");
DataTable dt = ds.Tables["WS_Real"]; string str = dt.Rows[0][0].ToString(); int Wind = Convert.ToInt32(str); if (Wind <= 15)
{
this.A_W1.Image = Image.FromFile("绿18x18.gif"); }
else if (Wind <= 20)
{
this.A_W1.Image = WindowsFormsApplication1.Properties.Resources.灰18x18; } else if (Wind <= 25)
{
this.A_W1.Load("灰18x18.gif"); } else if (Wind <= 30)
{
this.A_W1.Image = Image.FromFile("WindowsFormsApplication1.Properties.Resources\\绿18x18.gif");
} else if (Wind <= 12000)
{
this.A_W1.Image = Image.FromFile("WindowsFormsApplication1.Properties.Resources\\绿18x18.gif"); }
} catch (Exception s)
{
Console.WriteLine(s.Message);
}
finally { con.Close(); }
}
这样读取文件的方式对不对