[英]What to put in the “Commit” button to insert data to the database? I am using stored procedure. C#
[英]What am i doing wrong in this Insert Stored Procedure in c# Console app
我在 c# 控制台中制作了一个应用程序,它读取 xml 文件并插入一些需要存储的关键属性。 我正在尝试使用进入我的 Program.cs 中调用的 DataAccess class 的存储过程来做到这一点。 我将首先展示我的 DataAccess class 以及我如何制作我的 SP_Insert
class DataAccess
{
SqlConnection connection = null;
public DataAccess()
{
connection = new SqlConnection(@"Persist Security Info=False;Integrated Security=true; Initial Catalog=Teste;Data Source=Teste;Server=localhost\sqlexpress");
}
private void ManageConnectionState()
{
if (connection == null || connection.ConnectionString.Equals(""))
{
connection = new SqlConnection(@"Persist Security Info = False; Integrated Security = true; Initial Catalog = Teste; Data Source = Teste; Server = localhost\sqlexpress");
connection.Open();
}
else
if (connection.State.Equals(ConnectionState.Closed))
{
connection.Open();
}
else
{
connection.Dispose();
connection.Close();
}
}
#region Insert
public void SP_Insert(string XMLPath, string XMLName, string BatchClassName, string BatchName, string BatchDescription, string Error, string ErrorImagePath, string Done)
{
try
{
ManageConnectionState();
SqlCommand command = new SqlCommand("Sp_Insert", connection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("@XMLPath", XMLPath);
command.Parameters.AddWithValue("@XMLName", XMLName);
command.Parameters.AddWithValue("@BatchClassName", BatchClassName);
command.Parameters.AddWithValue("@BatchName", BatchName);
command.Parameters.AddWithValue("@BatchDescription", BatchDescription);
command.Parameters.AddWithValue("@Error", Error);
command.Parameters.AddWithValue("@ErrorImagePath", ErrorImagePath);
command.Parameters.AddWithValue("@Done", Done);
command.ExecuteScalar();
}
catch (Exception ex)
{
Console.WriteLine("Erro: " + ex.Message);
}
finally
{
ManageConnectionState();
connection.Close();
}
}
#endregion
}
}
我认为这部分都是正确的,接下来的两个是我不知道的现在这是我的 Progam.cs 文件
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Data;
namespace XMLReaderConsoleAPP1
{
class Program
{
static void Main()
{
DataAccess da = new DataAccess();
try
{
var path = @"C:\XMLFiles";
DirectoryInfo di = new DirectoryInfo(path);
foreach (var file in Directory.GetFiles(path, "*.xml"))
{
ProcessFile(file);
}
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine("Erro: {0}", ex.Message);
return;
}
void ProcessFile(string Filename)
{
bool temErro = false;
Console.WriteLine("A processar xml: " + Filename);
XmlDocument xml = new XmlDocument();
xml.Load(Filename);
//XmlNodeList xnLista = xml.SelectNodes(@"//Pages/Page");
//Console.WriteLine($"Selected {xnLista.Count} nodes");
XmlNodeList xnLista3 = xml.SelectNodes(@"//ImportSession/Batches");
XmlNodeList xnLista2 =
xml.SelectNodes(@"//ImportSession/Batches/Batch/Documents/Document/Pages/Page");
//Console.WriteLine($"Selected {xnLista3.Count} nodes");
//Console.WriteLine($"Selected {xnLista2.Count} nodes");
//int j = 0;
foreach (XmlNode xn in xnLista2)
{
//Console.WriteLine($"{++i} {xn.Name}: {xn.Attributes["ImportFileName"].Value}");
string processed = "@//Batch[Processed]";
//Console.WriteLine($"{++j} {xn.Name}: {xn.Attributes["Value"].Value}");
string BatchClassName = xn.Attributes["BatchClassName"]?.Value;
string BatchName = xn.Attributes["Name"]?.Value;
string BatchDescription = xn.Attributes["Description"]?.Value;
//string process = xn.Attributes["Processed"]?.Value;
string error = xn.Attributes["ErrorMessage"]?.Value;
string errorImagePath = xn.Attributes["ErrorCode"]?.Value;
if (!string.IsNullOrEmpty(processed)) //xml já foi processado
{
if (!string.IsNullOrEmpty(error))
{
string page = xn.Attributes["ImportFileName"].Value;
Console.WriteLine("Página com erro: " + page);
temErro = true;
da.SP_Insert(page, Filename, BatchClassName, BatchName, BatchDescription,
error, errorImagePath, processed);
}
}
}
if (temErro)
Console.WriteLine("Ficheiro com erro: " + Filename);
else
Console.WriteLine("Ficheiro processado: " + Filename);
//Console.WriteLine(Filename);
}
}
}
}
我一直在使用 xn.Attributes 来获取我需要的信息,但由于某种原因,某些字符串没有返回值(从 BatchClassName 到结尾)
我觉得很奇怪,因为前两个确实显示了信息。
现在这是我的存储过程
USE [Teste]
GO
/****** Object: StoredProcedure [dbo].[SP_Insert] Script Date: 22/01/2021 09:32:14 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[SP_Insert]
@XMLPath nvarchar(250),
@XMLName nvarchar(250),
@BatchClassName nvarchar(250),
@BatchName nvarchar(250),
@BatchDescription nvarchar(250),
@Error bit,
@ErrorImagePath nvarchar(250),
@Done bit
AS
BEGIN
Insert into T_XMLFiles (XMLPath, XMLName, BatchClassName, BatchName, BatchDescription, Error, ErrorImagePath, Done)
Values(@XMLPath, @XMLName, @BatchClassName, @BatchName, @BatchDescription, @Error, @ErrorImagePath, @Done)
END
我还将留下一个 xml 文件的示例,因为它们都遵循相同的结构
<ImportSession>
<Batches>
<Batch Name="MSG_SaintGobainPam_20210118" Description="SaintGobain_20210118" BatchClassName="SAINTGOBAIN - Faturas Notas" Processed="1">
<BatchFields>
<BatchField Name="CAPALOTE" Value="0" />
<BatchField Name="NCONTENTOR" Value="0" />
</BatchFields>
<Documents>
<Document FormTypeName="DOC_SaintGobain_Faturas_Notas">
<IndexFields>
<IndexField Name="ETIQUETA" Value="ZE80047804" />
</IndexFields>
<Pages>
<Page ImportFileName="\\umm\c$\acxmlaid\SaintGobain\20210118\ZE80047804.pdf" />
</Pages>
</Document>
<Document FormTypeName="DOC_SaintGobain_Faturas_Notas">
<IndexFields>
<IndexField Name="ETIQUETA" Value="ZE80047842" />
</IndexFields>
<Pages>
<Page ImportFileName="\\umm\c$\acxmlaid\SaintGobain\20210118\ZE80047842.pdf" />
</Pages>
</Document>
<Document FormTypeName="DOC_SaintGobain_Faturas_Notas">
<IndexFields>
<IndexField Name="ETIQUETA" Value="ZE80047843" />
</IndexFields>
<Pages>
<Page ImportFileName="\\umm\c$\acxmlaid\SaintGobain\20210118\ZE80047843.pdf" />
</Pages>
</Document>
<Document FormTypeName="DOC_SaintGobain_Faturas_Notas">
<IndexFields>
<IndexField Name="ETIQUETA" Value="ZE80047849" />
</IndexFields>
<Pages>
<Page ImportFileName="\\umm\c$\acxmlaid\SaintGobain\20210118\ZE80047849.pdf" />
</Pages>
</Document>
<Document FormTypeName="DOC_SaintGobain_Faturas_Notas">
<IndexFields>
<IndexField Name="ETIQUETA" Value="ZE80047853" />
</IndexFields>
<Pages>
<Page ImportFileName="\\umm\c$\acxmlaid\SaintGobain\20210118\ZE80047853.pdf" />
</Pages>
</Document>
<Document FormTypeName="DOC_SaintGobain_Faturas_Notas">
<IndexFields>
<IndexField Name="ETIQUETA" Value="ZE80047854" />
</IndexFields>
<Pages>
<Page ImportFileName="\\umm\c$\acxmlaid\SaintGobain\20210118\ZE80047854.pdf" />
</Pages>
</Document>
<Document FormTypeName="DOC_SaintGobain_Faturas_Notas">
<IndexFields>
<IndexField Name="ETIQUETA" Value="ZE80047855" />
</IndexFields>
<Pages>
<Page ImportFileName="\\umm\c$\acxmlaid\SaintGobain\20210118\ZE80047855.pdf" />
</Pages>
</Document>
<Document FormTypeName="DOC_SaintGobain_Faturas_Notas">
<IndexFields>
<IndexField Name="ETIQUETA" Value="ZE80047860" />
</IndexFields>
<Pages>
<Page ImportFileName="\\umm\c$\acxmlaid\SaintGobain\20210118\ZE80047860.pdf" ErrorCode="20" ErrorMessage="Illegal file format (\\umm\c$\acxmlaid\SaintGobain\20210118\ZE80047860.pdf)" />
</Pages>
</Document>
<Document FormTypeName="DOC_SaintGobain_Faturas_Notas">
<IndexFields>
<IndexField Name="ETIQUETA" Value="ZE80047861" />
</IndexFields>
<Pages>
<Page ImportFileName="\\umm\c$\acxmlaid\SaintGobain\20210118\ZE80047861.pdf" />
</Pages>
</Document>
</Documents>
</Batch>
</Batches>
</ImportSession>
就像我在开始时所说的那样,我试图做到这一点,就好像我在 windows forms 中制作它,但没有按钮可以单击并插入信息,如果你们中的任何人可以帮助我,我将不胜感激。 谢谢大家,如果您需要更多信息来帮助我,请随时询问。
我在 c# 控制台中制作了一个应用程序,它读取 xml 文件并插入一些需要存储的关键属性。 我正在尝试使用进入我的 Program.cs 中调用的 DataAccess class 的存储过程来做到这一点。 我将首先展示我的 DataAccess class 以及我如何制作我的 SP_Insert
class DataAccess
{
SqlConnection connection = null;
public DataAccess()
{
connection = new SqlConnection(@"Persist Security Info=False;Integrated Security=true; Initial Catalog=Teste;Data Source=Teste;Server=localhost\sqlexpress");
}
private void ManageConnectionState()
{
if (connection == null || connection.ConnectionString.Equals(""))
{
connection = new SqlConnection(@"Persist Security Info = False; Integrated Security = true; Initial Catalog = Teste; Data Source = Teste; Server = localhost\sqlexpress");
connection.Open();
}
else
if (connection.State.Equals(ConnectionState.Closed))
{
connection.Open();
}
else
{
connection.Dispose();
connection.Close();
}
}
#region Insert
public void SP_Insert(string XMLPath, string XMLName, string BatchClassName, string BatchName, string BatchDescription, string Error, string ErrorImagePath, string Done)
{
try
{
ManageConnectionState();
SqlCommand command = new SqlCommand("Sp_Insert", connection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("@XMLPath", XMLPath);
command.Parameters.AddWithValue("@XMLName", XMLName);
command.Parameters.AddWithValue("@BatchClassName", BatchClassName);
command.Parameters.AddWithValue("@BatchName", BatchName);
command.Parameters.AddWithValue("@BatchDescription", BatchDescription);
command.Parameters.AddWithValue("@Error", Error);
command.Parameters.AddWithValue("@ErrorImagePath", ErrorImagePath);
command.Parameters.AddWithValue("@Done", Done);
command.ExecuteScalar();
}
catch (Exception ex)
{
Console.WriteLine("Erro: " + ex.Message);
}
finally
{
ManageConnectionState();
connection.Close();
}
}
#endregion
}
}
我认为这部分都是正确的,接下来的两个是我不知道的现在这是我的 Progam.cs 文件
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Data;
namespace XMLReaderConsoleAPP1
{
class Program
{
static void Main()
{
DataAccess da = new DataAccess();
try
{
var path = @"C:\XMLFiles";
DirectoryInfo di = new DirectoryInfo(path);
foreach (var file in Directory.GetFiles(path, "*.xml"))
{
ProcessFile(file);
}
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine("Erro: {0}", ex.Message);
return;
}
void ProcessFile(string Filename)
{
bool temErro = false;
Console.WriteLine("A processar xml: " + Filename);
XmlDocument xml = new XmlDocument();
xml.Load(Filename);
//XmlNodeList xnLista = xml.SelectNodes(@"//Pages/Page");
//Console.WriteLine($"Selected {xnLista.Count} nodes");
XmlNodeList xnLista3 = xml.SelectNodes(@"//ImportSession/Batches");
XmlNodeList xnLista2 =
xml.SelectNodes(@"//ImportSession/Batches/Batch/Documents/Document/Pages/Page");
//Console.WriteLine($"Selected {xnLista3.Count} nodes");
//Console.WriteLine($"Selected {xnLista2.Count} nodes");
//int j = 0;
foreach (XmlNode xn in xnLista2)
{
//Console.WriteLine($"{++i} {xn.Name}: {xn.Attributes["ImportFileName"].Value}");
string processed = "@//Batch[Processed]";
//Console.WriteLine($"{++j} {xn.Name}: {xn.Attributes["Value"].Value}");
string BatchClassName = xn.Attributes["BatchClassName"]?.Value;
string BatchName = xn.Attributes["Name"]?.Value;
string BatchDescription = xn.Attributes["Description"]?.Value;
//string process = xn.Attributes["Processed"]?.Value;
string error = xn.Attributes["ErrorMessage"]?.Value;
string errorImagePath = xn.Attributes["ErrorCode"]?.Value;
if (!string.IsNullOrEmpty(processed)) //xml já foi processado
{
if (!string.IsNullOrEmpty(error))
{
string page = xn.Attributes["ImportFileName"].Value;
Console.WriteLine("Página com erro: " + page);
temErro = true;
da.SP_Insert(page, Filename, BatchClassName, BatchName, BatchDescription,
error, errorImagePath, processed);
}
}
}
if (temErro)
Console.WriteLine("Ficheiro com erro: " + Filename);
else
Console.WriteLine("Ficheiro processado: " + Filename);
//Console.WriteLine(Filename);
}
}
}
}
我一直在使用 xn.Attributes 来获取我需要的信息,但由于某种原因,某些字符串没有返回值(从 BatchClassName 到结尾)
我觉得很奇怪,因为前两个确实显示了信息。
现在这是我的存储过程
USE [Teste]
GO
/****** Object: StoredProcedure [dbo].[SP_Insert] Script Date: 22/01/2021 09:32:14 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[SP_Insert]
@XMLPath nvarchar(250),
@XMLName nvarchar(250),
@BatchClassName nvarchar(250),
@BatchName nvarchar(250),
@BatchDescription nvarchar(250),
@Error bit,
@ErrorImagePath nvarchar(250),
@Done bit
AS
BEGIN
Insert into T_XMLFiles (XMLPath, XMLName, BatchClassName, BatchName, BatchDescription, Error, ErrorImagePath, Done)
Values(@XMLPath, @XMLName, @BatchClassName, @BatchName, @BatchDescription, @Error, @ErrorImagePath, @Done)
END
我还将留下一个 xml 文件的示例,因为它们都遵循相同的结构
<ImportSession>
<Batches>
<Batch Name="MSG_SaintGobainPam_20210118" Description="SaintGobain_20210118" BatchClassName="SAINTGOBAIN - Faturas Notas" Processed="1">
<BatchFields>
<BatchField Name="CAPALOTE" Value="0" />
<BatchField Name="NCONTENTOR" Value="0" />
</BatchFields>
<Documents>
<Document FormTypeName="DOC_SaintGobain_Faturas_Notas">
<IndexFields>
<IndexField Name="ETIQUETA" Value="ZE80047804" />
</IndexFields>
<Pages>
<Page ImportFileName="\\umm\c$\acxmlaid\SaintGobain\20210118\ZE80047804.pdf" />
</Pages>
</Document>
<Document FormTypeName="DOC_SaintGobain_Faturas_Notas">
<IndexFields>
<IndexField Name="ETIQUETA" Value="ZE80047842" />
</IndexFields>
<Pages>
<Page ImportFileName="\\umm\c$\acxmlaid\SaintGobain\20210118\ZE80047842.pdf" />
</Pages>
</Document>
<Document FormTypeName="DOC_SaintGobain_Faturas_Notas">
<IndexFields>
<IndexField Name="ETIQUETA" Value="ZE80047843" />
</IndexFields>
<Pages>
<Page ImportFileName="\\umm\c$\acxmlaid\SaintGobain\20210118\ZE80047843.pdf" />
</Pages>
</Document>
<Document FormTypeName="DOC_SaintGobain_Faturas_Notas">
<IndexFields>
<IndexField Name="ETIQUETA" Value="ZE80047849" />
</IndexFields>
<Pages>
<Page ImportFileName="\\umm\c$\acxmlaid\SaintGobain\20210118\ZE80047849.pdf" />
</Pages>
</Document>
<Document FormTypeName="DOC_SaintGobain_Faturas_Notas">
<IndexFields>
<IndexField Name="ETIQUETA" Value="ZE80047853" />
</IndexFields>
<Pages>
<Page ImportFileName="\\umm\c$\acxmlaid\SaintGobain\20210118\ZE80047853.pdf" />
</Pages>
</Document>
<Document FormTypeName="DOC_SaintGobain_Faturas_Notas">
<IndexFields>
<IndexField Name="ETIQUETA" Value="ZE80047854" />
</IndexFields>
<Pages>
<Page ImportFileName="\\umm\c$\acxmlaid\SaintGobain\20210118\ZE80047854.pdf" />
</Pages>
</Document>
<Document FormTypeName="DOC_SaintGobain_Faturas_Notas">
<IndexFields>
<IndexField Name="ETIQUETA" Value="ZE80047855" />
</IndexFields>
<Pages>
<Page ImportFileName="\\umm\c$\acxmlaid\SaintGobain\20210118\ZE80047855.pdf" />
</Pages>
</Document>
<Document FormTypeName="DOC_SaintGobain_Faturas_Notas">
<IndexFields>
<IndexField Name="ETIQUETA" Value="ZE80047860" />
</IndexFields>
<Pages>
<Page ImportFileName="\\umm\c$\acxmlaid\SaintGobain\20210118\ZE80047860.pdf" ErrorCode="20" ErrorMessage="Illegal file format (\\umm\c$\acxmlaid\SaintGobain\20210118\ZE80047860.pdf)" />
</Pages>
</Document>
<Document FormTypeName="DOC_SaintGobain_Faturas_Notas">
<IndexFields>
<IndexField Name="ETIQUETA" Value="ZE80047861" />
</IndexFields>
<Pages>
<Page ImportFileName="\\umm\c$\acxmlaid\SaintGobain\20210118\ZE80047861.pdf" />
</Pages>
</Document>
</Documents>
</Batch>
</Batches>
</ImportSession>
就像我在开始时所说的那样,我试图做到这一点,就好像我在 windows forms 中制作它,但没有按钮可以单击并插入信息,如果你们中的任何人可以帮助我,我将不胜感激。 谢谢大家,如果您需要更多信息来帮助我,请随时询问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.