c#从sql表中获取2列的空值

时间:2022-10-27 09:50:44

Basically before i asked a question on how to return the null values from a columns found from a table in sql. Now i need to know how to do the same thing but for two columns and putting the user name near them which is caught from another column. This means i have 3 tables.

基本上在我问一个关于如何从sql中的表中找到的列返回空值的问题之前。现在我需要知道如何做同样的事情,但是对于两列并将用户名放在它们附近,这是从另一列捕获的。这意味着我有3张桌子。

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace sql_connection
{
class Program
{
static void Main(string[]args)
{

    string conn=null;
    SqlConnection connection;
    conn=("Data Source=Database\\SQL2012;Initial Catalog=Table;User ID=user;Password=example");

     connection=new SqlConnection(conn);
     try{

         connection.Open();
         Console.Writeline("Connection Open!");
         SqlCommand cmd= new SqlCommand("SELECT tablein,tableout,tableuser FROM [dbo].[tb_Showingnull]"WHERE tablein AND tableout IS NULL);
         cmd.Connection = connection;
         SqlDataReader reader = cmd.ExecuteReader();
         while(reader.Read())
          {

               Console.WriteLine("{2}, {1} , {0}", reader["tablein"] == DBNull.Value? "NULL" : reader["tablein"].ToString(); reader["tableout"] == DBNull.Value? "NULL" : reader["tableout"].ToString(); reader["tableuser"].ToString();

           }

           connection.Close();
            }

            catch(Exception ex)
           {
               Console.WriteLine(ex.Message);
            }

            }
        }
  }

1 个解决方案

#1


instead of

SqlCommand cmd= 
new SqlCommand("SELECT tablein,tableout,tableuser FROM 
[dbo].[tb_Showingnull]"WHERE tablein AND tableout IS NULL);

Use

SqlCommand cmd= 
new SqlCommand("SELECT tablein,tableout,tableuser FROM 
[dbo].[tb_Showingnull] WHERE tablein IS NULL AND tableout IS NULL");

#1


instead of

SqlCommand cmd= 
new SqlCommand("SELECT tablein,tableout,tableuser FROM 
[dbo].[tb_Showingnull]"WHERE tablein AND tableout IS NULL);

Use

SqlCommand cmd= 
new SqlCommand("SELECT tablein,tableout,tableuser FROM 
[dbo].[tb_Showingnull] WHERE tablein IS NULL AND tableout IS NULL");