问题描述
我有两个sqlite数据库文件,需要通过button控件和textbox控件打开这两个数据库文件,然后通过查询查询语句下哈到结果,再通过某一个字段将相同的内容行全部显示
解决方案
解决方案二:
分别查到两个集合,然后在程序里面通过Linq的Union进行合并
解决方案三:
selecta.id,a.field1,a.field2...,b.field1,b.field2...fromdb1.dbo.table1a,db2.dbo.table2bwherea.id=b.id若你的两个库不在一个服务器下,则要建立“链接服务器”,然后和上面一样
解决方案四:
//SQLiteHelper.csusingSystem;usingSystem.Collections.Generic;usingSystem.Diagnostics;usingSystem.Linq;usingSystem.Text;usingTRACE=System.Diagnostics.Debug;usingSystem.Data;usingSystem.Data.SQLite;namespaceSQLiteDemo{publicclassSQLiteHelper{conststring_className="SQLiteDemo.SQLiteHelper";conststring_DATA_PATH="datasource=.\Data\users.db";conststring_DB_DEPARTMENT_NAME=".\Data\department.db";staticSQLiteConnections_sqliteConnection;#region<<私有>>staticvoidattach(Stringfilepath){stringstrSQL="ATTACH'"+filepath+"'AS"+System.IO.Path.GetFileNameWithoutExtension(filepath);using(SQLiteCommandcmd=newSQLiteCommand(strSQL,s_sqliteConnection)){cmd.ExecuteNonQuery();}}#endregion#region<<公开>>publicstaticboolopen(){s_sqliteConnection=newSQLiteConnection(_DATA_PATH);s_sqliteConnection.Open();attach(_DB_DEPARTMENT_NAME);returntrue;}publicstaticboolclose(){s_sqliteConnection.Close();returnfalse;}publicstaticDataTableopenSQL(stringstrSQL){DataTabletableResult=newDataTable();using(SQLiteCommandcmd=newSQLiteCommand(strSQL,s_sqliteConnection)){SQLiteDataReaderreader=cmd.ExecuteReader();tableResult.Load(reader);}returntableResult;}#endregion}}
***************************测试usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespaceSQLiteDemo{publicpartialclassfrmMain:Form{publicfrmMain(){InitializeComponent();SQLiteHelper.open();stringstrSQL=@"SELECT*FROMusersinfouiLEFTJOINdepartment.depdONui.[id]=d.[userId];";DataTabletable=SQLiteHelper.openSQL(strSQL);foreach(DataRowrowintable.Rows){Console.WriteLine("userName:{0},department:{1}",row["userName"],row["depName"]);}}protectedoverridevoidOnClosing(CancelEventArgse){SQLiteHelper.close();base.OnClosing(e);}}}
解决方案五:
userInfoid,int,自增userName,text(255),用户姓名表名:departmentid,int,自增userId,int,关联userInfo的iddepName,text(255),部门名称