将XML数据发送到SQL server时替换/避免XML C#中的unicode字符
下面是创建XML数据的代码,这样我就可以将XML数据作为字符串发送到Sql server进行批量插入将XML数据发送到SQL server时替换/避免XML C#中的unicode字符,c#,sql-server,xml,winforms,C#,Sql Server,Xml,Winforms,下面是创建XML数据的代码,这样我就可以将XML数据作为字符串发送到Sql server进行批量插入 因此,除了编码/解码的概念外,请提供关于转义这些字符或任何其他插入到数据库中的方法的建议 谢谢。找到了解决方案。当我使用XML数据类型而不是varchar(max)时,它保存了详细信息。因此,问题在于参数的数据类型。我已改为nvarchar(max),它工作正常 nvarchar(马克斯)做到了这一点 PrathapHm。由于.NET和SQL Server都应该完全启用unicode,我猜错误
因此,除了编码/解码的概念外,请提供关于转义这些字符或任何其他插入到数据库中的方法的建议
谢谢。找到了解决方案。当我使用XML数据类型而不是varchar(max)时,它保存了详细信息。因此,问题在于参数的数据类型。我已改为nvarchar(max),它工作正常 nvarchar(马克斯)做到了这一点
PrathapHm。由于.NET和SQL Server都应该完全启用unicode,我猜错误在代码中的某个地方。也许您没有在XML头中传递正确的编码?或者在将字符串传递到SQL server之前将其转换为ASCII或ANSI?在数据层中,我收到此错误..因此StoredProcess中一定存在一些问题。我没有在XML标头中使用任何编码。如何在上述代码中添加此编码..请让我知道
private string SavePurchaseInvoiceProducts()
{
string xmldata = string.Empty;
List<ProductDetail> ProductsList = new List<ProductDetail>();
try
{
foreach (DataGridViewRow row in gvPurchaseInvoiceItems.Rows)
{
if (dtBatchDetails.AsEnumerable().Count(i => i.Field<int>("ItemID").ToString().Equals(row.Cells["ItemID"].EditedFormattedValue.ToString())) > 0)
{
ProductsList.Add(new ProductDetail
{
ItemID = Convert.ToInt32(row.Cells["ItemID"].EditedFormattedValue.ToString()),
StockInHand = Convert.ToDecimal(row.Cells["StockinHand"].EditedFormattedValue),
ReceivedQTY = Convert.ToDecimal(row.Cells["Qty"].EditedFormattedValue),
NetPrice = Convert.ToDecimal(row.Cells["NetPrice"].EditedFormattedValue),
SellPrice1 = Convert.ToDecimal(row.Cells["StockWas"].EditedFormattedValue),
Tax_ID = Convert.ToInt32(row.Cells["Tax_ID"].EditedFormattedValue.ToString()),
ItemDescription = row.Cells["Description"].EditedFormattedValue.ToString()
});
row.Cells["StockWas"].Value = Convert.ToDecimal(row.Cells["StockinHand"].EditedFormattedValue);
}
else
{
MessageBox.Show("Please complete Batch qty entry for the product " + row.Cells["Description"].EditedFormattedValue.ToString(), "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return "Error";
}
}
var xmlPBDetails = new XElement("Root",
from pbdetail in ProductsList
select new XElement("InvoiceItemDetails",
new XElement("ItemID", pbdetail.ItemID),
new XElement("ItemDesc", pbdetail.ItemDescription),
new XElement("StockinHand", pbdetail.StockInHand),
new XElement("NetPrice", pbdetail.NetPrice),
new XElement("Stock_RecievedQty", pbdetail.ReceivedQTY),
new XElement("StockInHand_Was", pbdetail.SellPrice1),
new XElement("Tax_ID", pbdetail.Tax_ID),
new XElement("StockUpdateReason", "PurchaseInvoice"),
new XElement("EmpID", GlobalData.EmpID),
new XElement("Company_ID", GlobalData.CompanyID),
new XElement("Lastupdated", String.Format("{0:MM/dd/yyyy HH:mm:ss}", DTPInvoiceDate.Value)),
new XElement("IsDeleted", 0)
));
xmldata = xmlPBDetails.ToString();
}
catch (Exception ex)
{
}
return xmldata;
}
Create PROCEDURE [dbo].[inv_Save_InvoiceProductDetails]
@InvoiceProductDetails as varchar(Max)
as
BEGIN
declare @i int
exec sp_xml_preparedocument @i output,@InvoiceProductDetails
INSERT INTO INV_ProductInvoiceDetails(ProductInvoiceID,ItemID,NetPrice,StockinHand,
Company_ID,Lastupdated,EmpID,IsDeleted,Stock_RecievedQty,Tax_ID,ItemDesc)
SELECT ProductInvoiceID,ItemID,NetPrice,StockinHand,Company_ID,getdate(),EmpID,IsDeleted,
Stock_RecievedQty,Tax_ID,ItemDesc FROM OPENXML(@i,'Root/InvoiceItemDetails',2)
WITH (
ProductInvoiceID int,ItemID int,NetPrice numeric(18,2),StockinHand numeric(18,3),Company_ID int,
Lastupdated datetime,EmpID int,IsDeleted bit,Stock_RecievedQty numeric(18,2),Tax_ID int,ItemDesc varchar(100))
exec sp_xml_removedocument @i
END