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

时间:2015-05-06 11:29:14

标签: c# sql-server

基本上我在询问如何从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 个答案:

答案 0 :(得分:0)

而不是

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

使用

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