在C#中从SQL检索信息

编程入门 行业动态 更新时间:2024-10-21 19:30:17
本文介绍了在C#中从SQL检索信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我目前在SQL Server中有一个列名为taskID和MDXQuery的表.

I currently has a table with column name taskID and MDXQuery in SQL Server.

我想在C#中检索这些信息,并将这些值分别存储在局部变量taskID和strMDX中.

I would like to retrieve those information in C# and store those values in local variables taskID and strMDX respectively.

我有:

使用系统;使用System.Collections.Generic;使用System.Linq;使用System.Text;使用System.Threading.Tasks;使用Microsoft.AnalysisServices.AdomdClient;使用System.Data;使用System.IO;使用System.Data.SqlClient;

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.AnalysisServices.AdomdClient; using System.Data; using System.IO; using System.Data.SqlClient;

////通过以下方法创建方法并建立连接:

///Creating a method and setting up connection by:

静态公共字符串SQLconnectionMDXstatement() { SqlConnection con =新的SqlConnection(); con.ConnectionString = @与服务器的连接"; con.Open(); 字符串queryString =从MDXQueryIndex WHERE TaskID = 24中选择*";

static public string SQLconnectionMDXstatement() { SqlConnection con = new SqlConnection(); con.ConnectionString = @"connection with my server"; con.Open(); string queryString = "select * from MDXQueryIndex WHERE TaskID = 24";

使用(SqlConnection连接=新的SqlConnection(connString)) 使用(SqlCommand命令= connection.CreateCommand()) { command.CommandText = queryString;

using (SqlConnection connection = new SqlConnection(connString)) using (SqlCommand command = connection.CreateCommand()) { command.CommandText = queryString;

connection.Open();

connection.Open();

使用(SqlDataReader reader = command.ExecuteReader()) { while(reader.Read()) { nbsp; bsp /> nbsp; bsp /> b字符串strMDX = reader ["MDXQuery"].ToString(); b字符串taskID = reader ["taskID"].ToString(); } }

using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { string strMDX = reader["MDXQuery"].ToString(); string taskID = reader["taskID"].ToString(); } }

/////////当前,我不确定答案是否已返回或哪里出了问题.请帮忙,谢谢!

/////////currently I am not sure if the answer has been returned or where went wrong. Please help, thank you!

推荐答案

你好,

到目前为止,您的C#代码看起来还可以,并且可以正常工作.在行

So far your C# code looks OK and should work. Set a breakpoint on line

string taskID = reader["taskID"].ToString();

并将鼠标移到"strMDX"上,VS会为您显示变量的值;或标记该变量,右键单击=>快速查看以查看变量的内容.

and move mouse over "strMDX", VS shows you the value for the variable; or mark the variable, right mouse click => Quick view to see the content of the variable.

更多推荐

在C#中从SQL检索信息

本文发布于:2023-10-27 03:18:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1532158.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:信息   SQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!