在C#中从SQL检索信息 [英] Retrieve information from SQL in C#
问题描述
我目前在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检索信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!