From 86f75cbd690003c913e25f41429c1212955d1445 Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期四, 04 一月 2024 13:33:20 +0800 Subject: [PATCH] 添加SEM数据入库功能 --- JavaCode/FrmSEM.cs | 83 +++++++ JavaCode/DLL/NPOI.dll | 0 JavaCode/DLL/SQLite.Interop.dll | 0 JavaCode/FrmSEM.Designer.cs | 72 ++++++ JavaCode/cs/SQLiteHelper.cs | 201 ++++++++++++++++++ JavaCode/DLL/System.Data.SQLite.dll | 0 JavaCode/FrmSEM.resx | 120 ++++++++++ JavaCode/FrmMain.Designer.cs | 16 + JavaCode/FrmMain.cs | 6 JavaCode/JavaCode.csproj | 22 ++ JavaCode/cs/ReadExcel.cs | 113 ++++++++++ 11 files changed, 632 insertions(+), 1 deletions(-) diff --git a/JavaCode/DLL/NPOI.dll b/JavaCode/DLL/NPOI.dll new file mode 100644 index 0000000..2415072 --- /dev/null +++ b/JavaCode/DLL/NPOI.dll Binary files differ diff --git a/JavaCode/DLL/SQLite.Interop.dll b/JavaCode/DLL/SQLite.Interop.dll new file mode 100644 index 0000000..0be84a6 --- /dev/null +++ b/JavaCode/DLL/SQLite.Interop.dll Binary files differ diff --git a/JavaCode/DLL/System.Data.SQLite.dll b/JavaCode/DLL/System.Data.SQLite.dll new file mode 100644 index 0000000..a7716d9 --- /dev/null +++ b/JavaCode/DLL/System.Data.SQLite.dll Binary files differ diff --git a/JavaCode/FrmMain.Designer.cs b/JavaCode/FrmMain.Designer.cs index 88acdec..9500c0d 100644 --- a/JavaCode/FrmMain.Designer.cs +++ b/JavaCode/FrmMain.Designer.cs @@ -32,6 +32,7 @@ this.btnMybatis = new System.Windows.Forms.Button(); this.btnQGis = new System.Windows.Forms.Button(); this.btnTerrain = new System.Windows.Forms.Button(); + this.btnSEM = new System.Windows.Forms.Button(); this.SuspendLayout(); // // btnSys @@ -78,11 +79,23 @@ this.btnTerrain.UseVisualStyleBackColor = true; this.btnTerrain.Click += new System.EventHandler(this.btnTerrain_Click); // + // btnSEM + // + this.btnSEM.Font = new System.Drawing.Font("瀹嬩綋", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); + this.btnSEM.Location = new System.Drawing.Point(143, 252); + this.btnSEM.Name = "btnSEM"; + this.btnSEM.Size = new System.Drawing.Size(183, 25); + this.btnSEM.TabIndex = 17; + this.btnSEM.Text = "S E M 鏁� 鎹� 澶� 鐞�"; + this.btnSEM.UseVisualStyleBackColor = true; + this.btnSEM.Click += new System.EventHandler(this.btnSEM_Click); + // // FrmMain // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(484, 261); + this.ClientSize = new System.Drawing.Size(468, 319); + this.Controls.Add(this.btnSEM); this.Controls.Add(this.btnTerrain); this.Controls.Add(this.btnQGis); this.Controls.Add(this.btnMybatis); @@ -100,5 +113,6 @@ private System.Windows.Forms.Button btnMybatis; private System.Windows.Forms.Button btnQGis; private System.Windows.Forms.Button btnTerrain; + private System.Windows.Forms.Button btnSEM; } } \ No newline at end of file diff --git a/JavaCode/FrmMain.cs b/JavaCode/FrmMain.cs index 285593a..b4fbbc3 100644 --- a/JavaCode/FrmMain.cs +++ b/JavaCode/FrmMain.cs @@ -39,5 +39,11 @@ { TerrainHelper.RemoveTerrain(); } + + private void btnSEM_Click(object sender, EventArgs e) + { + FrmSEM sem = new FrmSEM(); + sem.Show(); + } } } diff --git a/JavaCode/FrmSEM.Designer.cs b/JavaCode/FrmSEM.Designer.cs new file mode 100644 index 0000000..a000198 --- /dev/null +++ b/JavaCode/FrmSEM.Designer.cs @@ -0,0 +1,72 @@ +锘縩amespace JavaCode +{ + partial class FrmSEM + { + /// <summary> + /// Required designer variable. + /// </summary> + private System.ComponentModel.IContainer components = null; + + /// <summary> + /// Clean up any resources being used. + /// </summary> + /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// <summary> + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// </summary> + private void InitializeComponent() + { + this.txtExcel = new System.Windows.Forms.TextBox(); + this.btnCalc = new System.Windows.Forms.Button(); + this.SuspendLayout(); + // + // txtExcel + // + this.txtExcel.Location = new System.Drawing.Point(18, 29); + this.txtExcel.Name = "txtExcel"; + this.txtExcel.Size = new System.Drawing.Size(581, 21); + this.txtExcel.TabIndex = 0; + this.txtExcel.Text = "D:\\SEM\\鎾掔偣鍧愭爣.xls"; + // + // btnCalc + // + this.btnCalc.Location = new System.Drawing.Point(618, 27); + this.btnCalc.Name = "btnCalc"; + this.btnCalc.Size = new System.Drawing.Size(75, 23); + this.btnCalc.TabIndex = 1; + this.btnCalc.Text = "璁� 绠�"; + this.btnCalc.UseVisualStyleBackColor = true; + this.btnCalc.Click += new System.EventHandler(this.btnCalc_Click); + // + // FrmSEM + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(712, 75); + this.Controls.Add(this.btnCalc); + this.Controls.Add(this.txtExcel); + this.Name = "FrmSEM"; + this.Text = "FrmSEM"; + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.TextBox txtExcel; + private System.Windows.Forms.Button btnCalc; + } +} \ No newline at end of file diff --git a/JavaCode/FrmSEM.cs b/JavaCode/FrmSEM.cs new file mode 100644 index 0000000..e086b18 --- /dev/null +++ b/JavaCode/FrmSEM.cs @@ -0,0 +1,83 @@ +锘縰sing JavaCode.cs; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace JavaCode +{ + public partial class FrmSEM : Form + { + public FrmSEM() + { + InitializeComponent(); + } + + private void btnCalc_Click(object sender, EventArgs e) + { + try + { + string xls = this.txtExcel.Text.Trim(); + string path = Path.GetDirectoryName(xls); + if (string.IsNullOrWhiteSpace(xls) || !File.Exists(xls)) + { + MessageBox.Show("Excel鏂囦欢璺緞涓虹┖鎴栨枃浠朵笉瀛樺湪锛�"); + return; + } + + DataTable dt = ReadExcel.ReadXls(xls, 0); + if (null == dt || dt.Rows.Count == 0) + { + MessageBox.Show("Excel鍐呭涓虹┖锛�"); + return; + } + + List<Coord> list = ModelHandler.FillModel<Coord>(dt); + if (null == list || list.Count == 0) + { + MessageBox.Show("璇诲彇Excel鍐呭涓虹┖锛�"); + return; + } + + int count = 0; + foreach (Coord c in list) + { + string db = Path.Combine(path, c.Name + ".sem"); + if (!File.Exists(db)) continue; + + SQLiteHelper.ConnectionString = string.Format("Data Source={0};Integrated Security=True;Max Pool Size=64", db); + + string sql = string.Format("insert into POSITION (id, x, y, height) values ((select count(*)+1 from POSITION), {0}, {1}, {2})", c.X, c.Y, c.Z); + int rows = SQLiteHelper.ExecuteNonQuery(sql, null); + if (rows > 0) + { + count++; + } + } + + MessageBox.Show("鍏辨洿鏂颁簡 " + count + " 涓�.sem鏂囦欢锛�"); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace); + } + } + } + + public class Coord + { + public string Name { set; get; } + + public string X { set; get; } + + public string Y { set; get; } + + public string Z { set; get; } + } +} diff --git a/JavaCode/FrmSEM.resx b/JavaCode/FrmSEM.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/JavaCode/FrmSEM.resx @@ -0,0 +1,120 @@ +锘�<?xml version="1.0" encoding="utf-8"?> +<root> + <!-- + Microsoft ResX Schema + + Version 2.0 + + The primary goals of this format is to allow a simple XML format + that is mostly human readable. The generation and parsing of the + various data types are done through the TypeConverter classes + associated with the data types. + + Example: + + ... ado.net/XML headers & schema ... + <resheader name="resmimetype">text/microsoft-resx</resheader> + <resheader name="version">2.0</resheader> + <resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader> + <resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader> + <data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data> + <data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data> + <data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64"> + <value>[base64 mime encoded serialized .NET Framework object]</value> + </data> + <data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64"> + <value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value> + <comment>This is a comment</comment> + </data> + + There are any number of "resheader" rows that contain simple + name/value pairs. + + Each data row contains a name, and value. The row also contains a + type or mimetype. Type corresponds to a .NET class that support + text/value conversion through the TypeConverter architecture. + Classes that don't support this are serialized and stored with the + mimetype set. + + The mimetype is used for serialized objects, and tells the + ResXResourceReader how to depersist the object. This is currently not + extensible. For a given mimetype the value must be set accordingly: + + Note - application/x-microsoft.net.object.binary.base64 is the format + that the ResXResourceWriter will generate, however the reader can + read any of the formats listed below. + + mimetype: application/x-microsoft.net.object.binary.base64 + value : The object must be serialized with + : System.Runtime.Serialization.Formatters.Binary.BinaryFormatter + : and then encoded with base64 encoding. + + mimetype: application/x-microsoft.net.object.soap.base64 + value : The object must be serialized with + : System.Runtime.Serialization.Formatters.Soap.SoapFormatter + : and then encoded with base64 encoding. + + mimetype: application/x-microsoft.net.object.bytearray.base64 + value : The object must be serialized into a byte array + : using a System.ComponentModel.TypeConverter + : and then encoded with base64 encoding. + --> + <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> + <xsd:import namespace="http://www.w3.org/XML/1998/namespace" /> + <xsd:element name="root" msdata:IsDataSet="true"> + <xsd:complexType> + <xsd:choice maxOccurs="unbounded"> + <xsd:element name="metadata"> + <xsd:complexType> + <xsd:sequence> + <xsd:element name="value" type="xsd:string" minOccurs="0" /> + </xsd:sequence> + <xsd:attribute name="name" use="required" type="xsd:string" /> + <xsd:attribute name="type" type="xsd:string" /> + <xsd:attribute name="mimetype" type="xsd:string" /> + <xsd:attribute ref="xml:space" /> + </xsd:complexType> + </xsd:element> + <xsd:element name="assembly"> + <xsd:complexType> + <xsd:attribute name="alias" type="xsd:string" /> + <xsd:attribute name="name" type="xsd:string" /> + </xsd:complexType> + </xsd:element> + <xsd:element name="data"> + <xsd:complexType> + <xsd:sequence> + <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" /> + <xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" /> + </xsd:sequence> + <xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" /> + <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" /> + <xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" /> + <xsd:attribute ref="xml:space" /> + </xsd:complexType> + </xsd:element> + <xsd:element name="resheader"> + <xsd:complexType> + <xsd:sequence> + <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" /> + </xsd:sequence> + <xsd:attribute name="name" type="xsd:string" use="required" /> + </xsd:complexType> + </xsd:element> + </xsd:choice> + </xsd:complexType> + </xsd:element> + </xsd:schema> + <resheader name="resmimetype"> + <value>text/microsoft-resx</value> + </resheader> + <resheader name="version"> + <value>2.0</value> + </resheader> + <resheader name="reader"> + <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> + </resheader> + <resheader name="writer"> + <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> + </resheader> +</root> \ No newline at end of file diff --git a/JavaCode/JavaCode.csproj b/JavaCode/JavaCode.csproj index db95c31..5e8a35b 100644 --- a/JavaCode/JavaCode.csproj +++ b/JavaCode/JavaCode.csproj @@ -61,9 +61,17 @@ <SpecificVersion>False</SpecificVersion> <HintPath>DLL\Npgsql.dll</HintPath> </Reference> + <Reference Include="NPOI, Version=2.4.1.0, Culture=neutral, PublicKeyToken=0df73ec7942b34e1, processorArchitecture=MSIL"> + <SpecificVersion>False</SpecificVersion> + <HintPath>DLL\NPOI.dll</HintPath> + </Reference> <Reference Include="System" /> <Reference Include="System.configuration" /> <Reference Include="System.Core" /> + <Reference Include="System.Data.SQLite, Version=1.0.94.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=MSIL"> + <SpecificVersion>False</SpecificVersion> + <HintPath>DLL\System.Data.SQLite.dll</HintPath> + </Reference> <Reference Include="System.Xml.Linq" /> <Reference Include="System.Data.DataSetExtensions" /> <Reference Include="Microsoft.CSharp" /> @@ -77,6 +85,8 @@ <Compile Include="cs\ModelHandler.cs" /> <Compile Include="cs\PostgreHelper.cs" /> <Compile Include="cs\QGisHelper.cs" /> + <Compile Include="cs\ReadExcel.cs" /> + <Compile Include="cs\SQLiteHelper.cs" /> <Compile Include="cs\TabInfo.cs" /> <Compile Include="cs\TerrainHelper.cs" /> <Compile Include="FrmMain.cs"> @@ -84,6 +94,12 @@ </Compile> <Compile Include="FrmMain.Designer.cs"> <DependentUpon>FrmMain.cs</DependentUpon> + </Compile> + <Compile Include="FrmSEM.cs"> + <SubType>Form</SubType> + </Compile> + <Compile Include="FrmSEM.Designer.cs"> + <DependentUpon>FrmSEM.cs</DependentUpon> </Compile> <Compile Include="FrmSys.cs"> <SubType>Form</SubType> @@ -102,6 +118,9 @@ <EmbeddedResource Include="FrmMain.resx"> <DependentUpon>FrmMain.cs</DependentUpon> <SubType>Designer</SubType> + </EmbeddedResource> + <EmbeddedResource Include="FrmSEM.resx"> + <DependentUpon>FrmSEM.cs</DependentUpon> </EmbeddedResource> <EmbeddedResource Include="FrmSys.resx"> <DependentUpon>FrmSys.cs</DependentUpon> @@ -150,6 +169,9 @@ </EmbeddedResource> <Content Include="DLL\Mono.Security.dll" /> <Content Include="DLL\Npgsql.dll" /> + <Content Include="DLL\NPOI.dll" /> + <Content Include="DLL\SQLite.Interop.dll" /> + <Content Include="DLL\System.Data.SQLite.dll" /> <Content Include="SysTemplate\pg.sql" /> <EmbeddedResource Include="SysTemplate\Controller.java"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> diff --git a/JavaCode/cs/ReadExcel.cs b/JavaCode/cs/ReadExcel.cs new file mode 100644 index 0000000..c514c45 --- /dev/null +++ b/JavaCode/cs/ReadExcel.cs @@ -0,0 +1,113 @@ +锘縰sing NPOI.HSSF.UserModel; +using NPOI.SS.UserModel; +using System; +using System.Collections.Generic; +using System.Data; +using System.IO; +using System.Linq; + +namespace JavaCode.cs +{ + /// <summary> + /// 璇诲彇Excel绫� + /// </summary> + public class ReadExcel + { + private string intName = typeof(int).Name; + + private string doubleName = typeof(double).Name; + + private string stringName = typeof(string).Name; + + /// <summary> + /// 璇诲彇Excel + /// </summary> + public static DataTable ReadXls(string file, int index = 0) + { + if (!File.Exists(file)) return null; + + string ext = Path.GetExtension(file).ToLower(); + using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read)) + { + //IWorkbook wk = ext == ".xls" ? new HSSFWorkbook(fs) : new XSSFWorkbook(); + IWorkbook wk = new HSSFWorkbook(fs); + ISheet sheet = wk.GetSheetAt(index); + if (sheet == null) + { + return null; + } + + return SheetToDataTable(sheet); + } + } + + /// <summary> + /// Sheet杞崲涓篋ataTable + /// </summary> + public static DataTable SheetToDataTable(ISheet sheet) + { + IRow firstRow = sheet.GetRow(0); + int cellCount = firstRow.LastCellNum; + int rowCount = sheet.LastRowNum; + + DataTable dt = CreateDataTable(cellCount, sheet.GetRow(sheet.FirstRowNum)); + //for (int i = sheet.FirstRowNum; i <= rowCount; i++) + for (int i = sheet.FirstRowNum + 1; i <= rowCount; i++) + { + IRow row = sheet.GetRow(i); + if (row == null) + { + continue; + } + + DataRow dtRow = dt.NewRow(); + for (int j = 0; j < cellCount; j++) + { + ICell cell = row.GetCell(j); + dtRow[j] = cell == null ? string.Empty : cell.ToString(); + } + + dt.Rows.Add(dtRow); + } + + return dt; + } + + /// <summary> + /// 鍒涘缓DataTable + /// </summary> + public static DataTable CreateDataTable(int columns, IRow row) + { + DataTable dt = new DataTable(); + for (int i = 0; i < columns; i++) + { + //DataColumn dc = new DataColumn("Col_" + i, typeof(string)); + string colName = null == row || null == row.GetCell(i) ? "Col_" + i : row.GetCell(i).ToString(); + DataColumn dc = new DataColumn(colName, typeof(string)); + dt.Columns.Add(dc); + } + + return dt; + } + + // 鏌ユ壘鍒椾綅缃� + private int FindColumn(DataRow firstRow, int columns, string name) + { + for (int i = 0, c = columns; i < c; i++) + { + string colName = firstRow[i] == null ? null : firstRow[i].ToString().Trim(); + if (string.IsNullOrEmpty(colName)) + { + break; + } + + if (name == colName) + { + return i; + } + } + + return -1; + } + } +} diff --git a/JavaCode/cs/SQLiteHelper.cs b/JavaCode/cs/SQLiteHelper.cs new file mode 100644 index 0000000..40a4ef0 --- /dev/null +++ b/JavaCode/cs/SQLiteHelper.cs @@ -0,0 +1,201 @@ +锘縰sing System; +using System.Collections.Generic; +using System.Data; +using System.Data.SQLite; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Web; + +namespace JavaCode.cs +{ + public class SQLiteHelper + { + private static string _connStr = null; + + private static bool _isLoadDriver = false; + + /// <summary> + /// ConnectionString鏍蜂緥锛欴atasource=Test.db3;Pooling=true;FailIfMissing=false + /// </summary> + public static string ConnectionString + { + get + { + if (string.IsNullOrEmpty(_connStr)) + { + //_connStr = string.Format("Data Source={0};Version={1};", Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources\\XiAn.sqlite"), 3); + _connStr = string.Format("Data Source={0};Integrated Security=True;Max Pool Size=64", Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App_Data\\js.db")); + } + + return _connStr; + } + set + { + _connStr = value; + } + } + + private static void LoadDrivers(SQLiteConnection conn) + { + if (!_isLoadDriver) + { + string spatialitePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources\\sqlite"); + Environment.SetEnvironmentVariable("PATH", spatialitePath + ";" + Environment.GetEnvironmentVariable("PATH")); + + _isLoadDriver = true; + } + + if (conn.State != ConnectionState.Open) + { + conn.Open(); + } + + conn.LoadExtension("mod_spatialite.dll"); + } + + /// <summary> + /// 鏌ヨ鏁版嵁搴撲腑鐨勬墍鏈夋暟鎹被鍨嬩俊鎭�� + /// </summary> + public DataTable GetSchema() + { + using (SQLiteConnection conn = new SQLiteConnection(ConnectionString)) + { + if (conn.State != ConnectionState.Open) + { + conn.Open(); + } + + return conn.GetSchema("TABLES"); + } + } + + private static void PrepareCommand(SQLiteCommand cmd, SQLiteConnection conn, params SQLiteParameter[] parameters) + { + if (conn.State != ConnectionState.Open) + { + conn.Open(); + } + if (parameters != null && parameters.Length > 0) + { + cmd.Parameters.AddRange(parameters); + } + } + + public static DataSet ExecuteQuery(string cmdText, params SQLiteParameter[] parameters) + { + using (SQLiteConnection conn = new SQLiteConnection(ConnectionString)) + { + using (SQLiteCommand cmd = new SQLiteCommand(cmdText, conn)) + { + PrepareCommand(cmd, conn, parameters); + + DataSet ds = new DataSet(); + using (SQLiteDataAdapter da = new SQLiteDataAdapter(cmd)) + { + da.Fill(ds); + } + + return ds; + } + } + } + + public static DataTable GetDataTable(string cmdText, params SQLiteParameter[] parameters) + { + using (SQLiteConnection conn = new SQLiteConnection(ConnectionString)) + { + LoadDrivers(conn); + + using (SQLiteCommand cmd = new SQLiteCommand(cmdText, conn)) + { + PrepareCommand(cmd, conn, parameters); + + DataTable dt = new DataTable(); + using (SQLiteDataAdapter da = new SQLiteDataAdapter(cmd)) + { + da.Fill(dt); + } + + return dt; + } + } + } + + public static int ExecuteNonQuery(string cmdText, params SQLiteParameter[] parameters) + { + using (SQLiteConnection conn = new SQLiteConnection(ConnectionString)) + { + using (SQLiteCommand cmd = new SQLiteCommand(cmdText, conn)) + { + PrepareCommand(cmd, conn, parameters); + + return cmd.ExecuteNonQuery(); + } + } + } + + public static SQLiteDataReader ExecuteReader(string cmdText, params SQLiteParameter[] parameters) + { + SQLiteConnection conn = new SQLiteConnection(ConnectionString); + using (SQLiteCommand cmd = new SQLiteCommand(cmdText, conn)) + { + PrepareCommand(cmd, conn, parameters); + + return cmd.ExecuteReader(CommandBehavior.CloseConnection); + } + } + + public static object ExecuteScalar(string cmdText, params SQLiteParameter[] parameters) + { + using (SQLiteConnection conn = new SQLiteConnection(ConnectionString)) + { + using (SQLiteCommand cmd = new SQLiteCommand(cmdText, conn)) + { + PrepareCommand(cmd, conn, parameters); + + return cmd.ExecuteScalar(); + } + } + } + + /// <summary> + /// 鎵归噺澶勭悊鏁版嵁鎿嶄綔璇彞銆� + /// </summary> + public void ExecuteNonQueryBatch(List<KeyValuePair<string, SQLiteParameter[]>> list) + { + using (SQLiteConnection conn = new SQLiteConnection(ConnectionString)) + { + if (conn.State != ConnectionState.Open) + { + conn.Open(); + } + + using (SQLiteTransaction tran = conn.BeginTransaction()) + { + using (SQLiteCommand cmd = new SQLiteCommand(conn)) + { + try + { + foreach (var item in list) + { + cmd.CommandText = item.Key; + if (item.Value != null) + { + cmd.Parameters.AddRange(item.Value); + } + cmd.ExecuteNonQuery(); + } + tran.Commit(); + } + catch (Exception ex) + { + tran.Rollback(); + throw ex; + } + } + } + } + } + } +} -- Gitblit v1.9.3