asp sql datareader如何把数据集转成json对象
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了asp sql datareader如何把数据集转成json对象相关的知识,希望对你有一定的参考价值。
'先根据所需json创建一个class''' <summary>
''' JSON序列化
''' </summary>
''' <typeparam name="T"></typeparam>
''' <param name="type"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function JsonSerializer(Of T)(type As T) As String
Dim ser As New DataContractJsonSerializer(GetType(T))
Dim ms As New MemoryStream()
ser.WriteObject(ms, type)
Dim jsonString As String = Encoding.UTF8.GetString(ms.ToArray())
ms.Close()
Return jsonString
End Function
''' <summary>
''' JSON反序列化
''' </summary>
''' <typeparam name="T"></typeparam>
''' <param name="jsonString"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function JsonDeserialize(Of T)(jsonString As String) As T
Dim ser As New DataContractJsonSerializer(GetType(T))
Dim ms As New MemoryStream(Encoding.UTF8.GetBytes(jsonString))
Dim obj As T = DirectCast(ser.ReadObject(ms), T)
Return obj
End Function 参考技术A 写成json格式追问
只能自己拼凑吗?还是说有更好的方法
如何使用 DataReader 提取 SQL Schema
【中文标题】如何使用 DataReader 提取 SQL Schema【英文标题】:How to pull SQL Schema using DataReader 【发布时间】:2014-11-06 09:14:32 【问题描述】:我们是否可以从表中提取 SQL 数据库架构?
以下是我的代码,但现在我可以像
一样提取整个架构Create Table Employee(code int, name blah blah...)
Dim sqlQuery As String
Dim textInsertQueryLine As String
Dim tableSchema As DataTable
Dim tableField As DataRow
Dim tableProperty As DataColumn
Dim SourceConn As New SqlConnection(sourceDBPath)
Dim DestinationConn As New SqlConnection(destinationDBPath)
SourceConn.Open()
DestinationConn.Open()
sqlQuery = "SELECT name FROM sys.tables"
Dim cmdX As New SqlCommand(sqlQuery, SourceConn)
Dim readerX As SqlDataReader = cmdX.ExecuteReader
Do While readerX.Read
Dim cmdY As New SqlCommand(sqlQuery, DestinationConn)
Dim readerY As SqlDataReader = cmdY.ExecuteReader
Do While readerY.Read
If readerX.GetString(0) = readerY.GetString(0) Then
txtConsoleView.AppendText(readerX.GetString(0) + "Matched. " + vbCrLf)
Else
tableSchema = readerX.GetSchemaTable()
txtConsoleView.AppendText(tableSchema.ToString + vbCrLf)
End If
Loop
readerY.Close()
Loop
readerX.Close()
SourceConn.Close()
DestinationConn.Close()
【问题讨论】:
你可以使用SqlDataReader.GetSchemaTable
。
readerX.GetSchemaTable() 我使用了这一行,但它只返回表名
因为你使用了SELECT name FROM sys.tables
,但你必须从sys.tables
返回的真实表中SELECT *
。
【参考方案1】:
这是我从头开始编写的一种工作方法,因此没有经过真正的测试。它使用DataReader.GetSchemaTable()
列出每个表的所有列:
您可以使用这些类来映射架构信息,您可以根据this list添加更多:
Public Class Table
Public Property DatabaseName As String
Public Property TableName As String
Public Property Schema As String
Public Property FullName As String
Public Property AllColumns As New List(Of TableColumn)
Public Overrides Function ToString() As String
Return FullName
End Function
End Class
Public Class TableColumn
Public Property ColumnName As String
Public Property DataType As Type
Public Property Size As Int32
Public Property ColumnOrdinal As Int32
Public Property AllowDBNull As Boolean
Public Property IsAutoIncrement As Boolean
Public Overrides Function ToString() As String
Return String.Format("0(1)", ColumnName, DataType.ToString())
End Function
End Class
此代码将所有表及其所有列读入一个列表:
Dim allTables As New List(Of Table)
Using con As New SqlConnection(My.Settings.RM2ConnectionString) ' use your connection-string '
Using sysTblCommand As New SqlCommand("SELECT [Database]=DB_NAME(DB_ID()),FullName='['+SCHEMA_NAME(schema_id)+'].['+name+']',[Schema]=SCHEMA_NAME(schema_id),Name FROM sys.tables ORDER BY schema_id,name", con)
con.Open()
Using readerSys = sysTblCommand.ExecuteReader()
While readerSys.Read()
Dim table As New Table()
table.DatabaseName = readerSys.GetString(0)
table.FullName = readerSys.GetString(1)
table.Schema = readerSys.GetString(2)
table.TableName = readerSys.GetString(3)
allTables.Add(table)
End While
End Using
For Each table In allTables
Using tblCommand As New SqlCommand("SELECT * FROM " & table.ToString(), con)
tblCommand.CommandTimeout = 0
Using rd = tblCommand.ExecuteReader()
Dim schemaTable As DataTable = rd.GetSchemaTable()
For Each row As DataRow In schemaTable.Rows
Dim col As New TableColumn()
col.ColumnName = row.Field(Of String)("ColumnName")
col.DataType = row.Field(Of Type)("DataType")
col.Size = row.Field(Of Int32)("ColumnSize")
col.ColumnOrdinal = row.Field(Of Int32)("ColumnOrdinal")
col.AllowDBNull = row.Field(Of Boolean)("AllowDBNull")
col.IsAutoIncrement = row.Field(Of Boolean)("IsAutoIncrement")
table.AllColumns.Add(col)
Next
End Using
End Using
Next
End Using
End Using
【讨论】:
我正在尝试这段代码,但是 allTables.Add(table) 是什么?数据集还是什么?我找不到 @kirk:对不起,我没有复制粘贴它,它是一个列表(表)。我现在已将其添加到苹果酒中。【参考方案2】:从数据库中获取模式信息是通过模式发现过程完成的。
架构发现允许应用程序请求托管提供程序查找并返回有关给定数据库的数据库架构(也称为元数据)的信息。
不同的数据库架构元素(例如表、列和存储过程)通过架构集合公开。
每个架构集合都包含特定于所使用的提供程序的各种架构信息。
每个 .NET Framework 托管提供程序都在 Connection 类中实现 GetSchema 方法,并且从 GetSchema 方法返回的架构信息以 DataTable 的形式出现。
GetSchema 方法是一个重载方法,它提供可选参数来指定要返回的架构集合,并限制返回的信息量。
更多信息:MSDN LINK
例子:
Imports System.Data.SqlClient
Module Module1
Sub Main()
Dim connectionString As String = GetConnectionString()
Using connection As New SqlConnection(connectionString)
'Connect to the database then retrieve the schema information.
connection.Open()
Dim table As DataTable = connection.GetSchema("Tables")
' Display the contents of the table.
DisplayData(table)
Console.WriteLine("Press any key to continue.")
Console.ReadKey()
End Using
End Sub
Private Function GetConnectionString() As String
' To avoid storing the connection string in your code,
' you can retrieve it from a configuration file.
Return "Data Source=(local);Database=AdventureWorks;" _
& "Integrated Security=true;"
End Function
Private Sub DisplayData(ByVal table As DataTable)
For Each row As DataRow In table.Rows
For Each col As DataColumn In table.Columns
Console.WriteLine("0 = 1", col.ColumnName, row(col))
Next
Console.WriteLine("============================")
Next
End Sub
End Module
【讨论】:
以上是关于asp sql datareader如何把数据集转成json对象的主要内容,如果未能解决你的问题,请参考以下文章
在标签控件 (ASP.NET) 中显示来自 DataReader 的数据
DataReader + DropDownList ASP.NET 项目