加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

CSharp之使用SqlHelper类操作SqlServer,省市连动

发布时间:2020-12-12 13:06:43 所属栏目:MsSql教程 来源:网络整理
导读:1.前台界面 2.后台代码 2.1封装后的SqlHelper using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Configuration; using System.Data.SqlClient; namespace _04省市联动 { ? ? public class SqlHelper ?


1.前台界面

2.后台代码

2.1封装后的SqlHelper

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Configuration;

using System.Data.SqlClient;


namespace _04省市联动

{

? ? public class SqlHelper

? ? {

? ? ? ? //读取连接字符串

? ? ? ? private static readonly string str = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;


? ? ? ? //三个方法

? ? ? ? /// <summary>

? ? ? ? /// 此方法可以做增删改

? ? ? ? /// </summary>

? ? ? ? /// <param name="sql">sql语句</param>

? ? ? ? /// <param name="ps">sql语句中的参数</param>

? ? ? ? /// <returns>返回受影响的行数,int类型</returns>

? ? ? ? public static int ExecuteNonQuery(string sql,SqlParameter[] ps)

? ? ? ? {

? ? ? ? ? ? //连接数据库

? ? ? ? ? ? using (SqlConnection con=new SqlConnection(str))

? ? ? ? ? ? {

? ? ? ? ? ? ? ? using (SqlCommand cmd=new SqlCommand(sql,con))

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? con.Open();

? ? ? ? ? ? ? ? ? ? if (ps!=null)

? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? cmd.Parameters.AddRange(ps);

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ??

? ? ? ? ? ? ? ? ? ? return cmd.ExecuteNonQuery();

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ??

? ? ? ? ? ? }

? ? ? ? }


? ? ? ? /// <summary>

? ? ? ? /// 该方法用在查询上

? ? ? ? /// <returns>返回首行首列</returns>

? ? ? ? public static object ExecuteScalar(string sql,SqlParameter[] ps)

? ? ? ? {

? ? ? ? ? ? using (SqlConnection con=new SqlConnection(str))

? ? ? ? ? ? {

? ? ? ? ? ? ? ? using (SqlCommand cmd=new SqlCommand(sql,con))

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? con.Open();

? ? ? ? ? ? ? ? ? ? if (ps!=null)

? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? cmd.Parameters.AddRange(ps);

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? return cmd.ExecuteScalar();


? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? /// <summary>

? ? ? ? /// 该方法用于查询读取数据

? ? ? ? /// </summary>

? ? ? ? /// <param name="sql">sql语句</param>

? ? ? ? /// <param name="ps">sql语句中的参数</param>

? ? ? ? /// <returns>返回的是SqlDataReader对象,里面有数据</returns>

? ? ? ? public static SqlDataReader ExecuteDataReader(string sql,params SqlParameter[] ps)

//Params是C#中的关键字,采用此关键字可以指定参数数目为可变;在方法声明中的 params 关键字之后不允许任何其他参数,并且在方法声明中只允许一个 params 关键字

? ? ? ? {

? ? ? ? ? ? SqlConnection con=new SqlConnection(str);

? ? ? ? ? ? using (SqlCommand cmd=new SqlCommand(sql,con))

? ? ? ? ? ? {

? ? ? ? ? ? ? ? if (ps!=null)

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? cmd.Parameters.AddRange(ps);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? try

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? con.Open();

? ? ? ? ? ? ? ? ? ? //此时SqlDataReader不能加using,否则reader对象会被释放掉,无法返回

? ? ? ? ? ? ? ? ? ? return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);//返回SqlDataReader对象,并关闭对应的SqlConnection对象

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?


? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? catch (Exception ex)

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? con.Close();

? ? ? ? ? ? ? ? ? ? con.Dispose(); ? ? //如果发生异常,手动释放

? ? ? ? ? ? ? ? ? ? throw ex;

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? }

? ? }

}


2.2主窗体后台代码

namespace _04省市联动

{

? ? public partial class Form1 : Form

? ? {

? ? ? ? public Form1()

? ? ? ? {

? ? ? ? ? ? InitializeComponent();

? ? ? ? }



? ? ? ? private int r = 0;

? ? ? ? private void Form1_Load(object sender,EventArgs e)

? ? ? ? {

? ? ? ? ? ? r = 1;

? ? ? ? ? ? //加载省份信息

? ? ? ? ? ? LoadAreaByAreaPId(0);

? ? ? ? }


? ? ? ? ? ? //所有省份显示到第一个下拉框中

? ? ? ? private void LoadAreaByAreaPId(int v)

? ? ? ? {

? ? ? ? ? ? List<Area> list=new List<Area>();

? ? ? ? ? ? list.Add(new Area() {AreaId = -1,AreaName = "---请选择---"});

? ? ? ? ? ? string sql = "select AreaId,AreaName from TblArea ?where AreaPId="+v;

? ? ? ? ? ? using (SqlDataReader reader=SqlHelper.ExecuteDataReader(sql))

? ? ? ? ? ? {

? ? ? ? ? ? ? ? if (reader.HasRows)

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? while (reader.Read())

? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? Area a=new Area();

? ? ? ? ? ? ? ? ? ? ? ? a.AreaId = Convert.ToInt32(reader["AreaId"]);

? ? ? ? ? ? ? ? ? ? ? ? a.AreaName = reader["AreaName"].ToString();

? ? ? ? ? ? ? ? ? ? ? ? list.Add(a);

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? ? ? if (r==1)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? cbPro.DataSource = list;

? ? ? ? ? ? ? ? cbPro.DisplayMember = "AreaName";

? ? ? ? ? ? ? ? cbPro.ValueMember = "AreaId";

? ? ? ? ? ? }

? ? ? ? ? ? if(r==2)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? cbCity.DataSource = list;

? ? ? ? ? ? ? ? cbCity.DisplayMember = "AreaName";

? ? ? ? ? ? }

? ? ? ? }



? ? ? ?//当省份下拉框文本改变时,加载对应的城市信息

? ? ? ? private void cbPro_SelectedIndexChanged(object sender,EventArgs e)

? ? ? ? {

? ? ? ? ? ? #region 练习用的代码

? ? ? ? ? ? //if (cbPro.SelectedIndex != 0)

? ? ? ? ? ? //{

? ? ? ? ? ? // ? ?cbCity.Items.Clear();

? ? ? ? ? ? // ? ?//获取当前省份的id

? ? ? ? ? ? // ? ?int id = Convert.ToInt32(cbPro.SelectedValue);

? ? ? ? ? ? // ? ?string sql = "select AreaId,AreaName from TblArea where AreaPId=" + id;

? ? ? ? ? ? // ? ?//List<Area> list = new List<Area>();

? ? ? ? ? ? // ? ?using (SqlDataReader reader = SqlHelper.ExecuteDataReader(sql))

? ? ? ? ? ? // ? ?{

? ? ? ? ? ? // ? ? ? ?if (reader.HasRows)

? ? ? ? ? ? // ? ? ? ?{

? ? ? ? ? ? // ? ? ? ? ? ?while (reader.Read())

? ? ? ? ? ? // ? ? ? ? ? ?{

? ? ? ? ? ? // ? ? ? ? ? ? ? ?Area a = new Area();

? ? ? ? ? ? // ? ? ? ? ? ? ? ?a.AreaId = Convert.ToInt32(reader["AreaId"]);

? ? ? ? ? ? // ? ? ? ? ? ? ? ?a.AreaName = reader["AreaName"].ToString();

? ? ? ? ? ? // ? ? ? ? ? ? ? ?cbCity.Items.Add(a); ?//重写了Area类的ToString()方法 ? public override string ToString()

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//否则添加的是类的命名空间 ? ? ? ? ?{

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?return this.AreaName;

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? // ? ? ? ? ? ?}

? ? ? ? ? ? // ? ? ? ?}

? ? ? ? ? ? // ? ?}



? ? ? ? ? ? //}?

? ? ? ? ? ? #endregion



? ? ? ? ? ? #region 简化写法,使用r变量判断事件

? ? ? ? ? ? r = 2;

? ? ? ? ? ? if (cbPro.SelectedIndex != 0)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? int id = Convert.ToInt32(cbPro.SelectedValue);

? ? ? ? ? ? ? ? LoadAreaByAreaPId(id);

? ? ? ? ? ? }?

? ? ? ? ? ? #endregion

? ? ? ? }

? ? }

}

欢迎关注趣味CSharp,完整笔记与您分享~~~~~~~~

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读