简单的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);
}
}
}