using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/*****************************
* 概要:SqlService数据库操作
* 设计者:DuanXuWen
* 设计时间:20180309
* 版本:0.1
* 修改者:
* 修改时间:
* ***************************/
namespace Common
{
public class SqlHelper
{
/// <summary>
/// 链接配置
/// </summary>
public static string connectionString = ConfigurationManager.AppSettings[\"ConnectionString\"].ToString();
/// <summary>
/// 返回DataTable
/// </summary>
/// <param name=\"strSql\"></param>
/// <returns></returns>
public static DataTable GetDataTable(string strSql)
{
try
{
using (SqlConnection con = new SqlConnection(connectionString))
{
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(strSql, con);
da.Fill(ds);
return ds.Tables[0];
}
}
catch (Exception ex)
{
string msg = \"数据库相关错误!详细内容:操作语句(\" + strSql + \")\";
string logAddress = System.Windows.Forms.Application.StartupPath + \"\\\\Log\\\\ErrorMsg\\\\\" + DateTime.Now.ToString(\"yyyyMMdd\") + \".log\";
LogHelper.WriteLog(ex, logAddress, true, msg);
return null;
}
}
/// <summary>
/// 执行sql,返回true 成功,false 失败
/// </summary>
/// <param name=\"strSql\"></param>
/// <returns></returns>
public static bool ExecuteSql(string strSql)
{
try
{
using (SqlConnection con = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(strSql, con);
con.Open();
return command.ExecuteNonQuery() >= 0;
}
}
catch (Exception ex)
{
string msg = \"数据库相关错误!详细内容:操作语句(\" + strSql + \")\";
string logAddress = System.Windows.Forms.Application.StartupPath + \"\\\\Log\\\\ErrorMsg\\\\\" + DateTime.Now.ToString(\"yyyyMMdd\") + \".log\";
LogHelper.WriteLog(ex, logAddress, true, msg);
return false;
}
}
/// <summary>
/// 执行sql,返回 对象
/// </summary>
/// <param name=\"strSql\"></param>
/// <returns></returns>
public static ExecuteScalar(string strSql)
{
try
{
using (SqlConnection con = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(strSql, con);
con.Open();
return command.ExecuteScalar();
}
}
catch (Exception ex)
{
string msg = \"数据库相关错误!详细内容:操作语句(\" + strSql + \")\";
string logAddress = System.Windows.Forms.Application.StartupPath + \"\\\\Log\\\\ErrorMsg\\\\\" + DateTime.Now.ToString(\"yyyyMMdd\") + \".log\";
LogHelper.WriteLog(ex, logAddress, true, msg);
return null;
}
}
}
}
继续阅读与本文标签相同的文章
java线程与OS线程
Flutter跨平台技术——承包移动互联网的未来
-
凯撒加密法
2026-05-18栏目: 教程
-
第一次对外出售芯片!华为宣布出售海思Balong 711
2026-05-18栏目: 教程
-
又一外卖巨头躺平,曾让美团饿了么后背发凉,网友:头回听说
2026-05-18栏目: 教程
-
SpaceX公司刚刚申请在轨道上再运行3万颗星链卫星
2026-05-18栏目: 教程
-
第三讲,Ceph内部构件
2026-05-18栏目: 教程
