省县市三级联动小bug
using System;using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using System.Data.SqlClient;
namespace ADONET
{
/// <summary>
/// sanjiliandong.xaml 的交互逻辑
/// </summary>
public partial class sanjiliandong : Window
{
public sanjiliandong()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
DataTable table = SqlHelper.ExecuteDataTable("select * from AreaFull where AreaPid = 0");
List<Area> listPro = new List<Area>();
foreach(DataRow row in table.Rows){
Area area = new Area();
area.AreaId = (int)row["AreaId"];
area.AreaName= (string)row["AreaName"];
listPro.Add(area);
}
lbPro.ItemsSource = listPro;
}
private void lbPro_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Area AreaPro =(Area)lbPro.SelectedItem;
DataTable table = SqlHelper.ExecuteDataTable("select * from AreaFull where AreaPid = @AreaPid", new SqlParameter("@AreaPid",AreaPro.AreaId));
List<Area> listCity = new List<Area>();
foreach (DataRow row in table.Rows)
{
Area area = new Area();
area.AreaId = (int)row["AreaId"];
area.AreaName = (string)row["AreaName"];
listCity.Add(area);
}
lbCity.ItemsSource = listCity;
}
private void lbCity_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Area AreaCity = (Area)lbCity.SelectedItem;
DataTable table = SqlHelper.ExecuteDataTable("select * from AreaFull where AreaPid = @AreaPid", new SqlParameter("@AreaPid", AreaCity.AreaId));
List<Area> listXian = new List<Area>();
foreach (DataRow row in table.Rows)
{
Area area = new Area();
area.AreaId = (int)row["AreaId"];
area.AreaName = (string)row["AreaName"];
listXian.Add(area);
}
lbXian.ItemsSource = listXian;
}
}
}
做出来效果以后 点击省再点县市以后再选省 会出错,这个是因为什么资源没有释放的原因 还是什么原因 ,怎么解决这个??、 SqlHelper没有关闭把? datetable用完也要释放一下 Dispose ibcadmin 发表于 2013-9-3 19:54
SqlHelper没有关闭把? datetable用完也要释放一下 Dispose
public static DataTable ExecuteDataTable(string sql,params SqlParameter[] parameters)
{
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = sql;
cmd.Parameters.AddRange(parameters);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet dataset = new DataSet();
adapter.Fill(dataset);
return dataset.Tables;
}
}
}
这个是我的那个代码 ,using使用完了会自动释放资源不是吗 ,不需要close了吧 ibcadmin 发表于 2013-9-3 19:54
SqlHelper没有关闭把? datetable用完也要释放一下 Dispose
你看看如何改,添一笔我试试 你再using中 return dataset.Tables;
你再using还没走完就return了 我不知道这样行不行
你最好吧return写在using外面吧
ibcadmin 发表于 2013-9-4 08:21
你再using中 return dataset.Tables;
你再using还没走完就return了 我不知道这样行不行
不是这个缘故啊 那就是你把相同的数据源绑定到了同一个下拉框 tablerow重复吧下了班晚上回去给你看下 ibcadmin 发表于 2013-9-4 13:17
那就是你把相同的数据源绑定到了同一个下拉框 tablerow重复吧下了班晚上回去给你看下
嗯 晚上我也回来看看
页:
[1]