简单的SQL问题.... [英] Simple SQL question....

查看:56
本文介绍了简单的SQL问题....的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以这可能是一个非常简单的问题,但是由于网络上所有可搜索的内容都难以确定答案.

我要做的就是从sql表中获取一条数据并将该条数据分配给一个变量,

例如.

string aPieceOfData =从sql表中获取的一条数据"

我可以毫无问题地设置一条select语句,但是如何使用该select语句来获取数据并将其放入变量中

ok so this is probably a really simple question but with all the googleable content on the net im having some trouble narrowing down the answer..

All I want to do is grab a piece of data out of a sql table and assign that piece of data to a variable,

eg.

string aPieceOfData = "A piece of data taken from a sql table"

I can setup a select statement without any problems but how do I use that select statement to grab the data and put it in a variable

推荐答案

Try:
Try:
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("SELECT iD, description FROM myTable", con))
        {
        using (SqlDataReader reader = com.ExecuteReader())
            {
            while (reader.Read())
                {
                int id = (int) reader["iD"];
                string desc = (string) reader["description"];
                Console.WriteLine("ID: {0}\n    {1}", iD, desc);
                }
            }
        }
    }



[edit]错字:键入时大写锁定...-OriginalGriff [/edit]



[edit]Typo: Caps lock on while typing... - OriginalGriff[/edit]


string ConnectionString = "Your Data Source";
string Val = "";
using (SqlConnection con = new SqlConnection(ConnectionString ))
{
    con.Open();
    using (SqlCommand com = new SqlCommand("SELECT Empname FROM Emp", con))
    {
         Val = com.ExecuteScalar(); 
    }
}


Select count(*) as varname from tablename where condiotion=con



使用此SQL Querry. U将值赋入varname



use this sql querry. U will get value into varname

using (SqlDataReader reader = com.ExecuteReader())
                        {
                        while (reader.Read())
                            {
                            int count = (int) reader["varname"];
                            Console.WriteLine("ID: {0}\n    {1}", varname);
                            }
                        }
                    }


这篇关于简单的SQL问题....的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆