sql server 字符转换问题,急急急

图片说明

11个回答

不好意思,刚刚发的太急,有错误,以这个为准
图片说明

刚刚一直有问题,无法显示,现在完整发一遍,麻烦点击一下采纳 ,谢谢

--把数据库名字 demoysc32 换成你自己的数据库名字就可以了
use demoysc32 
declare @myxml xml
set @myxml='<Peoples>
                <people>
                    <Name>dongsheng</Name>
                    <Sex>男</Sex>
                    <QQ>423545</QQ>
                </people>
                <people>
                    <Name>土豆</Name>
                    <Sex>男</Sex>
                    <QQ>123123</QQ>
                </people>
                <people>
                    <Name>choushuigou</Name>
                    <Sex>女</Sex>
                    <QQ>54543545</QQ>
                </people>
            </Peoples>'

--获得所有student节点的数据,每一行显示一条people节点的数据      
/*select @myxml.query('.')  as result from @myxml.nodes('/Peoples/people') as  T(c) */
--将这些数据显示为一个表格
select  T.c.value('(./Name)[1]','nvarchar(16)') as name,
        T.c.value('(./Sex)[1]','nvarchar(4)') as sex,
        T.c.value('(./QQ)[1]','nvarchar(16)') as QQ
        from @myxml.nodes('/Peoples/people') as T(c)

执行结果,就是你想要的

图片说明

上面是XML数据格式,
数据Model
public class People
{
public string NAME { get; set; }
public string SEX { get; set; }
public string QQ { get; set; }

}
    用以下方法从文件中直接读取出对象List<People>  然后就是将对象插入SQLserver数据库对应字段--- 别说这个不会!!
     /// <summary>
    /// 反序列化
    /// </summary>
    /// <returns></returns>
    public static TModel Deserialize<TModel>(string fileName)
    {
        TModel ob;
        if (File.Exists(fileName) == false)
            File.Create(fileName).Close();
        FileStream f = new FileStream(fileName, FileMode.Open);
        XmlSerializer sr = new XmlSerializer(typeof(TModel));
        try
        {
            ob = (TModel)sr.Deserialize(f);
            f.Close();
        }
        catch (Exception e)
        {
            string b = e.Message;
            f.Close();
            return default(TModel);
        }
        return ob;
    }

use testDb
declare @myxml xml
set @myxml='

dongsheng

423545


土豆

123123


choushuigou

54543545

'

--获得所有student节点的数据,每一行显示一条people节点的数据

select @myxml.query('.') as result from @myxml.nodes('/Peoples/people') as T(c)

--将这些数据显示为一个表格
select T.c.value('(@id)[1]','int') as id,
T.c.value('(./Name)[1]','nvarchar(16)') as name,
T.c.value('(./Sex)[1]','nvarchar(4)') as sex,
T.c.value('(./QQ)[1]','nvarchar(16)') as QQ
from @myxml.nodes('/people/student') as T(c)

use testDb
declare @myxml xml
set @myxml='

dongsheng

423545


土豆

123123


choushuigou

54543545

'

--获得所有student节点的数据,每一行显示一条people节点的数据

select @myxml.query('.') as result from @myxml.nodes('/Peoples/people') as T(c)

--将这些数据显示为一个表格
select T.c.value('(@id)[1]','int') as id,
T.c.value('(./Name)[1]','nvarchar(16)') as name,
T.c.value('(./Sex)[1]','nvarchar(4)') as sex,
T.c.value('(./QQ)[1]','nvarchar(16)') as QQ
from @myxml.nodes('/people/student') as T(c)


这个真不好用,插入代码段,我截图吧

图片说明

别人帖子都有写过,可以去看看;
https://blog.csdn.net/kk185800961/article/details/42277035

use demoysc32 这里数据库名字换成你自己的就可以了

共11条数据 1 尾页
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问