c#提取SQL Server 数据库的数据,雨水计算练习

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;//
using System.Data.Sql;//
using System.Data.SqlClient;//

namespace 省份
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

    private void Form1_Load(object sender, EventArgs e)
    {
        string str1 = "select name from sysobjects where xtype='u'";//提取所有表名
        string constr = "Server=LWH;Database=rain;user=sa;password=********";//连接数据库
        SqlConnection conn = new SqlConnection(constr);//
        ArrayList list = new ArrayList();
        try
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand(str1, conn);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                list.Add(dr.GetValue(0));
              }
         comboBox1.DataSource = list;
             }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        //finally
        //{ conn.Close(); }
    }
    private string province;//定义省份字段
    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        province = comboBox1.SelectedItem.ToString();//将comboBox1省份名称提取出来,赋与方法体外面字段
        string str2 = "select * from" + " " + province;//这个不同
        string constr = "Server=LWH;Database=rain;user=sa;password=********";//连接数据库
        SqlConnection conn = new SqlConnection(constr);//
        ArrayList list = new ArrayList();
        try
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand(str2,conn);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                list.Add(dr.GetValue(1));//这个不同
            }

           

            comboBox2.DataSource = list;//这个不同
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        finally
        { conn.Close(); }
    }

    private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
    {
        string Xcity = comboBox2.SelectedItem.ToString();
        string str3 = "select A_pr,B_Pr,C_pr,n_pr from" + " "+ province + " "+ "where city='"+Xcity+"'";
        string constr = "Server=LWH;user=sa;Database=rain;pwd=********";
        SqlConnection conn = new SqlConnection(constr);
        try
        {
            conn.Open();
            SqlCommand com = new SqlCommand(str3,conn);
            SqlDataReader dr = com.ExecuteReader();
            while (dr.Read())
            { label_A_PR.Text = dr.GetValue(0).ToString();
              label_B_PR.Text = dr.GetValue(1).ToString();
              label_C_PR.Text = dr.GetValue(2).ToString();
              label_n_PR.Text = dr.GetValue(3).ToString();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }
}

}
数据库截图
c#提取SQL Server 数据库的数据,雨水计算练习_第1张图片
c#提取SQL Server 数据库的数据,雨水计算练习_第2张图片
c#提取SQL Server 数据库的数据,雨水计算练习_第3张图片
c#提取SQL Server 数据库的数据,雨水计算练习_第4张图片
Form1截图
c#提取SQL Server 数据库的数据,雨水计算练习_第5张图片
运行截图
c#提取SQL Server 数据库的数据,雨水计算练习_第6张图片
c#提取SQL Server 数据库的数据,雨水计算练习_第7张图片
稍稍优化后的代码:
namespace 省份
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private SqlDataReader der;//私有字段
public SqlDataReader Der
{
get
{
return der;
}

        set
        {
            der = value;
        }
    }//属性

    public SqlDataReader  ReadFromdatabase(string strselect)//方法
    {
        string constr = "Server=LWH;Database=rain;user=sa;password=*******";//连接数据库,字符串
        SqlConnection conn = new SqlConnection(constr);//建立与数据库连接
        try
        {
            conn.Open();//打开数据库
            SqlCommand com = new SqlCommand(strselect,conn);//注入命令
            Der = com.ExecuteReader();//执行SqlCommand方法, 执行命令

        }
           catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        return Der;
    }
   
    private void Form1_Load(object sender, EventArgs e)
    {
        string str1 = "select name from sysobjects where xtype='u'";//提取所有表名
        ArrayList list = new ArrayList();//声明动态集合
        ReadFromdatabase(str1);
      while (Der.Read())
        {
                list.Add(Der.GetValue(0));
         }
         comboBox1.DataSource = list;
       }

    private string province;//定义省份字段

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        province = comboBox1.SelectedItem.ToString();//将comboBox1省份名称提取出来,赋与方法体外面字段
        string str2 = "select * from" + " " + province;
        ArrayList list = new ArrayList();
        ReadFromdatabase(str2);
        while (Der.Read())
            {
                list.Add(Der.GetValue(1));
            }
             comboBox2.DataSource = list;
       }

    private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
    {
        string Xcity = comboBox2.SelectedItem.ToString();
        string str3 = "select A_pr,B_Pr,C_pr,n_pr from" + " "+ province + " "+ "where city='"+Xcity+"'";
        ReadFromdatabase(str3);
        while (Der.Read())
            { label_A_PR.Text = Der.GetValue(0).ToString();
              label_B_PR.Text = Der.GetValue(1).ToString();
              label_C_PR.Text = Der.GetValue(2).ToString();
              label_n_PR.Text = Der.GetValue(3).ToString();
            }
      }
}

}

你可能感兴趣的:(c#提取SQL Server 数据库的数据,雨水计算练习)