使用C#比较本地数据库的两个表
我是 C# 新手。
我在VS2010中创建了一个本地数据库(.sdf文件)。我正在尝试在数据库列之间创建一些比较。我使用下面的连接字符串成功连接到数据库:
string connectionString = "Provider=Microsoft.SQLSERVER.CE.OLEDB.3.5;data source=c:\\users\\user\\documents\\visual studio 2010\\Projects\\myapp\\myapp\\mydb.sdf";
我的数据库有两个表。昵称和名字。 昵称有一个名为“别名”的字段,它仅列出存储的昵称。 名称有一个名为“文本”的字段,它仅列出存储的名称。
例如:
别名
Masher
Jones
Jaime
John
Joker
假设
John
Adam
Matt
Jones
这些是我的数据库中的值。我想要的是找到不在名称表中的昵称,即 ->在我的例子中,有 Masher、Jaime 和 Joker。
我该怎么做?我使用的是 C# 和 VS2010。
这是我在 SQL 代码方面尝试过的:
"SELECT Alias FROM nicknames WHERE (NOT (Alias IN(SELECT Text FROM Names))) ";
"SELECT Alias FROM nicknames EXCEPT SELECT Text FROM Names";
"SELECT Alias FROM nicknames t LEFT JOIN Names m ON m.Text = t.Alias WHERE m.Text IS NULL";
"SELECT Alias FROM nicknames UNION SELECT Text FROM Names";
我应该做什么?
编辑
OleDbConnection conn = new OleDbConnection(connectionString);
conn.Open();
string sql = "MYQUERYFROMTHEABOVEEXAMPLE";
OleDbCommand cmd = new OleDbCommand(sql, conn);
OleDbDataReader reader;
reader = cmd.ExecuteReader();
string result = "";
while (reader.Read())
{
result += reader.GetString(0) + "\n";
}
这是我用来读取结果的。
I am a newbie in C#.
I created a local database in VS2010(.sdf file). I am trying to create some comparisons between database columns. I successfully connected with the database using the connectionstring below:
string connectionString = "Provider=Microsoft.SQLSERVER.CE.OLEDB.3.5;data source=c:\\users\\user\\documents\\visual studio 2010\\Projects\\myapp\\myapp\\mydb.sdf";
My database has two tables. Nicknames and Names.
Nicknames has one field called "Alias" which simply lists the nicknames stored.
Names has one field called "Text" which simply lists the names stored.
Ex:
Alias
Masher
Jones
Jaime
John
Joker
Names
John
Adam
Matt
Jones
Let's say these are the values in my database. What I want is to find the nicknames who aren't in the table of Names which is -> Masher, Jaime and Joker in my example.
How would i do that? I am using C# and VS2010.
Here is what i tried in terms of SQL codes:
"SELECT Alias FROM nicknames WHERE (NOT (Alias IN(SELECT Text FROM Names))) ";
"SELECT Alias FROM nicknames EXCEPT SELECT Text FROM Names";
"SELECT Alias FROM nicknames t LEFT JOIN Names m ON m.Text = t.Alias WHERE m.Text IS NULL";
"SELECT Alias FROM nicknames UNION SELECT Text FROM Names";
What should i do?
EDIT
OleDbConnection conn = new OleDbConnection(connectionString);
conn.Open();
string sql = "MYQUERYFROMTHEABOVEEXAMPLE";
OleDbCommand cmd = new OleDbCommand(sql, conn);
OleDbDataReader reader;
reader = cmd.ExecuteReader();
string result = "";
while (reader.Read())
{
result += reader.GetString(0) + "\n";
}
This is what i use to read the results.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试:
编辑:尝试这个:
我的尝试@你的代码(忽略我使用SQL语法的事实,因为这就是我使用的):
Try:
Edit: Try this instead:
My attempt @ your code (Ignore the fact I use SQL syntax as that's what I work with):
应该做你想做的事。
should do exactly what you want.