[英]Visual Studio Web, I am trying to retrieve value from SQL, how do I check whether value is NULL?
[英]How do I check whether a value is 1 or 0 in SQL?
因此,我已經設置了數據庫連接,並且它確實連接了。 現在,我想檢查表中的每一行,是否已isactivated
該列是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;
}
}
SqlCommand command = new SqlCommand("SELECT column FROM table", connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
// 0 = false, 1 = true,
bool result = (bool)reader[0];
//... do whatever you wanna do with the result
}
我認為您正在尋找這樣的東西:
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();
取自msdn-lib 。
在SqlDataReader中應該是所有數據,包括isactivated列,然后可以檢查其各自的值。
如果您想閱讀該字段,則可以實現以下內容:
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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.