using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Configuration;using System.Data;using System.Data.SqlClient;namespace demo{ public abstract class SqlServerHelper { public static string ConnStr
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Configuration; using System.Data; using System.Data.SqlClient; namespace demo { public abstract class SqlServerHelper { public static string ConnString = string.Empty; public static string Conn_Config_Str_Name = string.Empty; public static string Conn_Server = string.Empty; public static string Conn_DBName = string.Empty; public static string Conn_Uid = string.Empty; public static string Conn_Pwd = string.Empty; private static string _ConnString { get { if (!string.IsNullOrEmpty(ConnString)) return ConnString; object oConn = ConfigurationManager.ConnectionStrings[Conn_Config_Str_Name]; if (oConn != null && oConn.ToString() != "") return oConn.ToString(); return string.Format(@"server={0};database={1};uid={2};password={3}", Conn_Server, Conn_DBName, Conn_Uid, Conn_Pwd); } } // 测试连接 public static bool TestConn() { SqlConnection myConn = null; bool bResult = false; try { myConn = new SqlConnection(_ConnString); myConn.Open(); } catch (Exception ex) { } finally { if (myConn != null && myConn.State.ToString() == "Open") bResult = true; } myConn.Close(); return bResult; } // 取datatable public static DataTable GetDataTable(out string sError, string sSQL) { DataTable dt = null; sError = string.Empty; try { SqlConnection conn = new SqlConnection(_ConnString); SqlCommand comm = new SqlCommand(); comm.Connection = conn; comm.CommandText = sSQL; SqlDataAdapter dapter = new SqlDataAdapter(comm); dt = new DataTable(); dapter.Fill(dt); } catch (Exception ex) { sError = ex.Message; } return dt; } // 取dataset public static DataSet GetDataSet(out string sError, string sSQL) { DataSet ds = null; sError = string.Empty; try { SqlConnection conn = new SqlConnection(_ConnString); SqlCommand comm = new SqlCommand(); comm.Connection = conn; comm.CommandText = sSQL; SqlDataAdapter dapter = new SqlDataAdapter(comm); ds = new DataSet(); dapter.Fill(ds); } catch (Exception ex) { sError = ex.Message; } return ds; } // 取某个单一的元素 public static object GetSingle(out string sError, string sSQL) { DataTable dt = GetDataTable(out sError, sSQL); if (dt != null && dt.Rows.Count > 0) { return dt.Rows[0][0]; } return null; } // 取最大的ID public static Int32 GetMaxID(out string sError, string sKeyField, string sTableName) { DataTable dt = GetDataTable(out sError, "select isnull(max([" + sKeyField + "]),0) as MaxID from [" + sTableName + "]"); if (dt != null && dt.Rows.Count > 0) { return Convert.ToInt32(dt.Rows[0][0].ToString()); } return 0; } // 执行 insert,update,delete 动作,也可以使用事务 public static bool UpdateData(out string sError, string sSQL, bool bUseTransaction = false) { int iResult = 0; sError = string.Empty; if (!bUseTransaction) { try { SqlConnection conn = new SqlConnection(_ConnString); if (conn.State != ConnectionState.Open) conn.Open(); SqlCommand comm = new SqlCommand(); comm.Connection = conn; comm.CommandText = sSQL; iResult = comm.ExecuteNonQuery(); } catch (Exception ex) { sError = ex.Message; iResult = -1; } } else // 使用事务 { SqlTransaction trans = null; try { SqlConnection conn = new SqlConnection(_ConnString); if (conn.State != ConnectionState.Open) conn.Open(); trans = conn.BeginTransaction(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = sSQL; cmd.Transaction = trans; iResult = cmd.ExecuteNonQuery(); trans.Commit(); } catch (Exception ex) { sError = ex.Message; iResult = -1; trans.Rollback(); } } return iResult > 0; } } }
调用方法:
一,先设置数据库连接的信息
//SqlServerHelper.ConnString = @"server=电脑名 或 电脑IP;database=数据库名;uid=数据库登录名;password=数据库登录密码";
SqlServerHelper.Conn_Config_Str_Name = @"ConnString"; // ConnString的信息在 App.Config里设置 //SqlServerHelper.Conn_Server = @"电脑名 或 电脑IP"; //SqlServerHelper.Conn_DBName = "数据库名"; //SqlServerHelper.Conn_Uid = "数据库登录名"; //SqlServerHelper.Conn_Pwd = "数据库登录密码";
二, App.Config
三, 读取 datatable / dataset 数据
private void InitGrid() {
string sSQL = "select * from test";
string sError = string.Empty;
DataTable dt = SqlServerHelper.GetDataTable(out sError, sSQL);
//DataSet dt = SqlServerHelper.GetDataSet(out sError, sSQL);
dataGridView1.DataSource = dt;
if (!string.IsNullOrEmpty(sError)) Common.DisplayMsg(this.Text, sError);
}
四,插入,修改,删除 数据 (都调用SqlServerHelper.UpdateData方法)
// 插入
string sError = string.Empty; int iMaxID = SqlServerHelper.GetMaxID(out sError, "id", "test") + 1; string sSql = "insert into test select " + iMaxID + ",'name" + iMaxID + "','remark" + iMaxID + "'"; sError = string.Empty; bool bResult = SqlServerHelper.UpdateData(out sError, sSql, true); if (bResult) Common.DisplayMsg(this.Text, "插入成功"); else Common.DisplayMsg(this.Text, sError);
InitGrid();
// 修改
sError = string.Empty; int iMaxID = SqlServerHelper.GetMaxID(out sError, "id", "test"); string sSql = "update test set name='name_jonse',remark='remark_jonse' where id=" + iMaxID; sError = string.Empty; bool bResult = SqlServerHelper.UpdateData(out sError, sSql, true); if (bResult) Common.DisplayMsg(this.Text, "修改成功"); else Common.DisplayMsg(this.Text, sError);
InitGrid();
// 删除
sError = string.Empty; int iMaxID = SqlServerHelper.GetMaxID(out sError, "id", "test"); string sSql = "delete from test where id=" + iMaxID; sError = string.Empty; bool bResult = SqlServerHelper.UpdateData(out sError, sSql); if (bResult) Common.DisplayMsg(this.Text, "删除成功"); else Common.DisplayMsg(this.Text, sError);
InitGrid();
五,其它
public static void DisplayMsg(string sCaption, string sMsg) { sMsg = sMsg.TrimEnd('!').TrimEnd('!') + " !"; MessageBox.Show(sMsg, sCaption); }

对于 SQL Server 数据库中已存在同名对象,需要采取以下步骤:确认对象类型(表、视图、存储过程)。如果对象为空,可使用 IF NOT EXISTS 跳过创建。如果对象有数据,使用不同名称或修改结构。使用 DROP 删除现有对象(谨慎操作,建议备份)。检查架构更改,确保没有引用删除或重命名的对象。

当 SQL Server 服务无法启动时,可采取以下步骤解决:检查错误日志以确定根本原因。确保服务帐户具有启动服务的权限。检查依赖项服务是否正在运行。禁用防病毒软件。修复 SQL Server 安装。如果修复不起作用,重新安装 SQL Server。

要查看 SQL Server 端口号:打开 SSMS,连接到服务器。在对象资源管理器中找到服务器名称,右键单击它,然后选择“属性”。在“连接”选项卡中,查看“TCP 端口”字段。

SQL Server 数据库文件通常存储在以下默认位置:Windows: C:\Program Files\Microsoft SQL Server\MSSQL\DATALinux: /var/opt/mssql/data可通过修改数据库文件路径设置来自定义数据库文件位置。

问题发现这次使用的是SqlServer数据库,之前并没有使用过,但是问题不大,我按照需求文档的步骤连接好SqlServer之后,启动SpringBoot项目,发现了一个报错,如下:刚开始我以为是SqlServer连接问题呢,于是便去查看数据库,发现数据库一切正常,我首先第一时间问了我的同事,他们是否有这样的问题,发现他们并没有,于是我便开始了我最拿手的环节,面向百度编程。开始解决具体报错信息是这样,于是我便开始了百度报错:ERRORc.a.d.p.DruidDataSource$CreateCo

SQL Server 英文安装可通过以下步骤更改为中文:下载相应语言包;停止 SQL Server 服务;安装语言包;更改实例语言;更改用户界面语言;重启应用程序。

有网友反馈,在win11上无法安装sqlserver这款软件,不知道是怎么回事,根据目前的测试来看,win11存在硬盘问题,部分接口硬盘无法安装这款软件。win11为啥不能安装sqlserver:答:win11不能安装sqlserver是硬盘的问题。1、据了解,win11存在对于硬盘的检测bug。2、这导致sqlserver无法在“三星m.2接口”硬盘上安装。3、因此,如果我们要安装的话,需要准备一块其他硬盘。4、然后将该硬盘安装到电脑里,如果没有额外插槽的话就要换掉之前的硬盘。5、安装完成后,

可以通过以下步骤查询 SQL Server 数据库日志:1. 打开 SQL Server Management Studio,连接到数据库服务器;2. 展开“管理”节点,导航到“SQL Server 日志”;3. 选择要查询的日志文件,右键单击并选择“查看日志文件”;4. 浏览日志记录。其他查询日志方法:使用 Transact-SQL 查询、PowerShell Cmdlet。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
