hbhbice

导航

SQLiteTest

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data.SQLite;
namespace SelectTest
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
string DBFilePath = @"C:\Documents and Settings\Lenovo\桌面\Email\Email.db";
            
if (!File.Exists (DBFilePath ))
            {
                Console.WriteLine(
"文件不存在");
            }
            SQLiteConnection  dbConnection 
= new SQLiteConnection();
            dbConnection.ConnectionString 
= 
                
@"Data Source = "+DBFilePath ;
            dbConnection.Open();
            SQLiteCommand command 
= new SQLiteCommand();
            command .Connection 
=dbConnection ;
            command.CommandText 
= "SELECT ID ,Status,DateTime from INBOX order by DateTime";

            SQLiteDataReader dr 
=command .ExecuteReader ();
            
string strData = string.Empty;

            
while (dr.Read ())
            {
                
string outPut = string.Empty;
                
for (int i = 0; i < dr.VisibleFieldCount ; i++)
                {
                    
                    
string strType = dr.GetDataTypeName(i);
                    
                    
if (strType == "Boolean")
                    {
                        strData 
= dr.GetBoolean(i).ToString();
                    }
                    
else if (strType == "Text")
                    {
                        strData 
= dr.GetString(i);
                    }
                    
else if (strType == "DATETIME")
                    {
                        strData 
= dr.GetString(i);
                    }
                    
else
                    {
                        strData 
= dr.GetValue(i).ToString();
                    }
                    outPut 
= outPut + "  " + strData;
                }
                Console.WriteLine(outPut );
            }
            Console.Read();
            dr.Close();
            dbConnection.Close();
        }
    }
}

 

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SQLite;
using System.IO;
namespace SQLiteTest
{
    
class Program
    {
        
static void Main(string[] args)
        {
            SQLiteConnection connection
= null;
            
try
            {
                
string databaseFileName = "C:\\hbhbice.db";
                
if (!File.Exists(databaseFileName))
                {
                    SQLiteConnection.CreateFile(databaseFileName);
                }
                connection 
= new SQLiteConnection(
                    
"Data Source=" + databaseFileName);
                connection.Open();
                
using (SQLiteCommand command = new SQLiteCommand(connection))
                {
                    command.CommandText 
= "SELECT * FROM Student";
                    command.ExecuteNonQuery();
                    
using (SQLiteDataReader reader = command.ExecuteReader())
                    {
                        StringBuilder sb 
= new StringBuilder();
                        
while (reader.Read())
                        {
                            sb.Append(
"ID\r\n" + reader.GetString(0));
                            sb.Append(
"\r\nName\r\n" + reader.GetString(1));

                        }
                        Console.WriteLine(sb.ToString());
                    }
                }
                Console.ReadLine();
                
            }
            
catch (Exception E)
            {

                
throw E;
            }
            
finally
            {
                
if (connection .State == System.Data.ConnectionState .Open )
                {
                    connection.Close();
                }
            }
 
        }
    }
}

 

 

 

posted on 2010-12-02 10:01  hbhbice  阅读(259)  评论(0编辑  收藏  举报