查看内容

枚举MSSQL Server的实例

  • 2020-04-17 10:04
  • 新浦京编程
  • Views

核心提示:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;//所引入的命名空间

第20课-数据库开发及ado.net

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms;

可空值类型,资料管理器,多条件查询,Case

//所引入的命名空间 using System.Data.Sql;

SqlHelper

namespace EnumSqlServer { public partial class MainForm : Form { public MainForm() { InitializeComponent(); }

using System;

private void MainForm_Load(object sender, EventArgs e) { //枚举本地网络内所有可用SqlServer实例 SqlDataSourceEnumerator instance = SqlDataSourceEnumerator.Instance; //返回 //---------------------------------------------------------------------------- // ServerName|服务器的名称 //---------------------------------------------------------------------------- // InstanceName|服务器实例的名称。如果服务器作为默认实例运行,则为空白 //---------------------------------------------------------------------------- // IsClustered|指示服务器是否属于群集 //---------------------------------------------------------------------------- // Version|服务器的版本(SQLServer2000为8.00.x,SQLServer2005为9.00.x) //---------------------------------------------------------------------------- DataTable dt = instance.GetDataSources(); //绑定 cboServerList.DataSource = CreateCboDataSource(dt); }

using System.Collections.Generic;

/// summary /// 取得ComBox的数据源 /// /summary /// param name=dt/param /// returns/returns private Liststring CreateCboDataSource(DataTable dt) { Liststring list = new Liststring();

using System.Configuration;

foreach (DataRow dr in dt.Rows) { //只取ServerName list.Add(dr[0].ToString()); }

using System.Data;

return list; } } }

using System.Data.SqlClient;

using System.Text;

namespace _02省市联动

{

   public static  class SqlHelper

    {

       //常量 无法修改

       private const string A = "aaa";

       //readonly 是一个只读的变量,只能在声明或构造函数中修改

       //只读变量只能在构造函数或者是一开始声明变量的时候赋值,不允许在其它地方为变量赋值。

       //连接字符串

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

    

     

       /// <summary>

       /// 封装一个ExecuteNonQuery方法,执行insert、delete、updata

新浦京澳门官网,       /// </summary>

       /// <param name="sql"></param>

       /// <param name="pms"></param>

       /// <returns></returns>

       public static int ExecuteQuery(string sql,params SqlParameter[] pms)

       {

           using (SqlConnection con=new SqlConnection (conStr))

           {

               using (SqlCommand com=new SqlCommand (sql,con))

               {

                   if (pms!=null)

                   {

                       com.Parameters.AddRange(pms);

                   }

                   con.Open();

                   return com.ExecuteNonQuery();

               }

           }

       }

       /// <summary>

       /// 封装返回一个单个值的方法

       /// </summary>

       /// <returns></returns>

       public static object ExecuteScalar(string sql,params SqlParameter[] pms)

       {

           using (SqlConnection con = new SqlConnection(conStr))

           {

               using (SqlCommand com=new SqlCommand (sql,con))

               {

                   if (pms!=null)

                   {

                       com.Parameters.AddRange(pms);

                   }

                   con.Open();

                   return com.ExecuteScalar();

               }

           }

       }

       /// <summary>

       /// 封装一个返回SqlDataReader的方法

       /// </summary>

       /// <returns></returns>

       public static SqlDataReader  ExecuteReader(string sql,params SqlParameter[] pms)

       {

              SqlConnection con=new SqlConnection (conStr);

               using (SqlCommand com=new SqlCommand (sql,con))

               {

                   if (pms!=null)

                   {

                       com.Parameters.AddRange(pms);

                   }

                   try

                   {

                       con.Open();

                       return com.ExecuteReader(System.Data.CommandBehavior.CloseConnection);   

                   }

                   catch (Exception)

                   {

                       con.Close();

                       con.Dispose();

                       throw;

                   }

                   

                }

       }

       public static DataTable ExecuteDataTable(string sql,params SqlParameter[] pms)

       {

           DataTable dt = new DataTable();

           using (SqlDataAdapter adapter=new SqlDataAdapter (sql,conStr ))

           {

               //如何添加参数

               adapter.Fill(dt);

           }

           return dt;

       }

    }

}

省市联动

using System;

using System.Collections.Generic;

using System.Text;

 

namespace _02省市联动

{

    public  class ProvinceItem

    {

        public int AreaId {get ;set;}

        public string AreaName{get ;set;}

        public int AreaPid{get ;set;}

        public override string ToString()

        {

            return this.AreaName;

        }

    }

}

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Data.SqlClient;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

 

namespace _02省市联动

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)

        {

           //加载省份信息到第一个ComboBox

            LoadProvince();

            //设置俩个下拉菜单的默认值为“请选择”

            comboBox1.SelectedIndex = 0;

            comboBox2.SelectedIndex = 0;

       

        }

        private void LoadProvince()

        {

            string sql = "select * from TblArea where AreaPId  =0";

            using (SqlDataReader reader=SqlHelper .ExecuteReader(sql))

上一篇:[转]Sql Server 分页存储过程 下一篇:没有了