⑴ 各位老師:我想寫一個xml數據過c#程序導入到sql中怎麼寫,需要分幾步學哪些知識,謝謝
你好,我做了一份建議的demo,希望對你又幫助
一份Xml 文件,內容如下:
privatevoidbutton1_Click(objectsender,EventArgse)
{
stringxmlPath=@"XMLFile1.xml";
List<StudentInfo>allStudents=newList<StudentInfo>();
//1.載入XML
XmlDocumentxmlDoc=newXmlDocument();
xmlDoc.Load(xmlPath);
//2.讀取XML里的數據
XmlNodeListnodeList=xmlDoc.SelectNodes("MyClass//Students//Student");
for(inti=0;i<nodeList.Count;i++)
{
varstu=nodeList[i].ChildNodes;
StudentInfostudent=newStudentInfo();
student.Name=stu[0].InnerText;
student.Address=stu[1].InnerText;
student.Phone=stu[2].InnerText;
allStudents.Add(student);
}
//3.開始把數據寫入資料庫。
//需要學習的只是:SQL的增、刪、改、查
stringconnPath=@"DataSource=.SQLEXPRESS;AttachDbFilename=C:tTest.mdf;IntegratedSecurity=True;UserInstance=True";
using(SqlConnectionconn=newSqlConnection(connPath))
{
conn.Open();
using(SqlCommandcmd=conn.CreateCommand())
{
cmd.CommandText="insertintoStudentsvalues(@StudentName,@StudentAddress,@StudentPhone)";
for(inti=0;i<allStudents.Count;i++)
{
cmd.Parameters.AddWithValue("StudentName",allStudents[i].Name);
cmd.Parameters.AddWithValue("StudentAddress",allStudents[i].Address);
cmd.Parameters.AddWithValue("StudentPhone",allStudents[i].Phone);
}
cmd.ExecuteNonQuery();
}
conn.Close();
}
MessageBox.Show("操作完成。");
}祝你好運!加油 有問題 可以在溝通。。。
⑵ sql select得到xml集合 當成表,怎麼寫
這種情況下還是要判斷下兩個表的結構是否相同,如果相同,就可以用union函數。
如:create table b
select * from a
union
select * from c
這樣,從a表中查詢出來的結果union 表c中查詢的結果,創建一個新表b
⑶ 如何在sql中利用xml提取需要的數據,我想提取出所有location內的stationId,lon,lat數據
不能被函數調用是什麼意思?你的函數呢?函數不寫出來怎麼幫你看?
我這個是假設你的到了XmlFiles/StuElements.xml,你不是已經通過File.CreateText(stuFileName)創建了該文件了嗎?
我建議你直接DataSet1.WriteXml(filepath);這種方式直接把DataSet生成xml文件
string strFilePath = Server.MapPath ( "XmlFiles/StuElements.xml" );
System.Xml.XmlDocument xmlDocument = new System.Xml.XmlDocument ( );
xmlDocument.Load ( strFilePath );
System.Xml.XmlElement xmlElement = xmlDocument.DocumentElement;
foreach ( System.Xml.XmlNode node in xmlElement.ChildNodes )
{
Response.Write ( "lat: " + node [ "lat" ].InnerText + ", lng: " + node [ "lng" ].InnerText + "<br />" );
}
//修改後的
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection("Data Source=.;Initial Catalog=Pubs;Integrated Security=True;");
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter("Select * from Authors", conn);
System.Data.DataSet ds = new System.Data.DataSet();
conn.Open();
da.Fill(ds, "Authors");
System.Data.DataTable dt;
dt = ds.Tables["Authors"];
foreach (System.Data.DataColumn dc in dt.Columns)
{
dc.ColumnMapping = System.Data.MappingType.Attribute;
}
string filePath = Server.MapPath("XmlFiles/Stu.xml");
ds.WriteXml(filePath);
System.Xml.XmlDocument xmlDocument = new System.Xml.XmlDocument();
xmlDocument.Load(filePath);
System.Xml.XmlNodeList Authors= xmlDocument.GetElementsByTagName("Author");
foreach (System.Xml.XmlNode node in Authors)
{
Response.Write("FirstName:" + node.Attributes["FirstName"].Value + "<br />");
}
⑷ sql中的openxml函數的使用
declare @xml varchar(8000)
set @xml=(SELECT * FROM OPENROWSET(
BULK 'D:\peo.xml',
SINGLE_CLOB) as x)
select @xml
Unicode 格式的文件用SINGLE_NCLOB.
⑸ sql中xml怎麼使用
using System;
using System.Xml;
public class Test
{
public static void Main(string[] args)
{
string xml = @"<a>b</a>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
XmlNode node = doc.SelectSingleNode("a");
XmlNode sqlNode = doc.CreateElement("sql");
sqlNode.InnerText = "Select * From T_User Where Name = '樓主'";
node.AppendChild(sqlNode);
doc.Save(Console.Out);
}
}
⑹ 求sql server2008 xml類型查詢條件怎麼寫
求sql server2008 xml類型查詢條件怎麼寫
/*
sql xml 入門:
--by jinjazz
--http://blog.csdn.net/jinjazz
1、xml: 能認識元素、屬性和值
2、xpath: 定址語言,類似windows目錄的查找(沒用過dir命令的話就去面壁)
語法格式,這些語法可以組合為條件:
"."表示自己,".."表示父親,"/"表示兒子,"//"表示後代,
"name"表示按名字查找,"@name"表示按屬性查找
"集合[條件]" 表示根據條件取集合的子集,條件可以是
數 值:數字,last(),last()-數字 等
布爾值:position()<數字,@name='條件',name='條件'
條件是布爾值的時候可以合並計算:and or
3、xquery: 基於xpath標的准查詢語言,sqlserver xquery包含如下函數
exist(xpath條件):返回布爾值表示節點是否存在
query(xpath條件):返回由符合條件的節點組成的新的xml文檔
value(xpath條件,數據類型):返回指定的標量值,xpath條件結果必須唯一
nodes(xpath條件): 返回由符合條件的節點組成的一行一列的結果表
*/
⑺ xml文件里如何寫sql語句實現年齡分組
xml 文件中 小於號 < 要用 < 代替吧。
selectt1.p1asnum1,t2.p2asnum2,t3.p3asnum3,t4.p4asnum4,t5.p5asnum5from
(selectcount(age)asp1frompersonwhereage>=0andage<11)t1INNERJOIN
(selectcount(age)asp2frompersonwhereage>=11andage<21)t2INNERJOIN
(selectcount(age)asp3frompersonwhereage>=21andage<31)t3INNERJOIN
(selectcount(age)asp4frompersonwhereage>=31andage<41)t4INNERJOIN
(selectcount(age)asp5frompersonwhereage>=41andage<51)t5
希望能幫到你
⑻ sql 2005資料庫 如何操作xml
查詢操作
在定義了一個XML類型的數據之後,我們最常用的就是查詢操作,下面我們來介紹如何使用SQL語句來進行查詢操作的。
在T-Sql中,提供了兩個對XML類型數據進行查詢的函數,分別是query(xquery)和value(xquery,
dataType),其中,query(xquery)得到的是帶有標簽的數據,而value(xquery,
dataType)得到的則是標簽的內容。接下類我們分別使用這兩個函數來進行查詢。
1
、使用query(xquery)
查詢
我們需要得到書的標題(title),使用query(xquery)來進行查詢,查詢語句為:
select
@xmlDoc.query('/book/title')
2
、使用value(xquery,
dataType)
查詢
同樣是得到書的標題,使用value函數,需要指明兩個參數,一個為xquery,
另一個為得到數據的類型。看下面的查詢語句:
select
@xmlDoc.value('(/book/title)[1]',
'nvarchar(max)')
3
、查詢屬性值
無論是使用query還是value,都可以很容易的得到一個節點的某個屬性值,例如,我們很希望得到book節點的id,我們這里使用value方法進行查詢,語句為:
select
@xmlDoc.value('(/book/@id)[1]',
'nvarchar(max)')
4
、使用xpath進行查詢
xpath是統一的Xml查詢語句。使用XPath可以方便的得到想要的節點,而不用使用where語句。例如,我們在@xmlDoc中添加了另外一個節點
例如
:得到id為0002的book節點
select
@xmlDoc.query('(/root/book[@id="0002"])')
參考資料:SQL如何操作Xml欄位
http://www.studyofnet.com/news/490.html
⑼ 在xml文件中插入sql語句
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
Response.ContentType="application/xml"
Response.Charset="utf-8"
response.cachecontrol="no-cache"
response.addHeader "pragma","no-cache"
response.expires=-1
response.expiresAbsolute=now-1
db="file.mdb" '資料庫路徑,相對路徑
set conn = server.CreateObject("adodb.connection")
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath(db)
Response.Write("<?xml version=""1.0"" encoding=""utf-8""?>")
set rs=Server.CreateObject("adodb.recordset")
rs.open "SELECT * FROM [file]",conn,1,1
if rs.Eof and rs.Bof then
Response.Write("<vcaster>")
Response.Write ("<item item_url=""no file"" item_title=""no file"">")
Response.Write("</item>")
Response.Write ("</vcaster>")
else
Response.Write("<vcaster>")
do while Not rs.Eof
Response.Write ("<item item_url="""&rs("fileurl")&""" item_title="""&rs("name")&""">")
Response.Write("</item>")
if rs.Eof then
Exit do
End if
rs.MoveNext
Loop
Response.Write ("</vcaster>")
End if
rs.Close
Set rs=Nothing
conn.Close
Set conn=Nothing
%>
⑽ mybatis在xml中生成guid,插入到mysql資料庫,sql怎麼寫
首先mysql 沒有guid()函數,oracle才有,mysql的是 uuid()函數,使用方法是:
insertintopub_attachments(vchr_id,vchr_domain))values(uuid(),'ibm');