• SQL数据库连接


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    using System.Data.SqlClient;
    using System.Data.SqlTypes;
    using System.Web;
    using System.Web.Configuration;
    using System.Configuration;
    
    namespace DAL
    {
        public class cls_SQLFunction
        {
            string connectionString = "";
            System.Configuration.ConnectionStringSettings connString = System.Configuration.ConfigurationManager.ConnectionStrings["K3ConnectionString"];
    
            /// <summary>
            /// 读取数据库
            /// </summary>
            /// <param name="ComTxt">数据库查询语句</param>
            /// <param name="TableName">表名</param>
            /// <returns>查询结果——数据集</returns>
            public DataSet Read_DataSet(string ComTxt, string TableName)
            {
                try
                {
                    if (null != connString)
                    {
                        connectionString = connString.ConnectionString;
                    }
                    SqlConnection con;
                    SqlCommand selectcmd;
                    con = new SqlConnection(connectionString);
                    con.Open();
                    selectcmd = new SqlCommand();
                    selectcmd.CommandText = ComTxt;
                    selectcmd.CommandTimeout = 1800;
                    SqlDataAdapter adapter = new SqlDataAdapter(selectcmd.CommandText, con);
                    adapter.SelectCommand.CommandTimeout = 1800;
                    DataSet ds = new DataSet();
                    adapter.Fill(ds, TableName);
                    DataTable dt = ds.Tables[TableName];
                    con.Close();
                    return ds;
                }
                catch
                {
                    DataSet ds = new DataSet();
                    ds = null;
                    return ds;
                }
            }
    
            //--------------------------------------------------------------------------------
            /// <summary>
            /// 插入、删除、修改数据库
            /// </summary>
            /// <param name="ComTxt">SQL语句</param>
            public bool SQLUpdate(string ComTxt)
            {
                try
                {
                    if (null != connString)
                    {
                        connectionString = connString.ConnectionString;
                    }
                    SqlConnection con;
                    SqlCommand selectcmd;
                    con = new SqlConnection(connectionString);
                    con.Open();
                    selectcmd = new SqlCommand();
                    selectcmd.Connection = con;
                    selectcmd.CommandText = ComTxt;  //执行TSQL、表名、存储过程、一段SQL语句
                    selectcmd.ExecuteNonQuery();
                    con.Close();
                    return true;
                }
                catch
                {
                    return false;
                }
            }
    
            //--------------------------------------------------------------------------------
            /// <summary>
            /// 数据库二进制数据写入
            /// </summary>
            /// <param name="ComTxt">SQL语句</param>
            /// <param name="File">文件二进制流</param>
            /// <returns>是否写入成功</returns>
            public bool SQLUpdate_Photo(string ComTxt, byte[] File)
            {
                try
                {
                    if (null != connString)
                    {
                        connectionString = connString.ConnectionString;
                    }
                    SqlConnection con;
                    SqlCommand selectcmd;
                    con = new SqlConnection(connectionString);
                    con.Open();
                    selectcmd = new SqlCommand(ComTxt, con);
                    selectcmd.Parameters.Add("@File", SqlDbType.Image);
                    selectcmd.Parameters[0].Value = File;
                    selectcmd.ExecuteNonQuery();
                    con.Close();
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }
    }
    

      

  • 相关阅读:
    JavaScript父子页面之间的相互调用
    把一个集合自定转成json字符串
    分页类与前台和后台的调用方法
    移动App测试实战—专项测试
    功能测试需求分析方法
    App弱网测试
    App常见产品问题及预防方法
    21天,搞定软件测试从业者必备的Linux命令
    Docker入门
    Linux核心-慧测课堂笔记
  • 原文地址:https://www.cnblogs.com/laojiefang/p/2491142.html
Copyright © 2020-2023  润新知