老甲克C++随笔(oldjacky||oldjackyone)  
.........体验真实C++.........

我有一个方案,但不知道存在的问题,优点我知道。
 
请大家参考。 
 
先发代码。
 
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;

namespace VS2005ConsoleTest
{
    class Program
    {
        static void Main(string[] args)
        {
            int status1 = 0;
            int status2 = 0;
            int status3 = 1;
            Books books = new Books();
            books.GetBooksData(int.Parse(status1.ToString() + status2.ToString() + status3.ToString()));
        }
    }
    class Books
    {
        System.Data.SqlClient.SqlConnection sqlConn;
        public Books()
        {
            sqlConn = new SqlConnection("Data Source=(local);user id=sa;password=123;Initial Catalog=Test");
            sqlConn.Open();
        }

        public void GetBooksData(int condition )
        {
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.CommandText = "select * from Books where convert(varchar(1),classone)+convert(varchar(1),classtwo)+convert(varchar(1),ClassThree)=" + condition.ToString();
            sqlCommand.Connection = sqlConn;
            using (SqlDataReader reader = sqlCommand.ExecuteReader())
            {
                while (reader.Read())
                {
                    Console.WriteLine(reader["BookTitle"].ToString() + " " + reader["ClassOne"] + " " + reader["ClassTwo"] + " " + reader["ClassThree"]);
                }
            }
            Console.Read();
        }
    }
}
 
 
创建TEST数据库。
 
CREATE TABLE [dbo].[Books] (
 [ID] [int] IDENTITY (1, 1) NOT NULL ,
 [BookTitle] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
 [ClassOne] [bit] NULL ,
 [ClassTwo] [bit] NULL ,
 [ClassThree] [bit] NULL ,
 [IsTop] [bit] NULL ,
 [IsPassport] [bit] NULL
) ON [PRIMARY]
GO
 
程序员.JACKY(48327206) 20:41:00
创建表。
 
INSERT INTO Books
      (BookTitle, ClassOne, ClassTwo, ClassThree, IsTop, IsPassport)
VALUES ('ASP.NET程序设计', 1, 0, 0, 1, 1)
          INSERT
        INTO Books(BookTitle, ClassOne, ClassTwo, ClassThree, IsTop, IsPassport)
VALUES ('.NET FRAMEWORK精解', 1, 0, 0, 0, 1)
          INSERT
        INTO Books(BookTitle, ClassOne, ClassTwo, ClassThree, IsTop, IsPassport)
VALUES ('摄影广告', 0, 1, 0, 1, 1)
          INSERT
        INTO Books(BookTitle, ClassOne, ClassTwo, ClassThree, IsTop, IsPassport)
VALUES ('摄影技巧', 0, 1, 0, 1, 0)
          INSERT
        INTO Books(BookTitle, ClassOne, ClassTwo, ClassThree, IsTop, IsPassport)
VALUES ('中国万年历', 0,0, 1, 1, 0)
 
填入值。
 
这样的方式,请大家有兴趣的可以试试。
 
 
我觉得效率上可以再度优化,但保证了灵活性。
 
classone|classtwo|ClassThree

不管你填什么值,都可以匹配。 
 
必竟是以010101010这样方式取匹配的值,某种意义上说,AND 和 OR 可以通用。 

 

posted on 2007-01-31 21:31  oldjacky  阅读(2205)  评论(9编辑  收藏  举报