⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usercls.cs

📁 这是我个人写的一个实例用于登记公司的电脑信息
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Data.SqlClient;//添加命名空间
using System.Windows.Forms;
using System.Drawing;
using System.IO;
using System.Text.RegularExpressions;
//引入打印组件
using GoldPrinter;

using System.Threading;   //线程命名空间 ,启动线程进行网内IP扫描
using System.Net;  //网络命名空间 
//思想为获取IP地址的主机名与信息.如发生异常,则说明主机不可到达(关机) 


/// <summary>
/// UserCls 的摘要说明
/// 作者:殷庆飞
/// 日期:2007-7-21
/// 功能:通用类,供程序中的其它页面调用
/// 传入参数:无
/// 类名:UserCls
/// </summary>

namespace 计算机及IP管理
{
    class UserCls
    {

        public int int_RetrunRow;

        //打印组件
        GoldPrinter.MisGoldPrinter webmis = new GoldPrinter.MisGoldPrinter();


        /// <summary>
        /// 数据库连接
        /// </summary>
        /// <returns></returns>
        public SqlConnection DBConn()
        {
            SqlConnection myconn = new SqlConnection("server=(local);uid = sa;pwd=;DataBase=db_computer");
            return myconn;
        }

        /// <summary>
        /// 该方法产要用于执行insert,update,delete等SQL语句,执行成功返回true.否则返回false
        /// </summary>
        /// <param name="strSql">
        /// 参数:strSql,为string类型,所要执行的SQL语句
        /// </param>
        /// <returns>
        /// 若返回true,说明执行成功;若返回false说明执行失败
        /// </returns>
        public bool ExecSqlString(string strSql)
        {
            SqlConnection myconn = new SqlConnection("server=(local);User ID = sa;PWD=;DataBase=db_computer");
            myconn.Open();
            SqlCommand mycomm = new SqlCommand(strSql, myconn);
            try
            {
                int_RetrunRow = mycomm.ExecuteNonQuery();
                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                myconn.Close();
            }
        }

        /// <summary>
        /// 执行SQL语句,返回数据集DataSet
        /// </summary>
        /// <param name="strSql">
        /// 参数:strSql,所要执行的SQL语句
        /// </param>
        /// <param name="tbName">
        /// 参数:tbName,所要填充数据集的表名
        /// </param>
        /// <returns>
        /// </returns>
        public  DataSet GetDataSet(string strSql, string tbName)
        {
            SqlConnection myconn = new SqlConnection("server=(local);User ID = sa;PWD=;DataBase=db_computer");
            DataSet myds = new DataSet();
            try
            {
                myconn.Open();
                SqlDataAdapter myada = new SqlDataAdapter(strSql, myconn);
                myada.Fill(myds, tbName);
                return myds;
            }
            catch
            {
                myds = null;
                return myds;
            }
            finally
            {
                myconn.Close();
            }

        }

        /// <summary>
        /// 该方法产要用于绑定DataGridView控件,并通过该控件将数据表中数据在页面中显示出来。
        /// </summary>
        /// <param name="GV">
        /// 参数:GV,所要显示数据的GridView;
        /// </param>
        /// <param name="strSql">
        /// 参数:strSql,所要执行的SQL语句
        /// </param>
        /// <returns>
        /// 绑定成功返回true,失败返回false
        /// </returns>
        public bool GetDataToGridView(DataGridView GV, string strSql)
        {
            SqlConnection myconn = new SqlConnection("server=(local);User ID = sa;PWD=;DataBase=db_computer");
            myconn.Open();
            SqlDataAdapter myada = new SqlDataAdapter(strSql, myconn);
            DataSet myds = new DataSet();
            myada.Fill(myds);
            GV.DataSource = myds;
           
            try
            {
               
                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                myconn.Close();
            }
        }

        /// <summary>
        /// 该方法产要用于绑定DataGridView控件,并通过该控件将数据表中数据在页面中显示出来。
        /// </summary>
        /// <param name="GV">
        /// 参数:GV,所要显示数据的GridView;
        /// </param>
        /// <param name="strSql">
        /// 参数:strSql,所要执行的SQL语句
        /// </param>
        /// <returns>
        /// 绑定成功返回true,失败返回false
        /// </returns>
        public bool GetDataToGridView(DataGridView GV, string strSql,string strTable)
        {
            SqlConnection myconn = new SqlConnection("server=(local);User ID = sa;PWD=;DataBase=db_computer");
            myconn.Open();
            SqlDataAdapter myada = new SqlDataAdapter(strSql, myconn);
            DataSet myds = new DataSet();
            myada.Fill(myds,strTable);
            GV.DataSource = myds.Tables[strTable];

            try
            {

                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                myconn.Close();
            }
        }


        /// <summary>
        /// 给DataGridView赋值,并回传DataSet
        /// </summary>
        /// <param name="GV">DataGridView</param>
        /// <param name="strSql">查询SQL语句</param>
        /// <param name="strTable">要查询的表名</param>
        /// <param name="myada">要回传的SqlDataAdapter</param>
        /// <returns></returns>
        public bool GetDataToGridView(DataGridView GV, string strSql, string strTable, out DataSet myds)
        {
            SqlConnection myconn = new SqlConnection("server=(local);User ID = sa;PWD=;DataBase=db_computer");
            myconn.Open();
            SqlDataAdapter myada = new SqlDataAdapter(strSql, myconn);
            myds = new DataSet();
            myada.Fill(myds, strTable);
            GV.DataSource = myds.Tables[strTable];

            try
            {

                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
               myconn.Close();
            }
        }

        /// <summary>
        /// 使用SqlCommandBuilder批量更新DataGridView
        /// 注意点:   1.只能更新一个表,如果此数据集是从两个或者两个以上的表关联而来的,则不能用此方法自动更新  2.表中必须设置主键
        ///3.更新的表中字段不能有image类型的
        ///5.优点:节省代码量,节省时间,这个方法可以代替所有的:  更新/删除/插入操作语句
        ///6.缺点:访问两次数据库(select * TableName,就是这句,要确认是哪个表,除非是很大的数据量,一般是感觉不到的),效率稍微有些慢
        /// </summary>
        /// <param name="ds">DataSet数据表</param>
        /// <param name="strTblName">要更新的数据表名称</param>
        /// <returns></returns>
        public bool  DataSetUpdateByDataSet(DataSet ds,string strTblName)
        {
             SqlConnection conn = new SqlConnection("server=(local);User ID = sa;PWD=;DataBase=db_computer");
                 
             SqlDataAdapter myAdapter = new SqlDataAdapter();
             SqlCommand myCommand = new SqlCommand("select * from " + strTblName, (SqlConnection)conn);    
             myAdapter.SelectCommand = myCommand;
             SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(myAdapter);     
              try
              {  

                  lock(this)            //处理并发情况(分布式情况)
                  {

                    myAdapter.Update(ds,strTblName);  

                  }
                  return true;

              }       
             catch(Exception ex)
             {

                  conn.Close();   
                  string msg = String.Format("Invalid input.\n\n[Error={0}]", ex.Message);
                  MessageBox.Show(msg, "错误提示");

                  return false;
             }

        }


        /// <summary>
        /// 给ComboBox控件赋值 
        /// </summary>
        /// <param name="strSql">
        /// 查询SQL
        /// </param>
        /// <param name="DDLName">
        /// DropDownList控件的名称
        /// </param>
        /// /// <param name="DDLName">
        /// tbName所在查询的数据表的名称
        /// </param>
        /// <returns></returns>  
        public bool GetDataToComboBox(string strSql, ComboBox DDLName, string tbName, string strDataTextField, string strDataValueField)
        {
            SqlConnection myconn = new SqlConnection("server=(local);uid = sa;pwd=;DataBase=db_computer");
            try
            {
                myconn.Open();
                SqlDataAdapter myada = new SqlDataAdapter(strSql, myconn);
                DataSet myds = new DataSet();
                myada.Fill(myds, tbName);
                DDLName.Items.Clear();//清空里面的数据后再给其赋值 
                if (myds.Tables[tbName].Rows.Count > 0)//说明有符合条件的记录
                {
                    DDLName.DataSource = myds.Tables[tbName];
                    DDLName.DisplayMember = strDataTextField;
                }
                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                myconn.Close();
            }
        }


        //打印DataGridView
        //参数说明:1.dataGridView1:表示要打印的内容
        //          2.PrintTitle打印时要显示的标题
        public void PrintData(DataGridView dataGridView1, string PrintTitle)
        {
            webmis.Title = PrintTitle;   //标题,还可设置子标题
            webmis.Footer = System.DateTime.Now.ToString();
            (webmis.Title as GoldPrinter.Title).Font = new System.Drawing.Font("宋体", 12, System.Drawing.FontStyle.Bold);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -