c#读取xml 中的数据库连接字符串

c#读取xml 中的数据库连接字符串,第1张

你可以用系统的config 的文件 在里面配置

加入你的文件的路径,然后用

string myXmlPath= ConfigurationManagerConnectionStrings["path"]ConnectionString ;

读出来路径,然后装载, *** 作XML

下面用读取 connectionStrings下的

SqlString为例子

var xml = XElementLoad(@"myXmlPath");

var xmlSqlNode=xmlElements()

Elements() //实现xml 的IEnumerable<T>的接口,用来支持linq

AsParallel() //启用多核处理器并发查找节点

Where(n=>nAttribute("name")ValueEquals("SqlString")) //找sql的节点

Select(n=>nAttribute("connectionString")Value) //取出sql的数据连接

First();

这个xmlSqlNode就是 sql的数据库连接地址了

-- 数据库用户名 UAGReport  表名student 已创建

use UAGReport

go  

DECLARE @doc varchar(1000)  

DECLARE @idoc int 

SET @doc='<ROOT>  

         <student>   

          <id>6</id>  

          <name>sun</name>  

          <sex>male</sex>  

           <age>24</age>  

           </student>  

          </ROOT>'  

  exec sp_xml_preparedocument @idoc output,@doc  

  select  from openxml(@idoc,'/ROOT/student',2)  

  with(id int,name varchar(40),sex varchar(20), age int)  

  insert student  

  select  from openxml(@idoc,'/ROOT/student',2)  

  with student  

  exec sp_xml_removedocument @idoc

实现思路:可以通过w3c的dom解析器进行 *** 作,之后通过getName获取到xmltpye中的属性值。

举例:

import javaioFile;

import javaxxmlparsersDocumentBuilder;

import javaxxmlparsersDocumentBuilderFactory;

import orgw3cdomDocument;

import orgw3cdomElement;

import orgw3cdomNodeList;

public class DomTest1

{

public static void main(String[] args) throws Exception

{

// step 1: 获得dom解析器工厂(工作的作用是用于创建具体的解析器)

DocumentBuilderFactory dbf = DocumentBuilderFactorynewInstance();

// Systemoutprintln("class name: " + dbfgetClass()getName());

// step 2:获得具体的dom解析器

DocumentBuilder db = dbfnewDocumentBuilder();

// Systemoutprintln("class name: " + dbgetClass()getName());

// step3: 解析一个xml文档,获得Document对象(根结点)

Document document = dbparse(new File("candidatexml"));

NodeList list = documentgetElementsByTagName("PERSON");

for(int i = 0; i < listgetLength(); i++)

{

Element element = (Element)listitem(i);

String content = elementgetElementsByTagName("NAME")item(0)getFirstChild()getNodeValue();

Systemoutprintln("name:" + content);

Systemoutprintln("--------------------------------------");

}

}

}

var tmpquery2:tadoquery;

tmpfile:string;

tmptable:tadotable;

begin

if applicationMessageBox( '导入答案将会清空以前的考生答案,确定是否继续? ', '提醒 ',MB_OKCANCEL or MB_ICONWARNING)=mrok then

begin

IF BSopenExecute then

try

try

begin

tmpfile:=bsopenFileName;

tmptable:=tadotableCreate(self);

tmpquery2:=tadoqueryCreate (self);

tmpquery2Connection :=dmconnect ;

tmptableConnection :=dmconnect ;

tmptableTableName:= 'exam_answer ';

with tmpquery2 do

begin

close;

sqlClear;

sqlAdd( 'delete from exam_answer ');

execsql;

end;

tmptableLoadFromFile(tmpfile);

tmptableFirst;

while not tmptableEof do

begin

with tmpquery2 do

begin

close;

sqlClear;

sqlAdd( 'insert into exam_answer(kh,xm,sj_id,sjts,tk_id,stbh,tx,daa,dab,dac,dad,dae,daf,tkda,df) values(:kh,:xm,:sj_id,:sjts,:tk_id,:stbh,:tx,:daa,:dab,:dac,:dad,:dae,:daf,:tkda,:df) ');

parametersParamByName( 'kh ')Value :=tmptablefieldbyname( 'kh ')AsString;

parametersParamByName( 'xm ')Value :=tmptablefieldbyname( 'xm ')AsString;

parametersParamByName( 'sj_id ')Value :=tmptablefieldbyname( 'sj_id ')AsString;

parametersParamByName( 'tx ')Value :=tmptablefieldbyname( 'tx ')AsString;

parametersParamByName( 'sjts ')Value :=tmptablefieldbyname( 'sjts ')AsString;

parametersParamByName( 'tk_id ')Value :=tmptablefieldbyname( 'tk_id ')AsString;

parametersParamByName( 'stbh ')Value :=tmptablefieldbyname( 'stbh ')AsString;

parametersParamByName( 'daa ')Value :=tmptablefieldbyname( 'daa ')AsBoolean;

parametersParamByName( 'dab ')Value :=tmptablefieldbyname( 'dab ')asboolean;

parametersParamByName( 'dac ')Value :=tmptablefieldbyname( 'dac ')AsBoolean ;

parametersParamByName( 'dad ')Value :=tmptablefieldbyname( 'dad ')AsBoolean ;

parametersParamByName( 'dae ')Value :=tmptablefieldbyname( 'dae ')AsBoolean ;

parametersParamByName( 'daf ')Value :=tmptablefieldbyname( 'daf ')AsBoolean ;

parametersParamByName( 'tkda ')Value :=tmptablefieldbyname( 'tkda ')AsString;

parametersParamByName( 'df ')Value :=tmptablefieldbyname( 'df ')AsString;

execsql;

end;

tmptablenext;

end;

showmessage( '导入完成,累死了 ')

end;

finally

tmptableFree;

tmpquery2Free;

end;

except

showmessage( '导入未完成。:( ')

end;

end;

end;

如果单纯的LOAD出来,它是不直接写到后台数据表的。分析下源码就知道它LOAD之前先CLOSE了数据表。只有一条一条增加到后台数据表了。

package test11;

import javaxxmlparsers;

import orgw3cdom;

import javaio;

public class XMLUtil

{

//该方法用于从XML配置文件中提取具体类类名,并返回一个实例对象

public static Object getBean()

{

try

{

//创建文档对象

DocumentBuilderFactory dFactory = DocumentBuilderFactorynewInstance();

DocumentBuilder builder = dFactorynewDocumentBuilder();

Document doc;

doc = builderparse(new File("configxml"));

//获取包含类名的文本节点

NodeList nl = docgetElementsByTagName("className");

Node classNode=nlitem(0)getFirstChild();

String cName=classNodegetNodeValue();

//通过类名生成实例对象并将其返回

Class c=ClassforName(cName);

Object obj=cnewInstance();

return obj;

}

catch(Exception e)

{

eprintStackTrace();

return null;

}

}

}

<xml version="10">

<config>

<className>test11CatAdapter</className>

</config>

然后你吧解析处理的值放到数组或LIST或其他的你能存放的对象中。再写SQL插入到数据库就好了啊。主要数据库事务处理或用批处理

产生原因是xml文件中包含低位非打印字符造成的

处理方法:在产生xml文件的时候,过滤低位非打印字符

public static string ReplaceLowOrderASCIICharacters(string tmp)

{

StringBuilder info = new StringBuilder();

foreach (char cc in tmp)

{

int ss = (int)cc;

if (((ss >= 0) && (ss <= 8)) || ((ss >= 11) && (ss <= 12)) || ((ss >= 14) && (ss <= 32)))

infoAppendFormat(" ", ss);//{0:X};

else infoAppend(cc);

}

return infoToString();

}

以上就是关于c#读取xml 中的数据库连接字符串全部的内容,包括:c#读取xml 中的数据库连接字符串、SQL怎么提取字段中XML内容,并插入到 ​另外一张表、oracle数据库如何解析XML数据获取值等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/9734031.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-01
下一篇 2023-05-01

发表评论

登录后才能评论

评论列表(0条)

保存