C# 4.0 这两个组合框代码有什么问题 名称空间组合框 { 公共部分类Form1:Form { SqlConnection con=新的SqlConnection(ConfigurationManager.ConnectionString[“conectionString”].ConnectionString); 公共表格1() { 初始化组件(); con.Open(); 使用(SqlCommand command=new SqlCommand(“按地区名称顺序按地区名称从[JP\U TP\U MST]组中选择[DISTRICT\U NAME],con)) { 尝试 { SqlDataReader=command.ExecuteReader(); while(reader.Read()) { 对于(int i=0;i
在catch之后,尝试将close方法放入finally函数C# 4.0 这两个组合框代码有什么问题 名称空间组合框 { 公共部分类Form1:Form { SqlConnection con=新的SqlConnection(ConfigurationManager.ConnectionString[“conectionString”].ConnectionString); 公共表格1() { 初始化组件(); con.Open(); 使用(SqlCommand command=new SqlCommand(“按地区名称顺序按地区名称从[JP\U TP\U MST]组中选择[DISTRICT\U NAME],con)) { 尝试 { SqlDataReader=command.ExecuteReader(); while(reader.Read()) { 对于(int i=0;i,c#-4.0,C# 4.0,在catch之后,尝试将close方法放入finally函数catch(){//your exception code}finally{con.close();} 和con.Open()将其放在try函数中,因为如果连接字符串错误或与打开连接相关的任何内容,它将在catch异常中处理您看到了哪些错误? namespace Combobox { public partial class Form1 : Form { SqlConnection con = new S
catch(){//your exception code}finally{con.close();}
和con.Open()将其放在try函数中,因为如果连接字符串错误或与打开连接相关的任何内容,它将在catch异常中处理您看到了哪些错误?
namespace Combobox
{
public partial class Form1 : Form
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conectionString"].ConnectionString);
public Form1()
{
InitializeComponent();
con.Open();
using (SqlCommand command = new SqlCommand("SELECT [DISTRICT_NAME]FROM[JP_TP_MST] group by DISTRICT_NAME order by DISTRICT_NAME", con))
{
try
{
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
for (int i = 0; i < reader.FieldCount; i++)
{
comboBox1.Items.Add(reader[i].ToString());
}
}
}
catch (SqlException error)
{
MessageBox.Show(error.Message);
}
}
con.Close();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
con.Open();
using (SqlCommand command = new SqlCommand("SELECT [TALUKA_NAME]FROM[JP_TP_MST]where DISTRICT_NAME like N'" + comboBox1.Text + "%' group by [TALUKA_NAME] order by TALUKA_NAME", con))
{
try
{
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
for (int i = 0; i < reader.FieldCount; i++)
{
comboBox1.Items.Add(reader[i].ToString());
}
}
}
catch (SqlException error)
{
MessageBox.Show(error.Message);
}
}
con.Close();