• 大小: 4KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-07
  • 语言: C#
  • 标签: c#  sql  sql操作  

资源简介

实现增删改查功能,调用函数时候只要输入表名和条件,省去大量代码。 查询多个数据默认是用dataset接收数据

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.SqlClient;
using System.Data;
using System.Text;


namespace sqlcz
{
    class sqlcz
    {
        #region 变量

        public static string sql;
        public static SqlConnection conn = new SqlConnection(“Server=.;Database=wyh;Integrated Security=True“);
        public static SqlCommand com;
        public static SqlDataAdapter da;
        #endregion


        #region 获取单个值

        public static string getValued(string selected string table string wheretj)
        {
            try
            {
                conn.Open();
                sql = “select “ + selected + “ from “ + table + “ where “ + wheretj;
                com = conn.CreateCommand();
                com.CommandText = sql;
                string result = com.ExecuteScalar().ToString();
                conn.Close();
                return result;
            }
            catch (Exception e)
            {
                return “error“;
            }
        }
        #endregion


        #region 删除

        public static bool delete(string table string wheretj)
        {
            try
            {
                conn.Open();
                sql = “delete “ + table + “ where “ + wheretj;
                com = conn.CreateCommand();
                com.CommandText = sql;
                com.ExecuteNonQuery();
                conn.Close();
                return true;
            }
            catch(Exception e)
            {
                return false;
            }
        }
        #endregion


        #region 修改

        public static bool update(string table string sertj string wheretj)
        {
            try
            {
                conn.Open();
                sql = “update “ + table + “ set “ + sertj + “ where “ + wheretj;
                com = conn.CreateCommand();
                com.CommandText = sql;
                object result = com.ExecuteNonQuery();
                conn.Close();
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }
        #endregion


        #region 插入

        public static bool insert(string table string lie string values)
        {
            try
            {
                conn.Open();
                sql = “insert into “ + table + “(“ + lie + “)“ + “ values( “ + values + “)“;
                com = conn.CreateCommand();
                com.CommandText = sql;
                object result = com.ExecuteNonQuery();
                conn.Close();
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }
        public static bool insert(string table  string values)
        {
            try
            {
                conn.Open();
                sql = “inse

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2012-12-05 11:57  sql操作类库\
     文件        4392  2012-12-05 11:54  sql操作类库\sqlcz.cs
     文件        6144  2012-12-05 12:05  sql操作类库\sqlcz.dll

评论

共有 条评论