同一服务器(SQL Server)中有多个数据库。我的要求是当在C#Windows窗体应用程序中给出GUID输入时,我必须在所有数据库中查询与该GUID相对应的详细信息并在应用程序中显示它。表名在所有数据库中都相同。也就是说,如果在一个数据库中找不到详细信息,那么我必须检入另一个数据库(在同一服务器内)并且必须遍历循环。但是,由于有超过10个数据库,因此会达到性能。那么,有没有办法以有效的方式获取细节?我已经广泛搜索但找不到任何解决方案
我没有权限在数据库服务器中创建类似View的权限。我有权仅使用选择查询从任何这些数据库中获取数据
如果需要更多细节,请告诉我
答案 0 :(得分:1)
根据您提供的信息,听起来您需要查看许多数据库,但是您只需要遍历它们,直到找到所需内容,或直到您用完数据库进行查看。这意味着如果您在DB#3中找到所需内容,则不应进入DB#4,#5,依此类推。希望这会有所帮助。对于我来说,这个伟大的表示你不需要查询你不需要的DB。
public Form1()
{
InitializeComponent();
string textThatWouldBeInATextBoxOnTheUserInterface = "1234,4567,789";//lets pretend this is the textbox value entered by the user
string[] GUIDs = textThatWouldBeInATextBoxOnTheUserInterface.Split(',').ToArray();//split that string into a string[]
List<string> listOfResults = new List<string>();//create something to hold your results
//iterate through each string
foreach (string item in GUIDs)
{
listOfResults.Add(getTheData(item));//add results to your list
}
}
private string getTheData(string incomingID)
{
List<string> dbConnStrings = new List<string>();
dbConnStrings.Add(@"Provider=SQLOLEDB;Server=ServerName\Schema;Database=dbName1;User Id=uid;Password=pwd;");
dbConnStrings.Add(@"Provider=SQLOLEDB;Server=ServerName\Schema;Database=dbName2;User Id=uid;Password=pwd;");
string sqlQuery = "select whatever from TheTable where MyRestriction = " + incomingID;
bool HaveYouFoundItYet = false;
int TrackMyPositionInTheList = 0;
string queryResults = string.Empty;
while (!HaveYouFoundItYet && TrackMyPositionInTheList < dbConnStrings.Count)//while you haven't found it and there are still DBs to look into
{
using (OleDbConnection currentConnection = new OleDbConnection(dbConnStrings[TrackMyPositionInTheList]))//look into the DB determined by the PositionTracker
{
currentConnection.Open();
OleDbCommand command = new OleDbCommand(sqlQuery, currentConnection);
if (command.ExecuteScalar() != null)//if you found what you're looking for
{
queryResults = command.ExecuteScalar().ToString();//do whatever with your pulled data
HaveYouFoundItYet = true;//make the while loop stop
}
else
{
TrackMyPositionInTheList++;//Lets increment the PositionTracker by 1 and look again.
}
}
}
return queryResults;
}
修改的 这可能是更快的方式
/// <summary>
/// this way should be faster than the previous one
/// because we won't connect to the same DB more than once.
/// </summary>
public Form1()
{
InitializeComponent();
//string textThatWouldBeInATextBoxOnTheUserInterface = "30439,30447,32273,32270";//lets pretend this is the textbox value entered by the user
string textThatWouldBeInATextBoxOnTheUserInterface = "30439,30447,32273";//lets pretend this is the textbox value entered by the user
Dictionary<string, string> DictionaryOfResults = new Dictionary<string, string>();
DictionaryOfResults = getTheDataLoopingThroughIDs(textThatWouldBeInATextBoxOnTheUserInterface);
}
private Dictionary<string, string> getTheDataLoopingThroughIDs(string incomingIDs)
{
Dictionary<string, string> incomingIDsTracker = new Dictionary<string, string>();//something to keep track of the incomingIDs
foreach (string item in incomingIDs.Replace(" ", "").Split(','))
{
incomingIDsTracker.Add(item, null);//add all the ids(keys) to the dictionary, and give them a null value to start
}
//lets build the list of DBs we can connect to
List<string> dbConnStrings = new List<string>();
dbConnStrings.Add(@"Provider=SQLOLEDB;Server=Server\Schema;Database=dbName1;User Id=uid;Password=pwd;");
dbConnStrings.Add(@"Provider=SQLOLEDB;Server=Server\Schema;Database=dbName2;User Id=uid;Password=pwd;");
int TrackMyPositionInTheList = 0;//keep track of how many DBs you connected to
while (incomingIDsTracker.ContainsValue(null) && TrackMyPositionInTheList < dbConnStrings.Count)//while there are values we haven't found yet, and while there are still DBs to connect to
{
using (OleDbConnection currentConnection = new OleDbConnection(dbConnStrings[TrackMyPositionInTheList]))//look into the DB determined by the PositionTracker
{
Dictionary<string, string> tempdict = new Dictionary<string, string>();
tempdict = incomingIDsTracker.Where(p => p.Value == null).ToDictionary(p => p.Key, p => p.Value);//make a temp dictionary of only the keys with null values
string idstolookfor = string.Join(",", tempdict.Keys);//make that into a comma separated list that we can use in our sql query
string sqlQuery = "select dialid, docid from dial where dialid in (" + idstolookfor + ")";
currentConnection.Open();
OleDbCommand command = new OleDbCommand(sqlQuery, currentConnection);
if (command.ExecuteScalar() != null)//if you found something
{
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
incomingIDsTracker[reader["DialId"].ToString()] = reader["docid"].ToString();
}
}
TrackMyPositionInTheList++;//Lets increment the PositionTracker by 1 and look again.
}
}
return incomingIDsTracker;
}
答案 1 :(得分:0)
您可以对CancellationToken使用async / await。 启动多个同时发生的异步请求,当一个返回结果时取消其他请求。 例如:
Sub CheckWord()
Dim arrVar As Variant
Dim ws As Worksheet
Dim strCheck As Range
Set arrVar = ActiveWorkbook.Worksheets
'MsgBox (arrVar)
For Each ws In arrVar
If ws.Range("C9:G20").Value = "Word" Then
MsgBox (True)
End If
Next ws
End Sub
答案 2 :(得分:0)
您可以使用三部分名称
Select col from db1.dbo.table2 where guid = 'xxx'
union
Select col from db2.dbo.table2 where guid = 'xxx'