step by step!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;

namespace AccessQuery
{
class Program
{
static void Main(string[] args)
{
string database;
Console.Write(
"输入Access文件的位置:");
database
= Console.ReadLine();
OleDbDataReader reader;
ADDO datasource
= new ADDO(database);

string sql = " ";
while (true)
{
Console.WriteLine(
"请输入查询语句,输入q退出");
Console.Write(
">");
sql
= Console.ReadLine();

string type = sql.Substring(0, sql.IndexOf(" ",StringComparison.CurrentCultureIgnoreCase));
Console.WriteLine(
"------------------" + type + "--------------------");
switch (type)
{
case "select":

reader
= datasource.ExcuteSql(sql);
if (reader == null)
break;
while (reader.Read())
{
int i = 0;
while (i < reader.FieldCount)
{
Console.Write(reader[i].ToString());
i
++;
}
Console.Write(
"\n");
}
reader.Close();
break;
case "insert":
case "delete":
case "update":
int count = datasource.ExcuteNoSql(sql);
Console.WriteLine(
"影响到" + count + "");
break;
case "dbdesc":
string[] strtable = datasource.GetTableName();
if(strtable == null)
{
break;
}
foreach (string item in strtable)
{
Console.WriteLine(item);
}
break;
case "tabledesc":
Console.WriteLine(
"功能完善中......");
break;
case "help":
Console.WriteLine(
"dbdesc\ntabledesc\n");
break;
case "q":
datasource.Close();
Environment.Exit(
0);
break;
default:
Console.WriteLine(
"你输入的语句我们的程序现在还不支持,正在完善!");
break;

}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.IO;

namespace AccessQuery
{
class ADDO
{
private string connectString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
private string databasePath = "";

private OleDbConnection conn = new OleDbConnection();
private OleDbCommand command;

public ADDO(string database)
{
while (!File.Exists(database))
{
Console.WriteLine(
"文件不存在,重新输入:");
database
= Console.ReadLine();
}

databasePath
= database;

connectString
+= databasePath;

conn.ConnectionString
= connectString;
try
{
conn.Open();
command
= new OleDbCommand("", conn);
command.CommandTimeout
= 60;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}

public void Close()
{
conn.Close();
}

public OleDbDataReader ExcuteSql(string sql)
{
OleDbDataReader reader;

command.CommandText
= sql;
try
{
reader
= command.ExecuteReader();
return reader;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
return null;
}

public int ExcuteNoSql(string sql)
{
command.CommandText
= sql;
try
{
int count = command.ExecuteNonQuery();
return count;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
return 0;
}

public string[] GetTableName()
{
try
{
DataTable shemaTable
= conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
int n = shemaTable.Rows.Count;
string[] strTable = new string[n];
int m = shemaTable.Columns.IndexOf("TABLE_NAME");

for (int i = 0; i < n; i++)
{
DataRow m_DataRow
= shemaTable.Rows[i];
strTable[i]
= m_DataRow.ItemArray.GetValue(m).ToString();
}
return strTable;
}
catch (OleDbException e)
{
Console.WriteLine(e.Message);
return null;
}
}

//TODO: 得到表的具体信息。
/*
*
public DataColumn[] GetColumnInfo(string tableName)
{
try
{
if (GetTableName().Contains(tableName))
{
DataTable thisTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables_Info, new object[] { null, null, null, "TABLE_INFO" });

}
else
{
Console.WriteLine("输入的信息有误,请再次输入“dbdesc”,查询相关表的信息");
return null;
}
}
catch (OleDbException e)
{
Console.WriteLine(e.Message);
}
return null;
}
*
*/

}
}
posted on 2011-06-06 15:47  myunelf  阅读(305)  评论(0)    收藏  举报