小编Kel*_*use的帖子

在 C# 中将 SqlDataReader 列值转换为 json 字符串

我想根据 SQLDataReader 读取的输出输出一个字符串(或者可能是其他变量类型,如果字符串不理想)。我一直在玩“while”循环, while (reader.Read())

我需要得到一些可以用 json.net 序列化的东西。

这是我当前的代码 - 它输出到调试日志只是为了确保我成功地从数据库读取。

protected void Page_Load(object sender, EventArgs e)
    {

        string connectionString = "Data Source =.\\SQLEXPRESS; Initial Catalog = TeamProject; Integrated Security = True; MultipleActiveResultSets = True";

        string querystring = "RosterMake";

        using (SqlConnection connection = 
            new SqlConnection(connectionString))
        {
            SqlCommand command = new SqlCommand(querystring, connection);
            command.CommandType = CommandType.StoredProcedure;

            try
            {
                connection.Open();
                SqlDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    System.Diagnostics.Debug.WriteLine("\t{0}\t{1}\t{2}",
                    reader[0], reader[1], reader[2]);

                    //  What do I put here to make the results something …
Run Code Online (Sandbox Code Playgroud)

.net c# asp.net ado.net json

2
推荐指数
2
解决办法
6563
查看次数

标签 统计

.net ×1

ado.net ×1

asp.net ×1

c# ×1

json ×1