C# 如何在SQL中检查值是1还是0?
所以我设置了数据库连接,它确实连接了。 现在我想检查表中的每一行C# 如何在SQL中检查值是1还是0?,c#,mysql,sql,.net,C#,Mysql,Sql,.net,所以我设置了数据库连接,它确实连接了。 现在我想检查表中的每一行列是否被激活是0还是1,因为它是位,但我不知道怎么做 我会执行一个查询吗?我是否将所有行存储在一个列表中,然后进行检查 using (var connection = new SqlConnection(cb.ConnectionString)) { try { connection.Open(); if (connection.State == ConnectionState.Ope
列是否被激活
是0
还是1
,因为它是位,但我不知道怎么做
我会执行一个查询吗?我是否将所有行存储在一个列表中,然后进行检查
using (var connection = new SqlConnection(cb.ConnectionString))
{
try
{
connection.Open();
if (connection.State == ConnectionState.Open)
{
Console.WriteLine("Connected!");
}
else
{
Console.WriteLine("Failed..");
}
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
我想你正在寻找这样的东西:
SqlConnection sqlConnection1 = new SqlConnection("Your Connection String");
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;
cmd.CommandText = "SELECT * FROM Customers";
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
reader = cmd.ExecuteReader();
// Data is accessible through the DataReader object here.
sqlConnection1.Close();
using (var connection = new SqlConnection(cb.ConnectionString)) {
try {
connection.Open();
// connection.Open() either succeeds (and so we print the message)
// or throw an exception; no need to check
Console.WriteLine("Connected!");
//TODO: edit the SQL if required
string sql =
@"select IsActivated
from MyTable";
using (var query = new SqlCommand(sql, connection)) {
using (var reader = query.ExecuteReader()) {
while (reader.Read()) {
// Now it's time to answer your question: let's read the value
// reader["IsActivated"] - value of IsActivated as an Object
// Convert.ToBoolean - let .Net do all low level work for you
bool isActivated = Convert.ToBoolean(reader["IsActivated"]);
// Uncomment, if you insist on 0, 1
// int bit = Convert.ToInt32(reader["IsActivated"]);
//TODO: field has been read into isActivated; put relevant code here
}
}
}
}
catch (DataException e) { // Be specific: we know how to process data errors only
Console.WriteLine(e);
throw;
}
}
摘自
SqlDataReader中应该包含所有数据,包括isactivated列,然后您可以检查其各自的值。我想您正在寻找类似以下内容:
SqlConnection sqlConnection1 = new SqlConnection("Your Connection String");
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;
cmd.CommandText = "SELECT * FROM Customers";
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
reader = cmd.ExecuteReader();
// Data is accessible through the DataReader object here.
sqlConnection1.Close();
using (var connection = new SqlConnection(cb.ConnectionString)) {
try {
connection.Open();
// connection.Open() either succeeds (and so we print the message)
// or throw an exception; no need to check
Console.WriteLine("Connected!");
//TODO: edit the SQL if required
string sql =
@"select IsActivated
from MyTable";
using (var query = new SqlCommand(sql, connection)) {
using (var reader = query.ExecuteReader()) {
while (reader.Read()) {
// Now it's time to answer your question: let's read the value
// reader["IsActivated"] - value of IsActivated as an Object
// Convert.ToBoolean - let .Net do all low level work for you
bool isActivated = Convert.ToBoolean(reader["IsActivated"]);
// Uncomment, if you insist on 0, 1
// int bit = Convert.ToInt32(reader["IsActivated"]);
//TODO: field has been read into isActivated; put relevant code here
}
}
}
}
catch (DataException e) { // Be specific: we know how to process data errors only
Console.WriteLine(e);
throw;
}
}
摘自
SqlDataReader中应该包含您的所有数据,包括您的isactivated列,然后您可以检查其各自的值。如果您想读取字段,您可以实现如下操作:
SqlConnection sqlConnection1 = new SqlConnection("Your Connection String");
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;
cmd.CommandText = "SELECT * FROM Customers";
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
reader = cmd.ExecuteReader();
// Data is accessible through the DataReader object here.
sqlConnection1.Close();
using (var connection = new SqlConnection(cb.ConnectionString)) {
try {
connection.Open();
// connection.Open() either succeeds (and so we print the message)
// or throw an exception; no need to check
Console.WriteLine("Connected!");
//TODO: edit the SQL if required
string sql =
@"select IsActivated
from MyTable";
using (var query = new SqlCommand(sql, connection)) {
using (var reader = query.ExecuteReader()) {
while (reader.Read()) {
// Now it's time to answer your question: let's read the value
// reader["IsActivated"] - value of IsActivated as an Object
// Convert.ToBoolean - let .Net do all low level work for you
bool isActivated = Convert.ToBoolean(reader["IsActivated"]);
// Uncomment, if you insist on 0, 1
// int bit = Convert.ToInt32(reader["IsActivated"]);
//TODO: field has been read into isActivated; put relevant code here
}
}
}
}
catch (DataException e) { // Be specific: we know how to process data errors only
Console.WriteLine(e);
throw;
}
}
如果要读取字段,可以实现如下操作:
SqlConnection sqlConnection1 = new SqlConnection("Your Connection String");
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;
cmd.CommandText = "SELECT * FROM Customers";
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
reader = cmd.ExecuteReader();
// Data is accessible through the DataReader object here.
sqlConnection1.Close();
using (var connection = new SqlConnection(cb.ConnectionString)) {
try {
connection.Open();
// connection.Open() either succeeds (and so we print the message)
// or throw an exception; no need to check
Console.WriteLine("Connected!");
//TODO: edit the SQL if required
string sql =
@"select IsActivated
from MyTable";
using (var query = new SqlCommand(sql, connection)) {
using (var reader = query.ExecuteReader()) {
while (reader.Read()) {
// Now it's time to answer your question: let's read the value
// reader["IsActivated"] - value of IsActivated as an Object
// Convert.ToBoolean - let .Net do all low level work for you
bool isActivated = Convert.ToBoolean(reader["IsActivated"]);
// Uncomment, if you insist on 0, 1
// int bit = Convert.ToInt32(reader["IsActivated"]);
//TODO: field has been read into isActivated; put relevant code here
}
}
}
}
catch (DataException e) { // Be specific: we know how to process data errors only
Console.WriteLine(e);
throw;
}
}
检查您的行,然后对它们做什么?@常规
我想检查表中的每一行,列是被激活的是0还是1,因为它有点,没有回答我的问题,您想数一数吗,您想过滤我的字段吗,你只想检查一下是否存在这是迄今为止初学者最多的问题,任何一个初学者教程都能回答这个问题。没有人天生就具备你需要学习的所有知识。检查你的行,然后用它们做什么?@TheGeneral我想检查我表格中的每一行,列是被激活的是0还是1,因为它有点不符合我的问题,你想数一数吗,你想过滤我的这个字段吗,你只想检查一下是否存在这是迄今为止初学者最多的问题,任何一个初学者教程都能回答这个问题。没有人天生就拥有你需要学习的所有知识。