使用C#比较本地数据库的两个表

发布于 2024-12-13 04:44:05 字数 1448 浏览 0 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

老街孤人 2024-12-20 04:44:05

尝试:

SELECT Alias FROM nicknames WHERE Alias not in (SELECT Text FROM Names)

编辑:尝试这个:

select a.Alias
from nicknames a left outer join Names n on a.alias = n.text 
where n.text IS NULL

我的尝试@你的代码(忽略我使用SQL语法的事实,因为这就是我使用的):

“在此输入图像描述”

Try:

SELECT Alias FROM nicknames WHERE Alias not in (SELECT Text FROM Names)

Edit: Try this instead:

select a.Alias
from nicknames a left outer join Names n on a.alias = n.text 
where n.text IS NULL

My attempt @ your code (Ignore the fact I use SQL syntax as that's what I work with):

enter image description here

机场等船 2024-12-20 04:44:05
Select [Alias] from [nicknames] where [Alias] not in (select [text] from [names])

应该做你想做的事。

Select [Alias] from [nicknames] where [Alias] not in (select [text] from [names])

should do exactly what you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文